[git commit] awk: fix printf "%o\n", -1

Denys Vlasenko vda.linux at googlemail.com
Mon Jul 6 02:37:44 UTC 2026


commit: https://git.busybox.net/busybox/commit/?id=5de0780eaf4ec779fbcc459f461d961144623082
branch: https://git.busybox.net/busybox/log/?h=master

function                                             old     new   delta
awk_printf                                          1381    1387      +6

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 editors/awk.c       | 10 +++++-----
 testsuite/awk.tests |  2 ++
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/editors/awk.c b/editors/awk.c
index 9a9e79bf8..42a1c3ceb 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -2522,7 +2522,6 @@ static char *awk_printf(node *n, size_t *len)
 				fmt_cur--;
 				slen = fmt_cur - s;
  append_slen:
-				/* append "....%" part verbatim */
 				//s = xstrndup(s, slen);
 				//goto append;
 				/* a bit bloaty, but avoids dup+free: */
@@ -2607,6 +2606,9 @@ static char *awk_printf(node *n, size_t *len)
 				baked_cur += sprintf(baked_cur, "%d", width);
 			}
 			if (diouxX) {
+				/* print %d, %i, positive %u as floats: */
+				/* this avoids losing most-significant bits, try */
+				/* awk 'BEGIN { printf "%d\n", 2**128 }' */
 				sprintf(baked_cur, ".0f%s", fmt_cur + 1);
 			} else {
 				if (prec >= 0)
@@ -2632,15 +2634,13 @@ static char *awk_printf(node *n, size_t *len)
 				s = xasprintf(use_fmt, cs);
 			} else { /* numeric conversion */
 				double d = getvar_i(arg);
-//bb_error_msg("d0:%f", d);
 				if (diouxX)
 					d = trunc(d);
-//bb_error_msg("d1:%f", d);
-				if ((c|0x20) == 'x' || (c == 'u' && d < 0)) {
+//bb_error_msg("d:%f", d);
+				if ((c|0x20) == 'x' || c == 'o' || (c == 'u' && d < 0)) {
 					sprintf(baked_cur, "ll%s", fmt_cur);
 					// -1.9 => -1 => integer (0xfff..fff)
 					// => interpret as unsigned integer
-					// => convert to double as POSITIVE integer
 					// awk 'BEGIN { printf "%u\n",-1.9; }': 18446744073709551615
 					// awk 'BEGIN { printf "%x\n",-1.9; }': ffffffffffffffff
 					s = xasprintf(use_fmt, (unsigned long long)d);
diff --git a/testsuite/awk.tests b/testsuite/awk.tests
index d0b28cf36..f4ddcbdfb 100755
--- a/testsuite/awk.tests
+++ b/testsuite/awk.tests
@@ -82,6 +82,8 @@ testing "awk %u -1.9" \
 "awk 'BEGIN { printf \"%u\n\", -1.9; }'" "18446744073709551615\n" "" ""
 testing "awk %x -1.9" \
 "awk 'BEGIN { printf \"%x\n\", -1.9; }'" "ffffffffffffffff\n" "" ""
+testing "awk %o -1.9" \
+"awk 'BEGIN { printf \"%o\n\", -1.9; }'" "1777777777777777777777\n" "" ""
 
 # 4294967295 = 0xffffffff
 testing "awk bitwise op" "awk '{ print or(4294967295,1) }'" "4294967295\n" "" "\n"


More information about the busybox-cvs mailing list