https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108448
Bug ID: 108448 Summary: GCC Elides Assignment to Pointer and memcpy Product: gcc Version: 11.3.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: gavin at yzena dot com Target Milestone: --- Host: x86_64 Target: x86_64 I have a stack allocator ("stackpool") that can automatically free all data allocated after entry to a function. To do this, I pass in a pointer to the function name to be the marker for the function start. To debug, I also check the function name on exit. This last item is segfaulting because the function name pointer is NULL. I ran it through GDB, and the assignment to the allocated pointer ([1]) is skipped entirely, but only when not compiling in plain Debug mode (CMake). I tried a memcpy() ([2]), but it is *also* skipped. I checked for undefined behavior with UBSan. It didn't catch any. I fully acknowledge that there could be some, though. To reproduce, run the following: ``` $ git clone https://git.yzena.com/Yzena/Yc.git $ cd Yc $ git config --local include.path ./.gitconfig $ git submodule update --init --recursive $ mkdir build $ cd build $ cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_C_COMPILER=<path/to/gcc> -DYC_ENABLE_STACKTRACES=ON -DYC_ENABLE_LONG_TESTS=ON -DYC_BUILD_DOCS=OFF .. $ make -j<ncores> ``` If the build fails, you have successfully reproduced the problem because there is a program that uses that code used as part of the build (to generate a test file). The program is `tests/strgen` (produced from `tests/strgen.c`), and the command-line it runs is something like: ``` $ tests/container/../strgen tests/container/kjvbible.txt tests/container/kjvbible.c kjvbible_array 1 ``` To pre-emptively answer some questions: the function name pointer that is passed in to `y_stackpool_enterFunc()` (the one with the elided assignment) is not NULL, and neither is the stackpool. But again, I wouldn't be surprised if there's some UB somewhere that is giving GCC the "right" to elide this assignment. I just can't find where because I'm not as expert in C as the GCC authors. [1]: https://git.yzena.com/Yzena/Yc/src/commit/6afdc86bd2c17f98b2f9e97e79e37fdf8c6b7708/src/alloc/stackpool.c#L441 [2]: https://git.yzena.com/Yzena/Yc/src/commit/c9a855a0b6d9c5758e4d605977bdf571830132a2/src/alloc/stackpool.c#L442-L443