https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128269

--- Comment #4 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Kyrylo Tkachov <[email protected]>:

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]>

Reply via email to