summaryrefslogtreecommitdiff
path: root/userland
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-06-26 21:47:15 +0200
committerAnton Kling <anton@kling.gg>2024-06-26 21:47:15 +0200
commitfddda835a28bbc52a8d8450395a3e57e181782a8 (patch)
treef515f65858621e6979bb847848929c9102e8f2d8 /userland
parente15065fe45f8004adcc8b69bbbe680ced0d530a1 (diff)
LibC: Fix regression in printf
printf did not write out anything for %d when the value was zero.
Diffstat (limited to 'userland')
-rw-r--r--userland/libc/stdio/vfprintf.c9
-rw-r--r--userland/test/test.c2
2 files changed, 9 insertions, 2 deletions
diff --git a/userland/libc/stdio/vfprintf.c b/userland/libc/stdio/vfprintf.c
index 2ba3e2c..d384c1d 100644
--- a/userland/libc/stdio/vfprintf.c
+++ b/userland/libc/stdio/vfprintf.c
@@ -28,8 +28,13 @@ int fprint_num(FILE *f, long long n, int base, char *char_set, int prefix,
n *= -1;
}
- for (; n != 0 && i < 32; i++, n /= base)
- str[i] = char_set[(n % base)];
+ if (0 == n) {
+ str[i] = char_set[0];
+ i++;
+ } else {
+ for (; n != 0 && i < 32; i++, n /= base)
+ str[i] = char_set[(n % base)];
+ }
if (is_signed) {
str[i] = '-';
diff --git a/userland/test/test.c b/userland/test/test.c
index 29aa34c..33361a5 100644
--- a/userland/test/test.c
+++ b/userland/test/test.c
@@ -588,6 +588,8 @@ void printf_test(void) {
buf_n = sprintf(buf, "oct: %00o", 1);
EXP("oct: 1");
+ buf_n = sprintf(buf, "int: %d", 0);
+ EXP("int: 0");
buf_n = sprintf(buf, "int: %-2dend", 1);
EXP("int: 1 end");
buf_n = sprintf(buf, "int: %-2dend", 12);