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

--- Comment #7 from Zhiyang Chen <jeffchen006 at gmail dot com> ---
(In reply to Martin Uecker from comment #2)
> Mine. What patch?

Sorry, my mistake: the sentence "The patch fixes both" should not have
been in the description; there is no patch. Thanks for the fix
(r17-6074) and the gcc-16 backport (r16-9964).

The second case from the description is not fixed yet: the address of a
nested function taken while its own body is still being parsed. Test:

int printf(const char *, ...);
void call(void (*f)(int), int k) { f(k); }
int main(void) {
  int x = 0;
  void n2(int k) {
    void n3(void) { call(n2, 0); }
    if (k)
      n3();
    else
      x = 42;
  }
  call(n2, 1);
  printf("%d\n", x);
}

$ gcc -O0 t.c && ./a.out
0

Expected: 42. GCC 15.2 prints 42 at -O0, -O1 and -O2. GCC 16.2 and
trunk r17-6489 print 0 at -O0 (42 at -O1 and -O2). No warnings with
-Wall -Wextra; UBSan is clean.

Cause: when "call(n2, 0)" is parsed, n2 is not marked as needing a
non-local context yet ("x = 42" comes later in its body). So
function_to_pointer_conversion sets TREE_NO_TRAMPOLINE on &n2, and n2
runs with a wrong static chain.

If you prefer a separate PR for this case, I can open one.

Reply via email to