public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/104367] New: Possible improvements for -Wmisleading-indentation
@ 2022-02-03 12:20 frantisek at sumsal dot cz
  2022-02-03 12:23 ` [Bug c/104367] " marxin at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: frantisek at sumsal dot cz @ 2022-02-03 12:20 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c/104367] Possible improvements for -Wmisleading-indentation
  2022-02-03 12:20 [Bug c/104367] New: Possible improvements for -Wmisleading-indentation frantisek at sumsal dot cz
@ 2022-02-03 12:23 ` marxin at gcc dot gnu.org
  2022-02-03 21:28 ` msebor at gcc dot gnu.org
  2022-02-04  0:06 ` [Bug c++/104367] " pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-02-03 12:23 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
                 CC|                            |dmalcolm at gcc dot gnu.org,
                   |                            |marxin at gcc dot gnu.org
   Last reconfirmed|                            |2022-02-03

--- Comment #1 from Martin Liška <marxin at gcc dot gnu.org> ---
It's something quite related to -Wmisleading-indentation, which is an
indentation warning.
What do you think David?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c/104367] Possible improvements for -Wmisleading-indentation
  2022-02-03 12:20 [Bug c/104367] New: Possible improvements for -Wmisleading-indentation frantisek at sumsal dot cz
  2022-02-03 12:23 ` [Bug c/104367] " marxin at gcc dot gnu.org
@ 2022-02-03 21:28 ` msebor at gcc dot gnu.org
  2022-02-04  0:06 ` [Bug c++/104367] " pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: msebor at gcc dot gnu.org @ 2022-02-03 21:28 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> ---
Declaring bar()n with attribute nonnull triggers -Wnonnull but only at -O1:

In function ‘foo’,
    inlined from ‘main’ at pr104367.c:15:5:
pr104367.c:11:5: warning: argument 1 null where non-null expected [-Wnonnull]
   11 |     bar(x);
      |     ^~~~~~
pr104367.c: In function ‘main’:
pr104367.c:3:32: note: in a call to function ‘bar’ declared ‘nonnull’
    3 | __attribute__ ((nonnull)) void bar(int *x) {
      |                                ^~~

At -O2 GCC notices the invalid access and emits a trap but doesn't warn:

void foo (int * x)
{
  int _3;

  <bb 2> [local count: 1073741824]:
  if (x_1(D) == 0B)
    goto <bb 3>; [9.81%]
  else
    goto <bb 4>; [90.19%]

  <bb 3> [local count: 105334072]:
  _3 ={v} MEM[(int *)0B];
  __builtin_trap ();

  <bb 4> [local count: 1073741824]:
  return;

}

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/104367] Possible improvements for -Wmisleading-indentation
  2022-02-03 12:20 [Bug c/104367] New: Possible improvements for -Wmisleading-indentation frantisek at sumsal dot cz
  2022-02-03 12:23 ` [Bug c/104367] " marxin at gcc dot gnu.org
  2022-02-03 21:28 ` msebor at gcc dot gnu.org
@ 2022-02-04  0:06 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-02-04  0:06 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
          Component|c                           |c++

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
In the C case, I had thought we warned about return with a statement for a void
return type but looks like I am wrong. This would have showed the issue too.
Obviously for C++, it is not that useful due to templates and such.

The non-null warning is not going to be useful in the general case really, it
might have helped here but does not mean it will help in general.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-02-04  0:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-03 12:20 [Bug c/104367] New: Possible improvements for -Wmisleading-indentation frantisek at sumsal dot cz
2022-02-03 12:23 ` [Bug c/104367] " marxin at gcc dot gnu.org
2022-02-03 21:28 ` msebor at gcc dot gnu.org
2022-02-04  0:06 ` [Bug c++/104367] " pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).