https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117643
Mikael Morin <mikael at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |mikael at gcc dot gnu.org --- Comment #6 from Mikael Morin <mikael at gcc dot gnu.org> --- I've had a look. The problem is the single tree expression len to hold the result length. Each branch of the conditional uses a different expression to evaluate the result, so the result length also uses a different expression. When generating the code of the "else" branch, the following creates the length expression: > + len = fold_build2_loc (input_location, PLUS_EXPR, cnode, > + fold_convert (cnode, tse.string_length), > + fold_convert (cnode, rse.string_length)); But it actually overwrites the expression that was used in the "then" branch. So the final length used for the intrinsic's result (used by reallocation, etc): > + se->string_length = len; is unconditionally the expression of the "else" branch (using the "len" trim result variable). The generation of the conditional : > + tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, > + asis_se.expr, then_branch, else_branch); immediately optimizes away the else branch code (condition is true), so all that remains is the "then" branch and the reallocation code using the length from the "else" branch. Ooops. I think what is missing is a variable to hold the length, and an assignment in each branch to set the variable.