commit da31e976ff59d68d4c5758761e6862c002a283c2
parent a8fd04705818b4ebe1d3ce1463857e4a40da247c
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Mon, 7 Nov 2022 10:27:54 +0100
libc/stdio: Fix strout() returned value
There were several cases where cnt was not correctly calculated, because
len can have a value of -1. The most secure form is counting every time
that putc() is called and then we can be sure that the value is correct.
Diffstat:
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/src/libc/stdio/vfprintf.c b/src/libc/stdio/vfprintf.c
@@ -152,16 +152,16 @@ static size_t
strout(char *s, size_t len, int width, int fill, FILE *restrict fp)
{
int left = 0, adjust, ch, prefix;
- size_t cnt;
+ size_t cnt = 0;
if (width < 0) {
left = 1;
width = -width;
}
+ if (len == (size_t) -1)
+ len = SIZE_MAX;
adjust = len < width ? width - len : 0;
- cnt = adjust + len;
-
if (left)
adjust = -adjust;
@@ -174,18 +174,25 @@ strout(char *s, size_t len, int width, int fill, FILE *restrict fp)
prefix = 0;
while (prefix--) {
putc(*s++, fp);
+ ++cnt;
--len;
}
}
- for ( ; adjust > 0; adjust--)
+ for ( ; adjust > 0; adjust--) {
putc(fill, fp);
+ ++cnt;
+ }
- for ( ; len-- > 0 && (ch = *s) != '\0'; ++s)
+ for ( ; len-- > 0 && (ch = *s) != '\0'; ++s) {
putc(ch, fp);
+ ++cnt;
+ }
- for ( ; adjust < 0; adjust++)
+ for ( ; adjust < 0; adjust++) {
putc(' ', fp);
+ ++cnt;
+ }
return cnt;
}