Kernel, libc: Fix a big bug in printf()

Every time printf flushes the buffer to us in sprintf() or snprintf(), we call strncat to append the data.

But we want to start from the beginning in the first flush. What if there was data already there?
Well, we just append to the old data. Which is not good, and breaks snprintf()'s maximum size policy.

This fix sets the first byte of str to NULL, to avoid this.
This commit is contained in:
apio 2022-10-30 09:53:23 +01:00
parent e705810af3
commit 8d46c9bbe2
2 changed files with 6 additions and 0 deletions

View File

@ -228,6 +228,7 @@ int sprintf(char* __s, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (__s) *__s = 0;
int written = internal_printf(
fmt,
[&](const char* s) {
@ -242,6 +243,7 @@ int snprintf(char* __s, size_t max, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (__s && max) *__s = 0;
int written = internal_printf(
fmt,
[&](const char* s) {
@ -266,6 +268,7 @@ int vkprintf(const char* fmt, va_list ap)
int vsprintf(char* __s, const char* fmt, va_list ap)
{
*__s = 0;
return internal_printf(
fmt,
[&](const char* s) {
@ -276,6 +279,7 @@ int vsprintf(char* __s, const char* fmt, va_list ap)
int vsnprintf(char* __s, size_t max, const char* fmt, va_list ap)
{
if (max) *__s = 0;
return internal_printf(
fmt,
[&](const char* s) {

View File

@ -261,6 +261,7 @@ extern "C"
int vsprintf(char* str, const char* format, va_list ap)
{
if (str) *str = 0; // so strncat starts from the beginning
return internal_printf(
format,
[&](const char* s) {
@ -271,6 +272,7 @@ extern "C"
int vsnprintf(char* str, size_t max, const char* format, va_list ap)
{
if (max && str) *str = 0; // so strncat starts from the beginning
return internal_printf(
format,
[&](const char* s) {