From: Matthias Kruk Date: Sun, 26 Jul 2020 08:12:19 +0000 (+0900) Subject: libc, kernel/klibc: Fix bug in snprintf() that would cause the wrong string length... X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=0751d99a3ea7ac81e9ce109c7babc9fa6ae64cad;p=corax libc, kernel/klibc: Fix bug in snprintf() that would cause the wrong string length to be compied for %s formats under certain circumstances --- diff --git a/kernel/klibc/stdio.c b/kernel/klibc/stdio.c index 7674e40..302226c 100644 --- a/kernel/klibc/stdio.c +++ b/kernel/klibc/stdio.c @@ -358,8 +358,10 @@ int _convert_str(char *dst, int precision, int padlen, const char *src) slen = strlen(src); padlen -= slen; - while(precision-- > 0 && padlen-- > 0) { + while(precision > 0 && padlen > 0) { dst[ret_val++] = ' '; + precision--; + padlen--; } /* precision may still be larger than slen */ diff --git a/libc/stdio.c b/libc/stdio.c index 7313ff6..fbd9a90 100644 --- a/libc/stdio.c +++ b/libc/stdio.c @@ -20,6 +20,8 @@ #define FLAG_LCHEX (1 << 6) #define FLAG_USIGN (1 << 7) +extern void* memcpy2(void*, const void*, size_t); + static int _convert_oct(char *dst, int flags, int precision, int width, int padlen, void *data) { union { @@ -348,8 +350,10 @@ static int _convert_str(char *dst, int precision, int padlen, const char *src) slen = strlen(src); padlen -= slen; - while(precision-- > 0 && padlen-- > 0) { + while(precision > 0 && padlen > 0) { dst[ret_val++] = ' '; + precision--; + padlen--; } /* precision may still be larger than slen */