https://gcc.gnu.org/g:75bbfaeb8ba0ad95e7c0fb306790ca71dbb55bbd
commit r17-5877-g75bbfaeb8ba0ad95e7c0fb306790ca71dbb55bbd Author: Martin Jambor <[email protected]> Date: Tue Nov 3 10:41:34 2026 +0100 ipa-sra: Check that index of a formal parameter fits unsigned char (PR128106) PR 128106 revealed that only two of the three places that need to check that the index of a formal parameter (among the parameters of the the caller function) is small enough to fit unsigned char, one did not do so. The indices are stored as unsigned char because the array that holds as many as 7 of them is stored per each argument of each call and so we want to keep it small. The fix is to add the missing check. gcc/ChangeLog: 2026-10-26 Martin Jambor <[email protected]> PR ipa/128106 * ipa-sra.cc (scan_expr_access): Add a check that the formal parameter being passed in a call has index fitting an unsigned char. gcc/testsuite/ChangeLog: 2026-10-26 Martin Jambor <[email protected]> PR ipa/128106 * g++.dg/ipa/pr128106.C: New test. Diff: --- gcc/ipa-sra.cc | 7 +++++++ gcc/testsuite/g++.dg/ipa/pr128106.C | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/gcc/ipa-sra.cc b/gcc/ipa-sra.cc index 776c4675397d..3aef7f1f92bf 100644 --- a/gcc/ipa-sra.cc +++ b/gcc/ipa-sra.cc @@ -1974,6 +1974,13 @@ scan_expr_access (tree expr, gimple *stmt, isra_scan_context ctx, { gcc_checking_assert (call_info); + if (desc->param_number > UCHAR_MAX) + { + disqualify_split_candidate (desc, "Caller parameter with a large " + "index passed in a call."); + return; + } + if (!deref) record_nonregister_call_use (desc, call_info, offset / BITS_PER_UNIT, size / BITS_PER_UNIT); diff --git a/gcc/testsuite/g++.dg/ipa/pr128106.C b/gcc/testsuite/g++.dg/ipa/pr128106.C new file mode 100644 index 000000000000..82ddf3bfe550 --- /dev/null +++ b/gcc/testsuite/g++.dg/ipa/pr128106.C @@ -0,0 +1,19 @@ +/* { dg-do compile { target c++17 } } */ +/* { dg-options "-O2" } */ + +#include <array> +#include <string_view> +#include <tuple> + +struct F { std::string_view name = "x"; }; +constexpr std::array<F, 256> fields{}; + +[[gnu::noinline]] void emit(std::string_view name) { + if (name != "x") __builtin_puts("miscompiled"); +} + +int main() { + long value{}; + std::apply([&](auto... f) { ((emit(f.name), value += value), ...); }, fields); + return value; +}
