remove -Wconversion warnings

master
leitner 11 years ago
parent 18afec7d68
commit e1081e4b13

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

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

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

@ -21,5 +21,5 @@ size_t scan_asn1derlength(const char* src,size_t len,unsigned long long* length)
src++; src++;
if (src+*length>max) return 0; /* catch integer overflow */ 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 */ 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);
} }

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

@ -6,7 +6,7 @@ size_t scan_longn(const char *src,size_t n,long *dest) {
register const char *tmp; register const char *tmp;
register long int l; register long int l;
register unsigned char c; register unsigned char c;
int neg; unsigned int neg;
int ok; int ok;
if (!n--) return 0; if (!n--) return 0;
tmp=src; l=0; ok=neg=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) * 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 */ * and check for overflow on all the intermediate steps */
n=(unsigned long)l<<3; if ((n>>3)!=(unsigned long)l) break; n=(unsigned long)l<<3; if ((n>>3)!=(unsigned long)l) break;
if (n+(l<<1) < n) break; if (n+((unsigned long)l<<1) < n) break;
n+=l<<1; n+=(unsigned long)l<<1;
if (n+c < n) break; if (n+c < n) break;
n+=c; n+=c;
if (n > maxlong+neg) break; if (n > maxlong+neg) break;
l=n; l=(long)n;
++tmp; ++tmp;
ok=1; ok=1;
} }

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

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

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

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

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

@ -137,7 +137,26 @@ int main() {
assert(fmt_escapecharquotedprintableutf8(NULL,0xf6)==6); // =c3=b6 assert(fmt_escapecharquotedprintableutf8(NULL,0xf6)==6); // =c3=b6
zap(); assert(fmt_escapecharquotedprintableutf8(buf,0xf6)==6 && byte_equal(buf,7,"=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); assert(scan_ulong("46halbe",&ul)==2 && ul==46);
if (sizeof(ul)==4) { if (sizeof(ul)==4) {
assert(scan_ulong("4294967295",&ul)==10 && ul==0xffffffff); assert(scan_ulong("4294967295",&ul)==10 && ul==0xffffffff);

Loading…
Cancel
Save