fix(libc): fix snprintf corner cases

The number formatting routine in snprintf was trying to be clever with
the buffer handling, but tripped over its own feet: snprintf() users
expect output to be emitted, even if not everything fits into the
buffer. The current code gives up completely when the buffer is too
small.

Fix those issues and simplify the code on the way, by consequently using
the CHECK_AND_PUT_CHAR() macro, which both checks for the buffer size
correctly, but also keeps track of the number of should-be-printed
characters for the return value.

Change-Id: Ifd2b03b9a73f9279abed53081a2d88720ecbdbc1
Signed-off-by: Andre Przywara <andre.przywara@arm.com>
This commit is contained in:
Andre Przywara 2021-12-21 12:35:54 +00:00
parent e0a6a512b5
commit c1f5a0925d
1 changed files with 7 additions and 24 deletions

View File

@ -52,31 +52,14 @@ static void unsigned_num_print(char **s, size_t n, size_t *chars_printed,
} while (unum > 0U);
width = i;
if (padn > width) {
(*chars_printed) += (size_t)padn;
} else {
(*chars_printed) += (size_t)width;
for (i = padn - width; i > 0; i--) {
CHECK_AND_PUT_CHAR(*s, n, *chars_printed, padc);
}
if (*chars_printed < n) {
if (padn > 0) {
while (width < padn) {
*(*s)++ = padc;
padn--;
}
}
while (--i >= 0) {
*(*s)++ = num_buf[i];
}
if (padn < 0) {
while (width < -padn) {
*(*s)++ = padc;
padn++;
}
}
for (i = width; i > 0; i--) {
CHECK_AND_PUT_CHAR(*s, n, *chars_printed, num_buf[i - 1]);
}
for (i = width + padn; i < 0; i++) {
CHECK_AND_PUT_CHAR(*s, n, *chars_printed, padc);
}
}