fmt_xlonglong was utterly broken (Johannes Vetter)

master
leitner 17 years ago
parent f28b0ee6b2
commit 3c31c1a03b

@ -8,6 +8,7 @@
use inttypes.h to declare ints in uint*.h use inttypes.h to declare ints in uint*.h
escape more in fmt_ldapescape escape more in fmt_ldapescape
try to catch malicious input in textcode fmt_* functions try to catch malicious input in textcode fmt_* functions
fmt_xlonglong was utterly broken (Johannes Vetter)
0.25: 0.25:
array_allocate no longer truncates the array array_allocate no longer truncates the array

@ -1,10 +1,17 @@
#include "fmt.h" #include "fmt.h"
static inline char tohex(char c) {
return c>=10?c-10+'a':c+'0';
}
size_t fmt_xlonglong(char *dest,unsigned long long i) { size_t fmt_xlonglong(char *dest,unsigned long long i) {
int tmp=0; unsigned long long len,tmp;
if (i>>32) { /* first count the number of bytes needed */
tmp=fmt_xlong(dest,i>>32); for (len=1, tmp=i; tmp>15; ++len) tmp>>=4;
if (dest) dest+=tmp; if (dest)
} for (tmp=i, dest+=len; ; ) {
return tmp+fmt_xlong(dest,i&0xffffffff); *--dest = tohex(tmp&15);
if (!(tmp>>=4)) break;
}
return len;
} }

@ -2,6 +2,7 @@
#include <str.h> #include <str.h>
#include <assert.h> #include <assert.h>
#include <scan.h> #include <scan.h>
#include <byte.h>
int main() { int main() {
char buf[1024]; char buf[1024];
@ -25,5 +26,7 @@ int main() {
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); assert(scan_longlong(buf,&l)==11); assert(l==-1234567890);
assert(fmt_xlonglong(buf,0x8000000000000000)==16 && byte_equal(buf,16,"8000000000000000"));
return 0; return 0;
} }

Loading…
Cancel
Save