X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=KernelLand%2FKernel%2Flibc.c;h=572184d9bd5a94a05342526ce4f84f763e03861d;hb=9eadc33399e705035c33e8434a9644d91e44ed44;hp=2950782453180f8ec63d564a7ac88e69793e0434;hpb=776d802dde3d6361d7c700e0c788d64d302bd537;p=tpg%2Facess2.git diff --git a/KernelLand/Kernel/libc.c b/KernelLand/Kernel/libc.c index 29507824..572184d9 100644 --- a/KernelLand/Kernel/libc.c +++ b/KernelLand/Kernel/libc.c @@ -74,6 +74,7 @@ EXPORT(CheckMem); // === CODE === // - Import userland stroi.c file #define _LIB_H_ +#define _SysDebug(f,v...) Log_Debug("libc", f ,## v) #include "../../Usermode/Libraries/libc.so_src/strtoi.c" int ParseInt(const char *string, int *Val) @@ -156,19 +157,20 @@ void itoa(char *buf, Uint64 num, int base, int minLength, char pad) int pos=0, i; Uint64 rem; + buf[0] = 0; + ASSERTR(base >= 2, ); + ASSERTR(base <= 16, ); + // Sanity check if(!buf) return; - // Sanity Check - if(base > 16 || base < 2) { - buf[0] = 0; - return; - } - // Convert while(num > base-1) { num = DivMod64U(num, base, &rem); // Shift `num` and get remainder ASSERT(rem >= 0); + if( rem >= base && base != 16 ) { + Debug("rem(%llx) >= base(%x), num=%llx", rem, base, num); + } ASSERT(rem < base); tmpBuf[pos] = cUCDIGITS[ rem ]; pos++; @@ -316,11 +318,14 @@ int vsnprintf(char *__s, const size_t __maxlen, const char *__format, va_list ar GETVAL(); if( isLongLong && val >> 63 ) { PUTCH('-'); - val = -val; + if( val == LLONG_MIN ) + val = LLONG_MAX; + else + val = -val; } - else if( !isLongLong && val >> 31 ) { + else if( !isLongLong && (val >> 31) ) { PUTCH('-'); - val = -(Sint32)val; + val = (~val & 0xFFFFFFFF)+1; } itoa(tmpBuf, val, 10, minSize, pad); goto printString; @@ -376,7 +381,7 @@ int vsnprintf(char *__s, const size_t __maxlen, const char *__format, va_list ar break; } p = va_arg(args, char*); // Get Argument - if( !p || !CheckString(p) ) p = "(inval)"; // Avoid #PFs + if( p && !CheckString(p) ) goto invalString; // Avoid #PFs printString: if(!p) p = "(null)"; len = strlen(p); @@ -384,13 +389,40 @@ int vsnprintf(char *__s, const size_t __maxlen, const char *__format, va_list ar while(*p && precision--) { PUTCH(*p); p++;} if( bPadLeft ) while(len++ < minSize) PUTCH(pad); break; + invalString: + PUTCH('(');PUTCH('i');PUTCH('n');PUTCH('v');PUTCH('a'); PUTCH('l');PUTCH(':'); + PUTCH('*');PUTCH('0');PUTCH('x'); + val = (tVAddr)p; + for( len = BITS/4; len -- && ((val>>(len*4))&15) == 0; ) + ; + len ++; + if( len == 0 ) + PUTCH( '0' ); + else + while( len -- ) + PUTCH( cUCDIGITS[ (val>>(len*4))&15 ] ); + PUTCH(')'); + break; case 'C': // Non-Null Terminated Character Array p = va_arg(args, char*); - if( !CheckMem(p, minSize) ) continue; // No #PFs please + if( !CheckMem(p, minSize) ) { + p = "(inval)"; + goto printString; + } if(!p) goto printString; while(minSize--) { - PUTCH(*p); + if(*p == '\0') { + PUTCH('\\'); + PUTCH('0'); + } + else if(*p == '\\') { + PUTCH('\\'); + PUTCH('\\'); + } + else { + PUTCH(*p); + } p ++; } break;