add beginnings of integer range checking
This commit is contained in:
parent
5674c5186d
commit
72ba2ad6b5
1
CHANGES
1
CHANGES
@ -16,6 +16,7 @@
|
|||||||
fix typo breaking buffer_GETC in buffer (Marcus Winkler)
|
fix typo breaking buffer_GETC in buffer (Marcus Winkler)
|
||||||
fix typo breaking fmt_long for dest==NULL
|
fix typo breaking fmt_long for dest==NULL
|
||||||
add fmt_*longlong()
|
add fmt_*longlong()
|
||||||
|
add range check to scan_ulong, scan_ulonglong
|
||||||
|
|
||||||
0.14:
|
0.14:
|
||||||
avoid bus errors in byte_copy
|
avoid bus errors in byte_copy
|
||||||
|
5
scan.h
5
scan.h
@ -27,6 +27,11 @@ extern unsigned int scan_8long(const char *src,unsigned long *dest);
|
|||||||
* and return the number of bytes that were parsed */
|
* and return the number of bytes that were parsed */
|
||||||
extern unsigned int scan_long(const char *src,signed long *dest);
|
extern unsigned int scan_long(const char *src,signed long *dest);
|
||||||
|
|
||||||
|
extern unsigned int scan_longlong(const char *src,signed long long *dest);
|
||||||
|
extern unsigned int scan_ulonglong(const char *src,unsigned long long *dest);
|
||||||
|
extern unsigned int scan_xlonglong(const char *src,unsigned long long *dest);
|
||||||
|
extern unsigned int scan_8longlong(const char *src,unsigned long long *dest);
|
||||||
|
|
||||||
extern unsigned int scan_uint(const char *src,unsigned int *dest);
|
extern unsigned int scan_uint(const char *src,unsigned int *dest);
|
||||||
extern unsigned int scan_xint(const char *src,unsigned int *dest);
|
extern unsigned int scan_xint(const char *src,unsigned int *dest);
|
||||||
extern unsigned int scan_8int(const char *src,unsigned int *dest);
|
extern unsigned int scan_8int(const char *src,unsigned int *dest);
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
unsigned int scan_long(const char *src,long *dest) {
|
unsigned int scan_long(const char *src,long *dest) {
|
||||||
register const char *tmp;
|
register const char *tmp;
|
||||||
register int l;
|
register long int l;
|
||||||
register unsigned char c;
|
register unsigned char c;
|
||||||
int neg;
|
int neg;
|
||||||
tmp=src; l=0; neg=0;
|
tmp=src; l=0; neg=0;
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
#include "scan.h"
|
#include "scan.h"
|
||||||
|
|
||||||
unsigned int scan_ulong(const char *src,unsigned long *dest) {
|
unsigned int scan_ulong(const char* src,unsigned long int* dest) {
|
||||||
register const char *tmp=src;
|
register const char *tmp=src;
|
||||||
register int l=0;
|
register unsigned long int l=0;
|
||||||
register unsigned char c;
|
register unsigned char c;
|
||||||
while ((c=*tmp-'0')<10) {
|
while ((c=*tmp-'0')<10) {
|
||||||
|
unsigned long int m=l;
|
||||||
l=l*10+c;
|
l=l*10+c;
|
||||||
|
if ((l>>3) < m) break;
|
||||||
++tmp;
|
++tmp;
|
||||||
}
|
}
|
||||||
*dest=l;
|
*dest=l;
|
||||||
|
@ -4,18 +4,24 @@
|
|||||||
|
|
||||||
main() {
|
main() {
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
|
long long l;
|
||||||
|
|
||||||
assert(fmt_longlong(0,12345)==5);
|
assert(fmt_longlong(0,12345)==5);
|
||||||
assert(fmt_longlong(0,-12345)==6);
|
assert(fmt_longlong(0,-12345)==6);
|
||||||
assert(fmt_longlong(buf,12345)==5); buf[5]=0;
|
assert(fmt_longlong(buf,12345)==5); buf[5]=0;
|
||||||
assert(str_equal(buf,"12345"));
|
assert(str_equal(buf,"12345"));
|
||||||
|
assert(scan_longlong(buf,&l)==5); assert(l==12345);
|
||||||
assert(fmt_longlong(buf,-12345)==6); buf[6]=0;
|
assert(fmt_longlong(buf,-12345)==6); buf[6]=0;
|
||||||
assert(str_equal(buf,"-12345"));
|
assert(str_equal(buf,"-12345"));
|
||||||
|
assert(scan_longlong(buf,&l)==6); assert(l==-12345);
|
||||||
|
|
||||||
assert(fmt_longlong(0,1234567890)==10);
|
assert(fmt_longlong(0,1234567890)==10);
|
||||||
assert(fmt_longlong(0,-1234567890)==11);
|
assert(fmt_longlong(0,-1234567890)==11);
|
||||||
assert(fmt_longlong(buf,1234567890)==10); buf[10]=0;
|
assert(fmt_longlong(buf,1234567890)==10); buf[10]=0;
|
||||||
assert(str_equal(buf,"1234567890"));
|
assert(str_equal(buf,"1234567890"));
|
||||||
|
assert(scan_longlong(buf,&l)==10); assert(l==1234567890);
|
||||||
|
|
||||||
assert(fmt_longlong(buf,-1234567890)==11); buf[11]=0;
|
assert(fmt_longlong(buf,-1234567890)==11); buf[11]=0;
|
||||||
assert(str_equal(buf,"-1234567890"));
|
assert(str_equal(buf,"-1234567890"));
|
||||||
|
assert(scan_longlong(buf,&l)==11); assert(l==-1234567890);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user