tertiary operator bug?

Dave Klint davek@ca.mgisoft.com
Fri May 26 16:46:00 GMT 2000


Here's the source to demonstrate a funky bug with egcs-2.91.66.  I 
realize that using the statement x=(x<<=1) is dumb to begin with,
but it shouldn't break the compiler.  The mal-effect is that the
array (which is supposed to be read only) is overwritten.

I don't know if it's kosher to include source code in posts, 
but I hope so.  I'm sorry if it's not.

% uname -a
Linux alfonse 2.2.14 #3 Wed May 17 16:33:44 PDT 2000 i686 unknown
% gcc --version
egcs-2.91.66
% cat bug.c

#include <stdio.h>

int quantizerScaleForScaleType1Table[64]={
    0, 1, 2, 3, 4, 5, 6, 7,
    8, 9,10,11,12,13,14,15,
   16,17,18,19,20,21,22,23,
   24,25,26,27,28,29,30,31,
   32,33,34,35,36,37,38,39,
   40,41,42,43,44,45,46,47,
   48,49,50,51,52,53,54,55,
   56,57,58,59,60,61,62,63};

typedef struct S{
    int q_scale_type;
} Stype;

typedef struct VLD{
    int quantizer_scale;
} VLDtype;

typedef struct VD{
    Stype s;
    VLDtype vld;
} VDtype;

VDtype vd;

#define BUG 1

void tryme(){
#if BUG
    vd.vld.quantizer_scale=(vd.s.q_scale_type)?
        quantizerScaleForScaleType1Table[vd.vld.quantizer_scale]:
        vd.vld.quantizer_scale<<=1;
#else
    if(vd.s.q_scale_type){
        vd.vld.quantizer_scale=
            quantizerScaleForScaleType1Table[vd.vld.quantizer_scale];
    }else{
        vd.vld.quantizer_scale=(vd.vld.quantizer_scale<<=1);
    }
#endif
}

int main(){
    int i;
    vd.s.q_scale_type=1;
    vd.vld.quantizer_scale=2;
    tryme();

    for(i=0;i<64;i++){
        if(quantizerScaleForScaleType1Table[i]!=i){
            printf("ERROR: quantizerScaleForScaleType1Table[%d]=%d\n",
                    i,quantizerScaleForScaleType1Table[i]);
        }
    }
}

% ./a.out
ERROR: quantizerScaleForScaleType1Table[2]=4


voila.  If you change the #define BUG to 0, it doesn't come up
with the error.

-dave klint


More information about the Gcc-bugs mailing list