https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123256

            Bug ID: 123256
           Summary: Possible false positive in -Wuninitialized warning
                    with pointer type conversion and bit-field access
           Product: gcc
           Version: 14.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jiangxuezhi2 at huawei dot com
  Target Milestone: ---

I've encountered what appears to be a false positive with the -Wuninitialized
warning.

$ gcc test.c -O2 -Wextra -S -o test.s

#include <stdint.h>

#define MAX_ID 10

typedef struct {
    uint32_t id : 6;
} Header;

int checkHeader(uint32_t a, uint32_t b)
{
    uint32_t buf[2];
    Header *h;

    buf[0] = a;
    buf[1] = b;
    h = (Header *)(void *)buf;
    volatile uint32_t result = h->id;

    return result > MAX_ID ? 0 : 1;
}

the output:
test.c: In function 'checkHeader':
test.c:39:33: warning: 'buf' is used uninitialized [-Wuninitialized]
   39 |     volatile uint32_t result = h->id;
      |                                ~^~~~
test.c:33:14: note: 'buf' declared here
   33 |     uint32_t buf[2];
      |              ^~~

Both elements of the array data are explicitly initialized, and the bit-field
id accesses only the initialized low 6 bits of data[0]. Given this, I think the
-Wuninitialized warning may be unnecessary for this case.

This seems to be related to the recent enhancements to uninitialized variable
detection (specifically the patch addressing PR middle-end/10138 and PR
middle-end/95136), which I understand were meant to improve detection of
legitimate bugs.

If this is indeed expected behavior, I'd appreciate understanding the
reasoning,Thanks.
  • [Bug c/123256] New: Possible f... jiangxuezhi2 at huawei dot com via Gcc-bugs

Reply via email to