Patch: Integer type inconsistencies (ITS#1732) ================ Written by Hallvard B. Furuseth and placed into the public domain. This software is not subject to any license of the University of Oslo. ================ Here are some fixes for mismatched integer types; cases where the sign is wrong. Typically a ber_tag_t which receives an int value and is later compared with an int, maybe -1. There are 3 sections: First removal of one strange cast of an unsigned value to (int), where the result is compared with an unsigned value. Either there is some strange purpose to this, or the cast is a holdover from a time when the integer types in the function were different. Section 2 has various plain fixes. Section 3 has patches with no effect except to remove most remaining 'comparison between signed and unsigned' warnings, which is how I caught the above bugs. Either it casts some constant to int, or it declares an index variable as unsigned (typically because it meets a size_t). Hallvard B. Furuseth , April 2002. ######################################################################## Details about section 2, if you are interested: liblber/encode.c (the worst offender): Hunk 1. Change return value of ber_calc_taglen from ber_len_t to int because it returns int and is mostly used in int context. Hunk 2. In ber_put_tag, change taglen and i from ber_len_t to int because ber_calc_taglen now returns int. Hunk 3. In ber_put_int_or_enum, make lenlen and taglen 'int' instead of ber_len_t, because they come from int ber_put_(). The code did `if((unsigned_var = signed_func()) == -1)'. Hunk 4. In ber_put_ostring, change lenlen and taglen to int. Hunk 5. In ber_put_bitstring, change lenlen and taglen to int. Hunk 6. In ber_put_null, change taglen to int. Hunk 7. In ber_put_boolean, change taglen to int. Hunk 8. In ber_put_seqorset, change taglen to int. It comes from int ber_put_tag() and is compared with -1. Hunk 9. ---------"---------, change i from ber_len_t to int, because taglen is now int. In result.c:build_result_ber(), change tag from ber_int_t to ber_tag_t since the value comes from ber_peek_tag(). In search.c:meta_back_search(), change suffixlen from int to ber_len_t since it is used as a bv_len. ######################################################################## Section 1. diff -u2 -r libraries/libldap/cache.c~ libraries/libldap/cache.c --- libraries/libldap/cache.c Mon Apr 8 14:14:16 2002 +++ libraries/libldap/cache.c Mon Apr 8 18:25:09 2002 @@ -497,5 +497,5 @@ ldap_msgfree( m ); } else { - if ( m->lm_msgtype == (int)msgtype && + if ( m->lm_msgtype == msgtype && request_cmp( m->lm_ber, &reqber ) == 0 ) { break; ######################################################################## Section 2. diff -u2 -r libraries/liblber/encode.c~ libraries/liblber/encode.c --- libraries/liblber/encode.c~ Wed Feb 13 21:26:24 2002 +++ libraries/liblber/encode.c Sat Apr 6 20:29:14 2002 @@ -46,5 +46,5 @@ -static ber_len_t +static int ber_calc_taglen( ber_tag_t tag ) { @@ -69,6 +69,6 @@ { int rc; - ber_len_t taglen; - ber_len_t i; + int taglen; + int i; unsigned char nettag[sizeof(ber_tag_t)]; @@ -179,6 +179,6 @@ { int rc; - int i, j, sign; - ber_len_t len, lenlen, taglen; + int i, j, sign, taglen, lenlen; + ber_len_t len; ber_uint_t unum, mask; unsigned char netnum[sizeof(ber_uint_t)]; @@ -277,6 +277,5 @@ ber_tag_t tag ) { - ber_len_t taglen, lenlen; - int rc; + int taglen, lenlen, rc; assert( ber != NULL ); @@ -340,5 +339,6 @@ ber_tag_t tag ) { - ber_len_t taglen, lenlen, len; + int taglen, lenlen; + ber_len_t len; unsigned char unusedbits; @@ -377,5 +377,5 @@ ber_put_null( BerElement *ber, ber_tag_t tag ) { - ber_len_t taglen; + int taglen; assert( ber != NULL ); @@ -403,5 +403,5 @@ ber_tag_t tag ) { - ber_len_t taglen; + int taglen; unsigned char c; @@ -498,5 +498,6 @@ ber_len_t len; unsigned char netlen[sizeof(ber_len_t)]; - ber_len_t taglen, lenlen; + int taglen; + ber_len_t lenlen; unsigned char ltag = 0x80U + FOUR_BYTE_LEN - 1; Seqorset *next; @@ -580,5 +581,5 @@ } else { - ber_len_t i; + int i; unsigned char nettag[sizeof(ber_tag_t)]; ber_tag_t tmptag = (*sos)->sos_tag; diff -u2 -r libraries/libldap/result.c~ libraries/libldap/result.c --- libraries/libldap/result.c~ Mon Apr 1 22:08:32 2002 +++ libraries/libldap/result.c Sat Apr 6 19:57:11 2002 @@ -889,5 +889,5 @@ { ber_len_t len; - ber_int_t tag; + ber_tag_t tag; ber_int_t along; BerElement *ber; diff -u2 -r servers/slapd/back-meta/search.c~ servers/slapd/back-meta/search.c --- servers/slapd/back-meta/search.c~ Sat Apr 6 19:23:02 2002 +++ servers/slapd/back-meta/search.c Sat Apr 6 20:08:01 2002 @@ -198,5 +198,5 @@ char *realbase = ( char * )base->bv_val; int realscope = scope; - int suffixlen; + ber_len_t suffixlen; char *mapped_filter, **mapped_attrs; ######################################################################## Section 3. diff -u2 -r libraries/liblber/io.c~ libraries/liblber/io.c --- libraries/liblber/io.c~ Thu Feb 14 13:32:40 2002 +++ libraries/liblber/io.c Sat Apr 6 19:56:12 2002 @@ -515,5 +515,5 @@ if (*ber->ber_ptr & 0x80) { /* multi-byte */ int llen = *(unsigned char *)ber->ber_ptr++ & 0x7f; - if (llen > sizeof(ber_len_t)) { + if (llen > (int)sizeof(ber_len_t)) { errno = ERANGE; return LBER_DEFAULT; diff -u2 -r libraries/libldap/request.c~ libraries/libldap/request.c --- libraries/libldap/request.c~ Sat Apr 6 18:18:56 2002 +++ libraries/libldap/request.c Sat Apr 6 20:00:30 2002 @@ -900,5 +900,6 @@ int *hadrefp ) { - int rc, count, len; + int rc, count; + unsigned len; char *p, *ref, *unfollowed; LDAPRequest *origreq; diff -u2 -r libraries/libldap/schema.c~ libraries/libldap/schema.c --- libraries/libldap/schema.c~ Fri Jan 4 21:17:39 2002 +++ libraries/libldap/schema.c Sat Apr 6 20:00:48 2002 @@ -2342,5 +2342,5 @@ ldap_scherr2str(int code) { - if ( code < 0 || code >= (sizeof(err2text)/sizeof(char *)) ) { + if ( code < 0 || code >= (int)(sizeof(err2text)/sizeof(char *)) ) { return "Unknown error"; } else { diff -u2 -r libraries/libldap/utf-8-conv.c~ libraries/libldap/utf-8-conv.c --- libraries/libldap/utf-8-conv.c~ Tue Jan 15 08:07:46 2002 +++ libraries/libldap/utf-8-conv.c Sat Apr 6 20:01:50 2002 @@ -88,5 +88,5 @@ utflen = LDAP_UTF8_CHARLEN2(utf8char, utflen); - if( utflen==0 || utflen > LDAP_MAX_UTF8_LEN ) + if( utflen==0 || utflen > (int)LDAP_MAX_UTF8_LEN ) return -1; /* Invalid input */ @@ -133,5 +133,5 @@ utflen = LDAP_UTF8_CHARLEN2(utf8str, utflen); - if( utflen==0 || utflen > LDAP_MAX_UTF8_LEN ) + if( utflen==0 || utflen > (int)LDAP_MAX_UTF8_LEN ) return -1; /* Invalid input */ diff -u2 -r libraries/liblunicode/ure/ure.c~ libraries/liblunicode/ure/ure.c --- libraries/liblunicode/ure/ure.c~ Fri Jan 4 21:17:43 2002 +++ libraries/liblunicode/ure/ure.c Sat Apr 6 19:35:57 2002 @@ -2045,5 +2045,5 @@ if (matched) { - if (ms == ~0) + if (ms == ~0UL) ms = lp - text; else @@ -2120,4 +2120,4 @@ *match_end = me; - return (ms != ~0) ? 1 : 0; + return (ms != ~0UL) ? 1 : 0; } diff -u2 -r libraries/liblutil/passwd.c~ libraries/liblutil/passwd.c --- libraries/liblutil/passwd.c~ Sat Apr 6 19:20:53 2002 +++ libraries/liblutil/passwd.c Sat Apr 6 19:38:59 2002 @@ -637,5 +637,5 @@ const struct berval * cred ) { - int i; + unsigned int i; int rtn; @@ -690,5 +690,5 @@ const struct berval * cred ) { - int i; + unsigned int i; int rtn; @@ -864,5 +864,5 @@ { char *cr; - int i; + unsigned int i; for( i=0; ibv_len; i++) { @@ -906,5 +906,5 @@ const struct berval * cred ) { - int i; + unsigned int i; char *pw,*cr; @@ -1205,5 +1205,5 @@ struct berval hash; unsigned char salt[32]; /* salt suitable for most anything */ - int i; + unsigned int i; for( i=0; ibv_len; i++) { diff -u2 -r servers/slapd/back-ldbm/idl.c~ servers/slapd/back-ldbm/idl.c --- servers/slapd/back-ldbm/idl.c~ Wed Jan 16 09:58:11 2002 +++ servers/slapd/back-ldbm/idl.c Sat Apr 6 20:06:35 2002 @@ -32,5 +32,5 @@ static void cont_id( Datum *cont, ID id ) { - int i; + unsigned int i; for( i=1; i <= sizeof(id); i++) { diff -u2 -r servers/slapd/module.c~ servers/slapd/module.c --- servers/slapd/module.c~ Wed Jan 2 11:00:37 2002 +++ servers/slapd/module.c Sat Apr 6 21:07:20 2002 @@ -167,5 +167,5 @@ } - if (rc >= (sizeof(module_regtable) / sizeof(struct module_regtable_t)) + if (rc >= (int)(sizeof(module_regtable) / sizeof(struct module_regtable_t)) || module_regtable[rc].proc == NULL) { diff -u2 -r servers/slapd/schemaparse.c~ servers/slapd/schemaparse.c --- servers/slapd/schemaparse.c~ Sat Jan 19 04:50:26 2002 +++ servers/slapd/schemaparse.c Sat Apr 6 21:05:33 2002 @@ -46,5 +46,5 @@ scherr2str(int code) { - if ( code < 0 || code >= (sizeof(err2text)/sizeof(char *)) ) { + if ( code < 0 || code >= (int)(sizeof(err2text)/sizeof(char *)) ) { return "Unknown error"; } else {