https://gcc.gnu.org/g:311e3ba953862ea9279b5af443ce6448834505a4

commit r17-6409-g311e3ba953862ea9279b5af443ce6448834505a4
Author: Kyrylo Tkachov <[email protected]>
Date:   Thu Nov 12 01:27:20 2026 -0800

    gimple-ssa-sprintf: Check the range of the %lc argument for ASCII [PR128269]
    
    format_character treats a %lc argument as ASCII when the minimum of its
    value range is in [0, 127] and then clears mayfail.  The maximum is not
    checked, so for a range such as [0, 200] the directive is assumed to
    always succeed although a value above 127 can fail to convert in the C
    locale.  Negative values are also affected, as get_int_range clamps a
    negative minimum to 0 and they convert to large wide characters.  The
    printf return value optimization then folds a test of the snprintf result
    < 0 to false.
    
    For:
    
      int r = snprintf (b, sizeof b, "%lc", (wint_t) x);  /* x in [0, 200] */
      return r < 0;
    
    snprintf returns -1 for x == 128 with glibc, but the comparison was
    folded to 0.
    
    Require the range to be within [0, 127], keeping negative bounds as -1,
    so that ranges containing other values take the branch that sets
    mayfail.
    
    Bootstrapped and tested on aarch64-none-linux-gnu.
    
    gcc/ChangeLog:
    
            PR tree-optimization/128269
            * gimple-ssa-sprintf.cc (format_character): Pass -1 as the negative
            bound to get_int_range.  Check the maximum of the range in the ASCII
            case.
    
    gcc/testsuite/ChangeLog:
    
            PR tree-optimization/128269
            * gcc.dg/torture/pr128269.c: New test.
            * gcc.dg/tree-ssa/pr128269.c: New test.
    
    Signed-off-by: Kyrylo Tkachov <[email protected]>

Diff:
---
 gcc/gimple-ssa-sprintf.cc                |  6 +++--
 gcc/testsuite/gcc.dg/torture/pr128269.c  | 38 ++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr128269.c | 46 ++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+), 2 deletions(-)

diff --git a/gcc/gimple-ssa-sprintf.cc b/gcc/gimple-ssa-sprintf.cc
index d23e4dfc2294..e716f6e2e973 100644
--- a/gcc/gimple-ssa-sprintf.cc
+++ b/gcc/gimple-ssa-sprintf.cc
@@ -2174,8 +2174,10 @@ format_character (const directive &dir, tree arg, 
pointer_query &ptr_qry)
       /* A wide character can result in as few as zero bytes.  */
       res.range.min = 0;
 
+      /* A negative value is represented by -1.  It converts to a wide
+        character outside the ASCII range.  */
       HOST_WIDE_INT min, max;
-      if (get_int_range (arg, dir.info->callstmt, &min, &max, false, 0,
+      if (get_int_range (arg, dir.info->callstmt, &min, &max, false, -1,
                         ptr_qry.rvals))
        {
          if (min == 0 && max == 0)
@@ -2188,7 +2190,7 @@ format_character (const directive &dir, tree arg, 
pointer_query &ptr_qry)
              res.range.max = 1;
              res.range.likely = res.range.unlikely = 1;
            }
-         else if (min >= 0 && min < 128)
+         else if (min >= 0 && max < 128)
            {
              /* Be conservative if the target execution character set
                 is not a 1-to-1 mapping to the source character set or
diff --git a/gcc/testsuite/gcc.dg/torture/pr128269.c 
b/gcc/testsuite/gcc.dg/torture/pr128269.c
new file mode 100644
index 000000000000..ac7c60bc6c2f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128269.c
@@ -0,0 +1,38 @@
+/* PR tree-optimization/128269 */
+/* { dg-do run } */
+/* { dg-require-effective-target wchar } */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <wchar.h>
+
+volatile int v = 128;
+volatile int sink, neg;
+
+/* The conversion may fail for a wide character the locale cannot encode,
+   the failure must remain visible to the caller.  */
+static inline __attribute__((always_inline)) void
+check (int r)
+{
+  sink = r;
+  neg = r < 0;
+  if (neg != (sink < 0))
+    abort ();
+}
+
+int
+main (void)
+{
+  char b[32];
+  int x = v;
+  if (x >= 0 && x <= 200)
+    check (snprintf (b, sizeof b, "%lc", (wint_t) x));
+  /* Ranges with negative values convert to large wide characters.  */
+  v = -1;
+  x = v;
+  if (x >= -5 && x <= 100)
+    check (snprintf (b, sizeof b, "%lc", x));
+  if (x >= -5 && x <= -1)
+    check (snprintf (b, sizeof b, "%lc", x));
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr128269.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr128269.c
new file mode 100644
index 000000000000..78ffa226c566
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr128269.c
@@ -0,0 +1,46 @@
+/* PR tree-optimization/128269 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-require-effective-target wchar } */
+
+int
+foo (__WINT_TYPE__ x, char *b)
+{
+  if (x > 200)
+    return 0;
+  int r = __builtin_snprintf (b, 32, "%lc", x);
+  return r < 0;
+}
+
+int
+bar (__WINT_TYPE__ x, char *b)
+{
+  if (x > 127)
+    return 0;
+  int r = __builtin_snprintf (b, 32, "%lc", x);
+  return r < 0;
+}
+
+int
+baz (int x, char *b)
+{
+  if (x < -5 || x > 100)
+    return 0;
+  int r = __builtin_snprintf (b, 32, "%lc", x);
+  return r < 0;
+}
+
+int
+qux (int x, char *b)
+{
+  if (x < -5 || x > -1)
+    return 0;
+  int r = __builtin_snprintf (b, 32, "%lc", x);
+  return r < 0;
+}
+
+/* The conversion of the ranges [0, 200], [-5, 100] and [-5, -1] can fail,
+   so foo, baz and qux keep the test of the result.  The one of [0, 127]
+   cannot with an ASCII execution character set, so bar does not use the
+   result.  */
+/* { dg-final { scan-tree-dump-times "= __builtin_snprintf" 3 "optimized" } } 
*/

Reply via email to