diff mbox series

[v2] gawk: Fix intformat ptest failure with clang

Message ID 20261125032545.536406-1-khem.raj@oss.qualcomm.com
State New
Headers show
Series [v2] gawk: Fix intformat ptest failure with clang | expand

Commit Message

Khem Raj Nov. 25, 2026, 3:25 a.m. UTC
format_int() relies on undefined behavior when converting out of range
doubles to integers, which clang optimizes in a way that makes the
intformat test fail. Add a patch to range check before converting.

AI-Generated: Uses Claude Code
Signed-off-by: Khem Raj <khem.raj@oss.qualcomm.com>
---
v2: Use the patch suggested by gawk maintainer ( Arnold ) which also fixes the problem.

 ...efined-behavior-converting-out-of-ra.patch | 53 +++++++++++++++++++
 meta/recipes-extended/gawk/gawk_5.4.1.bb      |  1 +
 2 files changed, 54 insertions(+)
 create mode 100644 meta/recipes-extended/gawk/gawk/0002-printf-Avoid-undefined-behavior-converting-out-of-ra.patch

Comments

Alexander Kanavin Nov. 25, 2026, 9:55 a.m. UTC | #1
On Wed, 25 Nov 2026 at 04:25, Khem Raj via openembedded-core <raj.khem=
gmail.com@lists.openembedded.org> wrote:

> v2: Use the patch suggested by gawk maintainer ( Arnold ) which also fixes
> the problem.
> +Upstream-Status: Pending
> +Reported upstream:
> https://lists.gnu.org/archive/html/bug-gawk/2026-11/msg00028.html


This link does not contain the suggested v2 patch, and my previous comment
(about not using Pending) is not addressed.

Alex
diff mbox series

Patch

diff --git a/meta/recipes-extended/gawk/gawk/0002-printf-Avoid-undefined-behavior-converting-out-of-ra.patch b/meta/recipes-extended/gawk/gawk/0002-printf-Avoid-undefined-behavior-converting-out-of-ra.patch
new file mode 100644
index 0000000000..b81b3a9118
--- /dev/null
+++ b/meta/recipes-extended/gawk/gawk/0002-printf-Avoid-undefined-behavior-converting-out-of-ra.patch
@@ -0,0 +1,53 @@ 
+From e00db6a8ee76f2618f38c0dde477cb111fb0df41 Mon Sep 17 00:00:00 2001
+From: Khem Raj <khem.raj@oss.qualcomm.com>
+Date: Fri, 16 Oct 2026 08:14:03 +0000
+Subject: [PATCH] printf: Avoid undefined behavior converting out of range
+ doubles
+
+format_int() casts the value to uintmax_t (or intmax_t) and then
+checks whether converting it back yields the original value to decide
+if it fits. A double that is out of range for the integer type makes
+the conversion itself undefined behavior, and clang folds the
+round-trip check away, so e.g. sprintf("%x", 2^64) prints
+0x8000000000000000 instead of falling back to %g. This makes the
+intformat test fail when gawk is built with clang.
+
+Check the range before doing the conversion instead.
+
+Upstream-Status: Pending
+Reported upstream: https://lists.gnu.org/archive/html/bug-gawk/2026-11/msg00028.html
+
+Signed-off-by: Khem Raj <khem.raj@oss.qualcomm.com>
+---
+diff --git a/printf.c b/printf.c
+index 02646928..590786c9 100644
+--- a/printf.c
++++ b/printf.c
+@@ -926,23 +926,23 @@ format_integer_digits(NODE *arg, struct flags *flags, bool *used_float)
+ 		chbuf = (flags->format == 'X' ? Uchbuf : lchbuf);
+
+ 		if (tmpval < 0) {
+-			uval = (uintmax_t) (intmax_t) tmpval;
+-			if ((AWKNUM)(intmax_t)uval != double_to_int(tmpval)) {
++			if (tmpval <= INTMAX_MIN) {
+ 				flags->format = 'g';
+ 				free((void *) buf);
+ 				*used_float = true;
+
+ 				return format_float(arg, flags);
+ 			}
++			uval = (uintmax_t) (intmax_t) tmpval;
+ 		} else {
+-			uval = (uintmax_t) tmpval;
+-			if ((AWKNUM)uval != double_to_int(tmpval)) {
++			if (tmpval >= UINTMAX_MAX) {
+ 				flags->format = 'g';
+ 				free((void *) buf);
+ 				*used_float = true;
+
+ 				return format_float(arg, flags);
+ 			}
++			uval = (uintmax_t) tmpval;
+ 		}
+
+ 		// generate the digits backwards.
diff --git a/meta/recipes-extended/gawk/gawk_5.4.1.bb b/meta/recipes-extended/gawk/gawk_5.4.1.bb
index d3bf532528..ba28f8832a 100644
--- a/meta/recipes-extended/gawk/gawk_5.4.1.bb
+++ b/meta/recipes-extended/gawk/gawk_5.4.1.bb
@@ -28,6 +28,7 @@  PACKAGECONFIG[pma-if-64bit] = "--enable-pma,--disable-pma, "
 SRC_URI = "${GNU_MIRROR}/gawk/gawk-${PV}.tar.xz \
            file://run-ptest \
            file://0001-configure.ac-re-enable-disabled-printf-features.patch \
+           file://0002-printf-Avoid-undefined-behavior-converting-out-of-ra.patch \
            "

 SRC_URI[sha256sum] = "07f6f7342b7febe4313fc2c2542ad93d64fe20ad8717200109f105a826f5fd37"