Bug 102996

Summary: No warning on dereferencing of uninitialized pointer in an array, in a loop
Product: gcc Reporter: Eyal Rozenberg <eyalroz1>
Component: tree-optimizationAssignee: Not yet assigned to anyone <unassigned>
Status: NEW ---    
Severity: normal CC: msebor
Priority: P3 Keywords: diagnostic
Version: 11.2.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2021-10-29 00:00:00
Bug Depends on:    
Bug Blocks: 24639    

Description Eyal Rozenberg 2021-10-29 07:34:10 UTC
Consider the following two functions:

void foo() {
    int *as[2];
    *(as[0])=1;
}

void bar() {
    int i = 0;
    int *as[2];
    for(i=0;i<1;i++)
    {
        *(as[i])=i;
    }
}


When compiling these with -Wall, we get warnings about the uninitialized use of as in the first function, but not in the second one.

GodBolt: https://godbolt.org/z/Ta9fWYWs6
Inspired by this StackOverflow question: https://stackoverflow.com/q/69764896/1593077
Comment 1 Richard Biener 2021-10-29 09:49:28 UTC
The foo form is handled by the early uninit pass but the bar form is optimized away as dead before we get to do a late warning.
Comment 2 Eyal Rozenberg 2021-10-29 10:10:11 UTC
(In reply to Richard Biener from comment #1)
> The foo form is handled by the early uninit pass

Since _none_ of `as` is initialized, one could argue that an early uninit pass could catch that as well.
Comment 3 Martin Sebor 2021-10-29 15:03:46 UTC
The early uninit pass deliberately defers the conditional cases to the late pass to avoid false positives.  It only handles straightforward unconditionally uninitialized reads.  It could probably do better.