mirror of
https://github.com/ARM-software/arm-trusted-firmware.git
synced 2025-04-16 09:34:18 +00:00
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:
parent
e0a6a512b5
commit
c1f5a0925d
1 changed files with 7 additions and 24 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue