commit 1d263bb8b0a2b372050ec285a1306431c203542e
parent 7e4b96f8949a4dc2e1da9a003b148ab0973123b0
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Tue, 17 May 2022 13:21:48 +0200
libc: Fix return value in strftime()
It must return 0 when there is truncation in the output buffer.
Diffstat:
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/src/libc/time/strftime.c b/src/libc/time/strftime.c
@@ -169,9 +169,10 @@ strftime(char *restrict s, size_t maxsize,
{
int ch, abrev, val, fill, width;
size_t n, inc;
- char *tfmt;
+ char *tfmt, *begin;
- for (n = --maxsize; (ch = *format++) && n > 0; s += inc, n -= inc) {
+ begin = s;
+ for (n = maxsize; (ch = *format++) && n > 0; s += inc, n -= inc) {
if (ch != '%') {
*s = ch;
inc = 1;
@@ -320,7 +321,11 @@ strftime(char *restrict s, size_t maxsize,
break;
}
}
+
+ n = s - begin;
+ if (n == maxsize)
+ return 0;
*s = '\0';
- return maxsize - n;
+ return n;
}