mouse07410 / asn1c

The ASN.1 Compiler
http://lionet.info/asn1c/
BSD 2-Clause "Simplified" License
94 stars 71 forks source link

Unsigned long should be used for 0 <= value <= 18446744073709551615 #80

Open gatopeich opened 3 years ago

gatopeich commented 3 years ago

Compiler is rightly complaining about 18446744073709551615L used in this constraint:

    usageCountUL                    INTEGER (0..18446744073709551615),

==>

memb_usageCountUL_constraint_1(const asn_TYPE_descriptor_t *td, const void *sptr,
            asn_app_constraint_failed_f *ctfailcb, void *app_key) {
    const INTEGER_t *st = (const INTEGER_t *)sptr;
    long value;

    if(!sptr) {
        ASN__CTFAIL(app_key, td, sptr,
            "%s: value not given (%s:%d)",
            td->name, __FILE__, __LINE__);
        return -1;
    }

    if(asn_INTEGER2long(st, &value)) {
        ASN__CTFAIL(app_key, td, sptr,
            "%s: value too large (%s:%d)",
            td->name, __FILE__, __LINE__);
        return -1;
    }

    if((value >= 0L && value <= 18446744073709551615L)) {
        /* Constraint check succeeded */
        return 0;
    } else {
        ASN__CTFAIL(app_key, td, sptr,
            "%s: constraint failed (%s:%d)",
            td->name, __FILE__, __LINE__);
        return -1;
    }
}

Instead, the value should be unsigned long, parsed with asn_INTEGER2ulong, and the number written with UL suffix.

_Note: 18446744073709551615 == max 64-bits value = ULONG_MAX_

mouse07410 commented 3 years ago

Do you have a suggested PR?

gatopeich commented 3 years ago

First I wanted to be sure that I am not missing some tweak or setting, since asn_INTEGER2ulong is there, I hoped somebody would give a quick solution... ... Like "use -fwide-types" :-) (though that would break a lot of my existing code)