fmt_xlonglong was utterly broken (Johannes Vetter)

master
leitner 18 years ago
parent f28b0ee6b2
commit 3c31c1a03b

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

@ -1,10 +1,17 @@
#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) {
int tmp=0;
if (i>>32) {
tmp=fmt_xlong(dest,i>>32);
if (dest) dest+=tmp;
unsigned long long len,tmp;
/* first count the number of bytes needed */
for (len=1, tmp=i; tmp>15; ++len) tmp>>=4;
if (dest)
for (tmp=i, dest+=len; ; ) {
*--dest = tohex(tmp&15);
if (!(tmp>>=4)) break;
}
return tmp+fmt_xlong(dest,i&0xffffffff);
return len;
}

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

Loading…
Cancel
Save