Bug 41382 - Memory corruption observed in executable generated from struct array with compiler option -O
Summary: Memory corruption observed in executable generated from struct array with com...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.2.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-09-16 20:43 UTC by Xiaochu Qi
Modified: 2009-09-16 21:20 UTC (History)
2 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: 4.3.3 4.4.1 4.5.0
Known to fail: 4.2.4
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Xiaochu Qi 2009-09-16 20:43:53 UTC
My source file contains some manipulation on struct arrays. After the initialization of some local data, there is a call to a dummy function which has its actual argument being the value of a field of one element of such an array. 

In running the executable compiled with -O, memory corruption can be observed. Note that if the call to the dummy function is removed, the generated code runs correctly.

Here is the example:

% cat tst.c
#include <stdio.h>

typedef struct {
      int re, im;
} cint32_T;

static void m_foo(int i)
{}

int main()
{
    static cint32_T sdata[3] = { { 504094, -1909779 }, { 658988, -552759 }, { -15591, -896799 } };

    cint32_T ldata[3];
    cint32_T odata[3];
    int i;

    for(i = 0; i < 3; i++) {// assign ldata from sdata
        ldata[i] = sdata[i];
    }

    for(i = 0; i < 3; i++) {// assign odata from ldata
        odata[i] = ldata[i];
    }

    m_foo(ldata[2].re);    // make a dummy call

    // Now data stored in odata[0] is corrupted.
    printf("%d, %d\n", odata[0].re, odata[0].im);

    return 0;
}


% gcc -Wall tst.c -o correct
% correct
504094, -1909779
% gcc -Wall tst.c -O -o wrong
% wrong
0, 0
%
Comment 1 Richard Biener 2009-09-16 21:19:53 UTC
Works on all active release branches.
Comment 2 Richard Biener 2009-09-16 21:20:44 UTC
Confirmed with 4.2.4 btw.