public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/98871] New: Cannot silence -Wmaybe-uninitialized at declaration site
@ 2021-01-28 21:44 1zeeky at gmail dot com
  2021-01-28 21:46 ` [Bug c/98871] " 1zeeky at gmail dot com
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: 1zeeky at gmail dot com @ 2021-01-28 21:44 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98871
           Summary: Cannot silence -Wmaybe-uninitialized at declaration
                    site
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 1zeeky at gmail dot com
  Target Milestone: ---

Created attachment 50077
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50077&action=edit
testcase

I have some code where I'm relying on the caller to uphold some guarantee in
order to properly initialize a variable.
gcc correctly detects that the variable maybe left uninitialized. The warning
is correct, but I am unable to properly silence this warning because the
warning doesn't "show up" at the location where the variable is declared, but
where it is used, which is another function, which receives it via pointer.

This is probably better explained with the testcase. The setup is a bit weird
and looks like it could be minimized. I needed to increase the complexity to
thwart static analysis sufficiently such that gcc is unsure if 'max' gets
initialized or not.
Here's the warning I get:

$ gcc -O -Wmaybe-uninitialized maybe-uninit.c
maybe-uninit.c: In function ‘main’:
maybe-uninit.c:8:9: warning: ‘max.x’ may be used uninitialized in this function
[-Wmaybe-uninitialized]
    8 |  return ++*x;
      |         ^~~~
maybe-uninit.c:15:13: note: ‘max.x’ was declared here
   15 |  struct Foo max;
      |             ^~~

A site-note: the first line in the error-report states "In function ‘main’" for
some reason.

I would like to silence the warning from within foo() (my failed attempt at
using '#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"' is left in the
testcase), since inc() is used in other places where having this warning may be
beneficial.
The warning would probably have to change to something like

maybe-uninit.c: In function ‘foo’:
maybe-uninit.c:22:13: warning: 'max.x' is passed to function 'inc', where it
may be used uninitialized [-Wmaybe-pass-uninitialized]
   22 |  return inc(&max.x);
      |             ^~~~~~
maybe-uninit.c:8:9: note: ‘max.x’ used here
    8 |  return ++*x;
      |         ^~~~
maybe-uninit.c:15:13: note: ‘max.x’ was declared here
   15 |  struct Foo max;

Alternatively, a variable attribute would work for me as well, e.g.
'__attribute__((assume_initialized))'.

Locally I use gcc 10.2.0, but through godbolt.org I also tested 10.2.1 and "gcc
(trunk)" which describes itself as "gcc (Compiler-Explorer-Build) 11.0.0
20210127 (experimental)".

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

* [Bug c/98871] Cannot silence -Wmaybe-uninitialized at declaration site
  2021-01-28 21:44 [Bug c/98871] New: Cannot silence -Wmaybe-uninitialized at declaration site 1zeeky at gmail dot com
@ 2021-01-28 21:46 ` 1zeeky at gmail dot com
  2021-01-29  0:49 ` [Bug middle-end/98871] " msebor at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: 1zeeky at gmail dot com @ 2021-01-28 21:46 UTC (permalink / raw)
  To: gcc-bugs

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

1zeeky at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  Attachment #50077|0                           |1
        is obsolete|                            |

--- Comment #1 from 1zeeky at gmail dot com ---
Created attachment 50078
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50078&action=edit
actual testcast

I played around with the code before posting the patch. This is the actual
version I meant to send.

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

* [Bug middle-end/98871] Cannot silence -Wmaybe-uninitialized at declaration site
  2021-01-28 21:44 [Bug c/98871] New: Cannot silence -Wmaybe-uninitialized at declaration site 1zeeky at gmail dot com
  2021-01-28 21:46 ` [Bug c/98871] " 1zeeky at gmail dot com
@ 2021-01-29  0:49 ` msebor at gcc dot gnu.org
  2021-01-29  0:59 ` msebor at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-01-29  0:49 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c                           |middle-end
   Last reconfirmed|                            |2021-01-29
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=98465,
                   |                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=98512
     Ever confirmed|0                           |1
           Keywords|                            |diagnostic
                 CC|                            |msebor at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW
             Blocks|                            |24639

--- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> ---
Confirmed.  The root cause of the problem is that the #pragma diagnostic
suppression machinery consider only one location but that location may be
different depending on the warning and depending on inlining.  For some
warnings it's the location of the call site into which the problem statement
has been inlined.  For others it might be the original location of the
statement itself.  -Wuninitialized and -Wmaybe-uninitialized happen to fall
into the first category.

The solution I posted for pr98465 and pr98512 lets it consider both (or all
locations along the inlining stack) and, with an minor extension, makes the
suppression work in this case as well.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=24639
[Bug 24639] [meta-bug] bug to track all Wuninitialized issues

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

* [Bug middle-end/98871] Cannot silence -Wmaybe-uninitialized at declaration site
  2021-01-28 21:44 [Bug c/98871] New: Cannot silence -Wmaybe-uninitialized at declaration site 1zeeky at gmail dot com
  2021-01-28 21:46 ` [Bug c/98871] " 1zeeky at gmail dot com
  2021-01-29  0:49 ` [Bug middle-end/98871] " msebor at gcc dot gnu.org
@ 2021-01-29  0:59 ` msebor at gcc dot gnu.org
  2021-01-29 21:56 ` msebor at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-01-29  0:59 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |msebor at gcc dot gnu.org

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
Until this is fixed, moving the suppression pragma above or around the
definition of the inc() function should work.

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

* [Bug middle-end/98871] Cannot silence -Wmaybe-uninitialized at declaration site
  2021-01-28 21:44 [Bug c/98871] New: Cannot silence -Wmaybe-uninitialized at declaration site 1zeeky at gmail dot com
                   ` (2 preceding siblings ...)
  2021-01-29  0:59 ` msebor at gcc dot gnu.org
@ 2021-01-29 21:56 ` msebor at gcc dot gnu.org
  2021-01-29 21:56 ` msebor at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-01-29 21:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Martin Sebor <msebor at gcc dot gnu.org> ---
Created attachment 50092
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50092&action=edit
Patch for GCC 12.

The attached patch applied on top the one for pr98465
(https://gcc.gnu.org/pipermail/gcc-patches/2021-January/563862.html) resolves
this bug.  Since it's not a regression it's out of scope for GCC 11 but if the
prerequisite patch is accepted I'll submit it for GCC 12.

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

* [Bug middle-end/98871] Cannot silence -Wmaybe-uninitialized at declaration site
  2021-01-28 21:44 [Bug c/98871] New: Cannot silence -Wmaybe-uninitialized at declaration site 1zeeky at gmail dot com
                   ` (3 preceding siblings ...)
  2021-01-29 21:56 ` msebor at gcc dot gnu.org
@ 2021-01-29 21:56 ` msebor at gcc dot gnu.org
  2021-06-10 23:31 ` msebor at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-01-29 21:56 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |12.0
           Keywords|                            |patch
      Known to fail|                            |10.2.0, 11.0, 9.3.0

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

* [Bug middle-end/98871] Cannot silence -Wmaybe-uninitialized at declaration site
  2021-01-28 21:44 [Bug c/98871] New: Cannot silence -Wmaybe-uninitialized at declaration site 1zeeky at gmail dot com
                   ` (4 preceding siblings ...)
  2021-01-29 21:56 ` msebor at gcc dot gnu.org
@ 2021-06-10 23:31 ` msebor at gcc dot gnu.org
  2021-07-02 22:20 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-06-10 23:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
Latest patch posted for review:
https://gcc.gnu.org/pipermail/gcc-patches/2021-June/572515.html

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

* [Bug middle-end/98871] Cannot silence -Wmaybe-uninitialized at declaration site
  2021-01-28 21:44 [Bug c/98871] New: Cannot silence -Wmaybe-uninitialized at declaration site 1zeeky at gmail dot com
                   ` (5 preceding siblings ...)
  2021-06-10 23:31 ` msebor at gcc dot gnu.org
@ 2021-07-02 22:20 ` cvs-commit at gcc dot gnu.org
  2021-07-06 19:43 ` cvs-commit at gcc dot gnu.org
  2021-11-12 19:44 ` msebor at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-07-02 22:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>:

https://gcc.gnu.org/g:6feb628a706e86eb3f303aff388c74bdb29e7381

commit r12-1992-g6feb628a706e86eb3f303aff388c74bdb29e7381
Author: Martin Sebor <msebor@redhat.com>
Date:   Fri Jul 2 16:16:31 2021 -0600

    Improve warning suppression for inlined functions [PR98512].

    Resolves:
    PR middle-end/98871 - Cannot silence -Wmaybe-uninitialized at declaration
site
    PR middle-end/98512 - #pragma GCC diagnostic ignored ineffective in
conjunction with alias attribute

    gcc/ChangeLog:

            PR middle-end/98871
            PR middle-end/98512
            * diagnostic.c (get_any_inlining_info): New.
            (update_effective_level_from_pragmas): Handle inlining context.
            (diagnostic_enabled): Same.
            (diagnostic_report_diagnostic): Same.
            * diagnostic.h (struct diagnostic_info): Add ctor.
            (struct diagnostic_context): Add new member.
            * tree-diagnostic.c (set_inlining_locations): New.
            (tree_diagnostics_defaults): Set new callback pointer.

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

* [Bug middle-end/98871] Cannot silence -Wmaybe-uninitialized at declaration site
  2021-01-28 21:44 [Bug c/98871] New: Cannot silence -Wmaybe-uninitialized at declaration site 1zeeky at gmail dot com
                   ` (6 preceding siblings ...)
  2021-07-02 22:20 ` cvs-commit at gcc dot gnu.org
@ 2021-07-06 19:43 ` cvs-commit at gcc dot gnu.org
  2021-11-12 19:44 ` msebor at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-07-06 19:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>:

https://gcc.gnu.org/g:6d3bab5d5adb3e28ddb16c97b0831efdea23cf7d

commit r12-2087-g6d3bab5d5adb3e28ddb16c97b0831efdea23cf7d
Author: Martin Sebor <msebor@redhat.com>
Date:   Tue Jul 6 13:41:02 2021 -0600

    Improve warning suppression for inlined functions.

    Resolves:
    PR middle-end/98871 - Cannot silence -Wmaybe-uninitialized at declaration
site
    PR middle-end/98512 - #pragma GCC diagnostic ignored ineffective in
conjunction with alias attribute

    gcc/ChangeLog:

            * builtins.c (warn_string_no_nul): Remove %G.
            (maybe_warn_for_bound): Same.
            (warn_for_access): Same.
            (check_access): Same.
            (check_strncat_sizes): Same.
            (expand_builtin_strncat): Same.
            (expand_builtin_strncmp): Same.
            (expand_builtin): Same.
            (expand_builtin_object_size): Same.
            (warn_dealloc_offset): Same.
            (maybe_emit_free_warning): Same.
            * calls.c (maybe_warn_alloc_args_overflow): Same.
            (maybe_warn_nonstring_arg): Same.
            (maybe_warn_rdwr_sizes): Same.
            * expr.c (expand_expr_real_1): Remove %K.
            * gimple-fold.c (gimple_fold_builtin_strncpy): Remove %G.
            (gimple_fold_builtin_strncat): Same.
            * gimple-ssa-sprintf.c (format_directive): Same.
            (handle_printf_call): Same.
            * gimple-ssa-warn-alloca.c (pass_walloca::execute): Same.
            * gimple-ssa-warn-restrict.c (maybe_diag_overlap): Same.
            (maybe_diag_access_bounds): Same.  Call gimple_location.
            (check_bounds_or_overlap): Same.
            * trans-mem.c (ipa_tm_scan_irr_block): Remove %K.  Simplify.
            * tree-ssa-ccp.c (pass_post_ipa_warn::execute): Remove %G.
            * tree-ssa-strlen.c (maybe_warn_overflow): Same.
            (maybe_diag_stxncpy_trunc): Same.
            (handle_builtin_stxncpy_strncat): Same.
            (maybe_warn_pointless_strcmp): Same.
            * tree-ssa-uninit.c (maybe_warn_operand): Same.

    gcc/testsuite/ChangeLog:

            * gcc.dg/Wobjsize-1.c: Prune expected output.
            * gcc.dg/Warray-bounds-71.c: New test.
            * gcc.dg/Warray-bounds-71.h: New test header.
            * gcc.dg/Warray-bounds-72.c: New test.
            * gcc.dg/Warray-bounds-73.c: New test.
            * gcc.dg/Warray-bounds-74.c: New test.
            * gcc.dg/Warray-bounds-75.c: New test.
            * gcc.dg/Wfree-nonheap-object-4.c: Adjust expected output.
            * gcc.dg/Wfree-nonheap-object-5.c: New test.
            * gcc.dg/Wfree-nonheap-object-6.c: New test.
            * gcc.dg/pragma-diag-10.c: New test.
            * gcc.dg/pragma-diag-9.c: New test.
            * gcc.dg/uninit-suppress_3.c: New test.
            * gcc.dg/pr79214.c: Xfail tests.
            * gcc.dg/tree-ssa/builtin-sprintf-warn-27.c: New test.
            * gcc.dg/format/c90-printf-1.c: Adjust expected output.

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

* [Bug middle-end/98871] Cannot silence -Wmaybe-uninitialized at declaration site
  2021-01-28 21:44 [Bug c/98871] New: Cannot silence -Wmaybe-uninitialized at declaration site 1zeeky at gmail dot com
                   ` (7 preceding siblings ...)
  2021-07-06 19:43 ` cvs-commit at gcc dot gnu.org
@ 2021-11-12 19:44 ` msebor at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-11-12 19:44 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED
      Known to work|                            |12.0

--- Comment #8 from Martin Sebor <msebor at gcc dot gnu.org> ---
Fixed in GCC 12.  The patch will not be backported to current releases.

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

end of thread, other threads:[~2021-11-12 19:44 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-28 21:44 [Bug c/98871] New: Cannot silence -Wmaybe-uninitialized at declaration site 1zeeky at gmail dot com
2021-01-28 21:46 ` [Bug c/98871] " 1zeeky at gmail dot com
2021-01-29  0:49 ` [Bug middle-end/98871] " msebor at gcc dot gnu.org
2021-01-29  0:59 ` msebor at gcc dot gnu.org
2021-01-29 21:56 ` msebor at gcc dot gnu.org
2021-01-29 21:56 ` msebor at gcc dot gnu.org
2021-06-10 23:31 ` msebor at gcc dot gnu.org
2021-07-02 22:20 ` cvs-commit at gcc dot gnu.org
2021-07-06 19:43 ` cvs-commit at gcc dot gnu.org
2021-11-12 19:44 ` msebor 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).