Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22034

Why doesn't this generate out-of-bounds access in C?

$
0
0

Playing around with some C code to do some comparisons between C99 and Rust, I've written the following,

typedef enum {
    NICKLE, DIME, QUARTER, DOLLAR,
} Denom;

static const int cents[] = {
    [NICKLE] = 5,
    [DIME] = 10,
    [QUARTER] = 25,
    [DOLLAR] = 100,
};

int main () {
    printf( "Result %d\n", cents[DIME] );
}

This works as expected, now I wanted to show an out of bounds access, and when I do

typedef enum {
    NICKLE, DIME, QUARTER, DOLLAR, ZOD = 20394
} Denom;

I was expecting

printf( "Result %d\n", cents[ZOD] );

To show access that was cents + ZOD. But it doesn't.

 mov eax, 0
 mov esi, eax
 lea rdi, str.Result__d        ; 0x5570bf1ed020 ; "Result %d\n"
 mov eax, 0
 call sym.imp.printf           ; int printf(const char *format)

Why is it setting esi to 0, and why does this print

Result 0

There are also no errors with -Wall -Wextra -Wpedantic. Does GCC provide a flag governing this behavior? Does it know this to be out-of-bounds, and if so wouldn't it warn (or can it be made to warn) as well?


Viewing all articles
Browse latest Browse all 22034

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>