User account creation filtered due to spam.

Bug 21404 - wrong code for array copy in while loop
Summary: wrong code for array copy in while loop
Status: RESOLVED DUPLICATE of bug 11751
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 3.4.4
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-05-05 17:10 UTC by Janis Johnson
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Janis Johnson 2005-05-05 17:10:33 UTC
This test case fails for GCC 3.3.x and GCC 3.4.x at any level of optimization
other than -O0:

extern void abort (void);
char buffer[20];
                                                                                
void foo (char *p, const char *q)
{
  int i = 0;
  while (q[i])
    p[i] = q[i++];
}
                                                                                
int
main ()
{
  foo (buffer, "howdy");
  if (buffer[0] != 'h')
    abort ();
  return 0;
}

I've tried it on powerpc64-linux and i686-linux and it fails on both.  It
doesn't fail with Red Hat's GCC 3.2.2-5 compiler (I don't have an FSF 3.2
compiler built anywhere), and doesn't fail with GCC 4.0.0 or GCC mainline.
Comment 1 Janis Johnson 2005-05-05 17:49:56 UTC
A workaround is to replace the body of the loop with:

{
  p[i] = q[i];
  i++;
}
Comment 2 Andrew Pinski 2005-05-05 18:12:12 UTC
This is invalid, there is no sequence point between the access of i and the increment of i so either can 
be first.

With -W -Wall we get a warning:
t.c:8: warning: operation on `i' may be undefined


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