Bug in GCC

Harald Servat harald.servat@bsc.es
Mon May 31 16:34:00 GMT 2010


Hello,

En/na naveen yadav ha escrit:
> Hi all,
> 
> When I compiled below test program using gcc version 4.1.2 20070925 (Red Hat
> 4.1.2-33) chain it is giving segmentation fault .
> *[naveen@localhost ~]$ ./test.out
> Segmentation fault*
> 
> But the same program is compiled using -O3 flag it is not giving the
> segmentation problem.
> 
> Plz can any one help to debug this problem??
> 
> //test.c
> 
> #include<stdio.h>
> 
> unsigned long FindCompoundSound(void* pPlayer, void* data, unsigned long
> dataLen, unsigned long* formats, unsigned short numFormats,
> void** csData, unsigned long* csDataLen)
> {
> unsigned long foundFormat = 1;
> unsigned short bestMatch = 0xFFFF;
> 
> unsigned long FI_DeviceSound_CacheMask = 0xFFFF;
> unsigned char sData;
> unsigned long sFormat= 0xFF;
> unsigned long sLength;
> 
> unsigned short i;
> 
> 
> 
> for (i=0;i<numFormats;i++)
> {
> if ((formats[i] & FI_DeviceSound_CacheMask) == sFormat)
> {
> if (i < bestMatch)
> {
> bestMatch = i;
> foundFormat = formats[i];
> if (csDataLen)
> {
> *csDataLen = sLength;
> }
> }
> }
> }
> }
> 
> int main(void)
> {
> FindCompoundSound(NULL,NULL,1,
> NULL,1,NULL,NULL);
> return 0;
> 
> }
> 
> Regards,
> 
> Raju G

You're accessing formats[0] and formats == NULL. Probably -O3 does not
expose the segmentation fault because the optimizer is able to remove
that part of code as it's not doing anything useful.

If you want to expose it using -O3 simply add
  printf ("%d\n", bestMatch);
at the end of FindCompoundSound.

Regards.



More information about the Gcc-help mailing list