On x86_64, gcc 4.5.0, this code generates bad assembly: --- C code --- typedef unsigned long size_t; void *memcpy(void *dest, const void *src, size_t n);
void buggy_init(void *ptr, size_t size) { const char *str = "Hello world!"; memcpy(ptr, &str, size); } -------------- Compiled with gcc -O -foptimize-sibling-calls, the generated assembly looks like this: -------------- buggy_init: movq %rsi, %rdx movq $.LC0, -16(%rsp) leaq -16(%rsp), %rsi jmp memcpy -------------- which passes rsp-16 as memcpy's second argument. memcpy overwrites this part of the stack, and copies the wrong value, which causes a crash later. -- Summary: Wrong code with -foptimize-sibling-calls and memcpy on x86_64 Product: gcc Version: 4.5.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: tavianator at gmail dot com GCC build triplet: x86_64-unknown-linux-gnu GCC host triplet: x86_64-unknown-linux-gnu GCC target triplet: x86_64-unknown-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43904