This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/37238] New: gcc miscompiles simple memcpy loop
- From: "felix-gcc at fefe dot de" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 25 Aug 2008 22:06:25 -0000
- Subject: [Bug c/37238] New: gcc miscompiles simple memcpy loop
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
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