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

            Bug ID: 101042
           Summary: Bogus -Wstringop-overread with 11.1.0 and -O1 because
                    of a call to printf _after_ assertions
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ypsah+6a11ea7d-369c-4db1-b3f4-60ced987a559 at devyard dot 
org
  Target Milestone: ---

Created attachment 50989
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50989&action=edit
reproducer.c

Hi,

It looks like printf-ing a pointer after running assertions on it throws gcc 11
off:

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

enum field_type {
    INTEGER,
    STRING,
};

struct field {
    enum field_type type;

    union {
        int integer;
        const char *string;
    };
};

int
main()
{
    const struct field FIELD = {
        .type = INTEGER,
        .integer = 1,
    };
    struct field *field = calloc(1, sizeof(struct field));
    assert(field);

    assert(field->type == FIELD.type);
    printf("field = %p\n", field);
    switch (field->type) {
    case STRING:
        assert(strcmp(field->string, FIELD.string) == 0);
        break;
    default:
        break;
    }

    free(field);
    return 0;
}

$ gcc -Werror -O0 reproducer.c # <== Runs fine
$ gcc -Werror -O1 reproducer.c
In file included from reproducer.c:1:
reproducer.c: In function ‘main’:
reproducer.c:34:16: error: ‘strcmp’ reading 1 or more bytes from a region of
size 0 [-Werror=stringop-overread]
   34 |         assert(strcmp(field->string, FIELD.string) == 0);
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

Any optimization level above 0 yields the warning.

If you remove the printf() statement, or place it before one of the two
assert(), the warning goes away.
Downgrading to 10.2.0 also fixes the issue.
  • ... ypsah+6a11ea7d-369c-4db1-b3f4-60ced987a559 at devyard dot org via Gcc-bugs
    • ... pinskia at gcc dot gnu.org via Gcc-bugs
    • ... msebor at gcc dot gnu.org via Gcc-bugs

Reply via email to