Bug 37238 - gcc miscompiles simple memcpy loop
Summary: gcc miscompiles simple memcpy loop
Status: RESOLVED DUPLICATE of bug 37090
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.3.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2008-08-25 22:06 UTC by felix-gcc
Modified: 2008-08-25 22:10 UTC (History)
3 users (show)

See Also:
Host: x86_64-unknown-linux-gnu
Target: x86_64-unknown-linux-gnu
Build: x86_64-unknown-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description felix-gcc 2008-08-25 22:06:24 UTC
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() {
  strcpy(text,"this is a test!\n");
  assert(!memcmp(buf,"this is a test!\n\0",18));
  return 0;
Comment 1 Andrew Pinski 2008-08-25 22:10:21 UTC

*** This bug has been marked as a duplicate of 37090 ***