This code: void byte_copy(void* out, size_t len, const void* in) { char* s=out; const char* t=in; const char* u=t+len; for (;;) { if (t==u) break; *s=*t; ++s; ++t; if (t==u) break; *s=*t; ++s; ++t; if (t==u) break; *s=*t; ++s; ++t; if (t==u) break; *s=*t; ++s; ++t; } }
gcc produces wrong code with -O1 or higher, but correct code with -O0. Here is some simple test code: #include <assert.h> #include <string.h> char buf[4096]; char text[128]; int main() { memset(buf,0,sizeof(buf)); strcpy(text,"this is a test!\n"); byte_copy(buf,16,text); assert(!memcmp(buf,"this is a test!\n\0",18)); return 0; } -- Summary: gcc miscompiles simple memcpy loop Product: gcc Version: 4.3.1 Status: UNCONFIRMED Severity: critical Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: felix-gcc at fefe dot de 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=37238