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

            Bug ID: 104367
           Summary: Possible improvements for -Wmisleading-indentation
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: frantisek at sumsal dot cz
  Target Milestone: ---

Hello!

Recently we encountered a pretty nasty bug in systemd[0], which makes me wonder
if this situation couldn't be prevented by throwing a compiler warning.

# cat /etc/redhat-release 
Fedora release 36 (Rawhide)
# gcc --version
gcc (GCC) 12.0.1 20220125 (Red Hat 12.0.1-0)

Reproducer:
#include <stdio.h>

void bar(int *x) {
    printf("%d\n", *x);
}

void foo(int *x) {
    if (!x)
        return

    bar(x);
}

int main(void) {
    foo(NULL);

    return 0;
}


The culprit here is, obviously, the missing semicolon after the return
statement, which currently leads to a segmentation fault:

# gcc -Wall -Wextra -Wmisleading-indentation -o main main.c
# ./main 
Segmentation fault (core dumped)

That is, however, silently accepted, since the resulting expression (return
bar(x);) is still valid in this context.

I wonder if this couldn't/shouldn't be detected by -Wmisleading-indentation
(since this definitely falls into that category).

Possibly related to other similar bugs like:
 - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66298
 - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70968
 - and others

[0] https://github.com/systemd/systemd/pull/22387/files

Reply via email to