Skip to content

Commit 90b5dea

Browse files
committed
Fix decimal output being printed in reverse in printf()
The __str_base10() function mistakenly reversed the digits again, causing the final string to be printed backward. This patch corrects the digit copy order in __str_base10() and adds a final string reversal in itoa() to restore the correct output order for base 10 conversions. Fixes: b3e756c ("Optimize base-10 conversion for RV32I libc")
1 parent 780dd18 commit 90b5dea

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/libc.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ static char *__str_base10(uint32_t value, char *buffer, int *length)
495495
/* Reverse digits into output buffer */
496496
*length = pos;
497497
for (int i = 0; i < pos; i++) {
498-
buffer[i] = tmp[pos - 1 - i];
498+
buffer[i] = tmp[i];
499499
}
500500

501501
return buffer;
@@ -573,6 +573,14 @@ void itoa(int32_t i, char *s, int32_t base)
573573
}
574574
} else if (base == 10) { /* Decimal conversion */
575575
__str_base10_signed(i, s, &len);
576+
577+
/* Reverse the string. */
578+
q = s + len;
579+
for (*q = 0; p <= --q; p++) {
580+
c = *p;
581+
*p = *q;
582+
*q = c;
583+
}
576584
} else { /* Other bases */
577585
if (i >= 0) {
578586
do {

0 commit comments

Comments
 (0)