GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
c++

Fix double-counting offsets in folded string builtins.

Corrects an issue where offsets were double-counted in folded string builtins like memchr.

A bug in cxx_eval_builtin_function_call caused offsets to be double-counted when folding string builtins like memchr with pointer arguments that already had an offset. This commit ensures the offset is applied correctly, fixing a regression that affected C++26 features and standard library implementations.

In Details

The cxx_eval_builtin_function_call in GCC's C++ frontend incorrectly re-bases folded POINTER_PLUS_EXPRs even when the original argument already carried an offset. This commit fixes it by clearing STRRET when the evaluated argument is not a STRING_CST, preventing double-counting of offsets. This resolves a long-standing bug, exacerbated by C++26's P2738, affecting constexpr evaluations of functions like memchr and std::string_view::find.

For Context
STRING_CST
Represents a string constant in GCC's internal representation (GIMPLE/TREE). It's a node that holds a literal string value.
POINTER_PLUS_EXPR
An expression in GCC's intermediate representation that represents pointer arithmetic, specifically adding an offset to a pointer.
folding
A compiler optimization technique where constant expressions are evaluated at compile time rather than runtime. This includes evaluating built-in function calls with constant arguments.
constexpr
A C++ keyword that indicates a variable or function can be evaluated at compile time. This requires that all operations within them can also be evaluated at compile time.
Filed Under: c++constexproptimizationbugfix