Browse Source

remove -Wconversion warnings

master
Felix von Leitner 8 years ago
parent
commit
ddec9f403a
  1. 4
      scan/scan_8int.c
  2. 2
      scan/scan_8long.c
  3. 6
      scan/scan_8short.c
  4. 2
      scan/scan_asn1derlength.c
  5. 12
      scan/scan_int.c
  6. 8
      scan/scan_longn.c
  7. 2
      scan/scan_nonwhitenskip.c
  8. 4
      scan/scan_uint.c
  9. 4
      scan/scan_ulonglong.c
  10. 4
      scan/scan_ulongn.c
  11. 4
      scan/scan_utf8.c
  12. 21
      test/marshal.c

4
scan/scan_8int.c

@ -4,11 +4,11 @@ size_t scan_8int(const char* src,unsigned int* dest) {
register const char *tmp=src;
register unsigned int l=0;
register unsigned char c;
while ((c=*tmp-'0')<8) {
while ((c=(char)(*tmp-'0'))<8) {
if (l>>(sizeof(l)*8-3)) break;
l=l*8+c;
++tmp;
}
*dest=l;
return tmp-src;
return (size_t)(tmp-src);
}

2
scan/scan_8long.c

@ -1,5 +1,5 @@
#include "scan.h"
size_t scan_8long(const char *src,unsigned long *dest) {
return scan_8longn(src,-1,dest);
return scan_8longn(src,(size_t)-1,dest);
}

6
scan/scan_8short.c

@ -4,11 +4,11 @@ size_t scan_8short(const char* src,unsigned short* dest) {
register const char *tmp=src;
register unsigned short l=0;
register unsigned char c;
while ((c=*tmp-'0')<8) {
while ((c=(unsigned char)(*tmp-'0'))<8) {
if (l>>(sizeof(l)*8-3)) break;
l=l*8+c;
l=(unsigned short)(l*8+c);
++tmp;
}
*dest=l;
return tmp-src;
return (size_t)(tmp-src);
}

2
scan/scan_asn1derlength.c

@ -21,5 +21,5 @@ size_t scan_asn1derlength(const char* src,size_t len,unsigned long long* length)
src++;
if (src+*length>max) return 0; /* catch integer overflow */
if ((uintptr_t)src+*length<(uintptr_t)src) return 0; /* gcc 4.1 removes this check without the cast to uintptr_t */
return src-orig;
return (size_t)(src-orig);
}

12
scan/scan_int.c

@ -6,15 +6,15 @@ size_t scan_int(const char* src,int* dest) {
register const char *tmp;
register int l;
register unsigned char c;
int neg;
unsigned int neg;
int ok;
tmp=src; l=0; ok=neg=0;
tmp=src; l=0; ok=0; neg=0;
switch (*tmp) {
case '-': neg=1;
case '+': ++tmp;
}
while ((c=(unsigned char)(*tmp-'0'))<10) {
unsigned long int n;
unsigned int n;
/* we want to do: l=l*10+c
* but we need to check for integer overflow.
* to check whether l*10 overflows, we could do
@ -23,12 +23,12 @@ size_t scan_int(const char* src,int* dest) {
* so instead of *10 we do (l<<3) (i.e. *8) + (l<<1) (i.e. *2)
* and check for overflow on all the intermediate steps */
n=(unsigned int)l<<3; if ((n>>3)!=(unsigned int)l) break;
if (n+(l<<1) < n) break;
n+=l<<1;
if (n+((unsigned int)l<<1) < n) break;
n+=(unsigned int)l<<1;
if (n+c < n) break;
n+=c;
if (n > maxint+neg) break;
l=n;
l=(int)n;
++tmp;
ok=1;
}

8
scan/scan_longn.c

@ -6,7 +6,7 @@ size_t scan_longn(const char *src,size_t n,long *dest) {
register const char *tmp;
register long int l;
register unsigned char c;
int neg;
unsigned int neg;
int ok;
if (!n--) return 0;
tmp=src; l=0; ok=neg=0;
@ -24,12 +24,12 @@ size_t scan_longn(const char *src,size_t n,long *dest) {
* so instead of *10 we do (l<<3) (i.e. *8) + (l<<1) (i.e. *2)
* and check for overflow on all the intermediate steps */
n=(unsigned long)l<<3; if ((n>>3)!=(unsigned long)l) break;
if (n+(l<<1) < n) break;
n+=l<<1;
if (n+((unsigned long)l<<1) < n) break;
n+=(unsigned long)l<<1;
if (n+c < n) break;
n+=c;
if (n > maxlong+neg) break;
l=n;
l=(long)n;
++tmp;
ok=1;
}

2
scan/scan_nonwhitenskip.c

@ -5,5 +5,5 @@ size_t scan_nonwhitenskip(const char *s,size_t limit) {
register const char *t=s;
register const char *u=t+limit;
while (t<u && !isspace(*t)) ++t;
return t-s;
return (size_t)(t-s);
}

4
scan/scan_uint.c

@ -14,7 +14,7 @@ size_t scan_uint(const char* src,unsigned int* dest) {
register const char *tmp=src;
register unsigned int l=0;
register unsigned char c;
while ((c=*tmp-'0')<10) {
while ((c=(char)(*tmp-'0'))<10) {
unsigned int n;
/* division is very slow on most architectures */
n=l<<3; if ((n>>3)!=l) break;
@ -25,6 +25,6 @@ size_t scan_uint(const char* src,unsigned int* dest) {
++tmp;
}
if (tmp-src) *dest=l;
return tmp-src;
return (size_t)(tmp-src);
}
}

4
scan/scan_ulonglong.c

@ -4,7 +4,7 @@ size_t scan_ulonglong(const char *src,unsigned long long *dest) {
register const char *tmp=src;
register unsigned long long l=0;
register unsigned char c;
while ((c=*tmp-'0')<10) {
while ((c=(char)(*tmp-'0'))<10) {
unsigned long long n;
/* division is very slow on most architectures */
n=l<<3; if ((n>>3)!=l) break;
@ -15,5 +15,5 @@ size_t scan_ulonglong(const char *src,unsigned long long *dest) {
++tmp;
}
if (tmp-src) *dest=l;
return tmp-src;
return (size_t)(tmp-src);
}

4
scan/scan_ulongn.c

@ -4,7 +4,7 @@ size_t scan_ulongn(const char* src,size_t n,unsigned long int* dest) {
register const char *tmp=src;
register unsigned long int l=0;
register unsigned char c;
while (n-->0 && (c=*tmp-'0')<10) {
while (n-->0 && (c=(char)(*tmp-'0'))<10) {
unsigned long int n;
/* we want to do: l=l*10+c
* but we need to check for integer overflow.
@ -21,5 +21,5 @@ size_t scan_ulongn(const char* src,size_t n,unsigned long int* dest) {
++tmp;
}
if (tmp-src) *dest=l;
return tmp-src;
return (size_t)(tmp-src);
}

4
scan/scan_utf8.c

@ -39,7 +39,7 @@ size_t scan_utf8(const char* in,size_t len,uint32_t* num) {
* expression can be written as
* 1 << (k*5-4+(k==2))
*/
m=(1<<(k*5-4+(k==2)));
m=((uint32_t)1<<(k*5-4+(k==2)));
while (k>1) {
if ((*in&0xc0)!=0x80) return 0;
i=(i<<6) | ((*in++)&0x3f);
@ -47,6 +47,6 @@ size_t scan_utf8(const char* in,size_t len,uint32_t* num) {
}
if (i<m) return 0; /* if the encoded value was less than m, reject */
if (num) *num=i;
return in-orig;
return (size_t)(in-orig);
}

21
test/marshal.c

@ -137,7 +137,26 @@ int main() {
assert(fmt_escapecharquotedprintableutf8(NULL,0xf6)==6); // =c3=b6
zap(); assert(fmt_escapecharquotedprintableutf8(buf,0xf6)==6 && byte_equal(buf,7,"=c3=b6_"));
assert(scan_ulong("23",&ul)==2 && ul==23);
assert(scan_ulong("23.",&ul)==2 && ul==23);
assert(scan_uint("23.",&ui)==2 && ui==23);
assert(scan_ushort("23.",&us)==2 && us==23);
assert(scan_ulonglong("23.",&ull)==2 && ull==23);
assert(scan_long("23.",&l)==2 && l==23);
assert(scan_int("23.",&i)==2 && i==23);
assert(scan_short("23.",&s)==2 && s==23);
assert(scan_longlong("23.",&ll)==2 && ll==23);
assert(scan_8long("23.",&l)==2 && l==023);
assert(scan_8int("23.",&i)==2 && i==023);
assert(scan_8short("23.",&s)==2 && s==023);
assert(scan_8longlong("23.",&ll)==2 && ll==023);
assert(scan_xlong("23.",&l)==2 && l==0x23);
assert(scan_xint("23.",&i)==2 && i==0x23);
assert(scan_xshort("23.",&s)==2 && s==0x23);
assert(scan_xlonglong("23.",&ll)==2 && ll==0x23);
assert(scan_ulong("46halbe",&ul)==2 && ul==46);
if (sizeof(ul)==4) {
assert(scan_ulong("4294967295",&ul)==10 && ul==0xffffffff);

Loading…
Cancel
Save