public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/108986] New: Incorrect warning for [static] array parameter
@ 2023-03-01 18:35 Keith.S.Thompson at gmail dot com
  2023-03-01 18:41 ` [Bug c/108986] " Keith.S.Thompson at gmail dot com
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: Keith.S.Thompson at gmail dot com @ 2023-03-01 18:35 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 108986
           Summary: Incorrect warning for [static] array parameter
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Keith.S.Thompson at gmail dot com
  Target Milestone: ---

When a parameter is declared with the (new in C99) [static] syntax, and
the argument is a null pointer, the warning incorrectly refers to the
expected size in bytes of the parameter rather than to its length.

That by itself might be annoying but not incorrect, but the warning
includes C syntax that is inconsistent with the actual declaration.

(When the argument is an array object that's too short, the warning
is different. It also refers to a size in bytes, and IMHO referring
to the length would be clearer. Details to follow in a comment.)

$ cat c.c
#include <stddef.h>

void f(int a[static 7]) { }

int main(void) {
    f(NULL);
}
$ gcc --version | head -n 1
gcc (GCC) 12.2.0
$ gcc -Wall -c c.c 
c.c: In function ‘main’:
c.c:6:5: warning: argument 1 to ‘int[static 28]’ is null where non-null
expected [-Wnonnull]
    6 |     f(NULL);
      |     ^~~~~~~
c.c:3:6: note: in a call to function ‘f’
    3 | void f(int a[static 7]) { }
      |      ^
c.c:6:5: warning: argument 1 to ‘int[static 28]’ is null where non-null
expected [-Wnonnull]
    6 |     f(NULL);
      |     ^~~~~~~
c.c:3:6: note: in a call to function ‘f’
    3 | void f(int a[static 7]) { }
      |      ^
$

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

* [Bug c/108986] Incorrect warning for [static] array parameter
  2023-03-01 18:35 [Bug c/108986] New: Incorrect warning for [static] array parameter Keith.S.Thompson at gmail dot com
@ 2023-03-01 18:41 ` Keith.S.Thompson at gmail dot com
  2023-03-02 10:11 ` rguenth at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Keith.S.Thompson at gmail dot com @ 2023-03-01 18:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Keith Thompson <Keith.S.Thompson at gmail dot com> ---
A similar case. The warning refers to the size in bytes, but unlike
the first case it's not incorrect, though referring to the length
would IMHO be clearer.

Note also that the warning appears twice. It only appears once in gcc 11.2.0.

Let me know if I should submit a new bug report for this.

$ cat c.c
#include <stddef.h>

void f(int a[static 7]) { }

int main(void) {
    f(NULL);
}
$ /o/apps/gcc-12.2.0/bin/gcc --version | head -n 1
gcc (GCC) 12.2.0
$ /o/apps/gcc-12.2.0/bin/gcc -Wall -c c.c
c.c: In function ‘main’:
c.c:6:5: warning: argument 1 to ‘int[static 28]’ is null where non-null
expected [-Wnonnull]
    6 |     f(NULL);
      |     ^~~~~~~
c.c:3:6: note: in a call to function ‘f’
    3 | void f(int a[static 7]) { }
      |      ^
c.c:6:5: warning: argument 1 to ‘int[static 28]’ is null where non-null
expected [-Wnonnull]
    6 |     f(NULL);
      |     ^~~~~~~
c.c:3:6: note: in a call to function ‘f’
    3 | void f(int a[static 7]) { }
      |      ^
$

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

* [Bug c/108986] Incorrect warning for [static] array parameter
  2023-03-01 18:35 [Bug c/108986] New: Incorrect warning for [static] array parameter Keith.S.Thompson at gmail dot com
  2023-03-01 18:41 ` [Bug c/108986] " Keith.S.Thompson at gmail dot com
@ 2023-03-02 10:11 ` rguenth at gcc dot gnu.org
  2023-03-02 15:42 ` [Bug c/108986] [11/12/13 Regression] " jakub at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-03-02 10:11 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |12.2.0, 13.0
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2023-03-02
           Keywords|                            |diagnostic

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.

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

* [Bug c/108986] [11/12/13 Regression] Incorrect warning for [static] array parameter
  2023-03-01 18:35 [Bug c/108986] New: Incorrect warning for [static] array parameter Keith.S.Thompson at gmail dot com
  2023-03-01 18:41 ` [Bug c/108986] " Keith.S.Thompson at gmail dot com
  2023-03-02 10:11 ` rguenth at gcc dot gnu.org
@ 2023-03-02 15:42 ` jakub at gcc dot gnu.org
  2023-03-02 18:09 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-03-02 15:42 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2
   Target Milestone|---                         |12.3
            Summary|Incorrect warning for       |[11/12/13 Regression]
                   |[static] array parameter    |Incorrect warning for
                   |                            |[static] array parameter
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
One warning with the bogus [static 28] appeared with -O2 -Wall in
r12-6606-g9d6a0f388eb048f8d87f
With -O0 -Wall we started warning once with correct [static 7] in
r11-3305-gbaad4c48a85a354d2bf1 and the bogus [static 28] first appeared in
r11-3540-g873f8c1e6df94a9dcbfbe69 and two warnings starting with r12-6606.

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

* [Bug c/108986] [11/12/13 Regression] Incorrect warning for [static] array parameter
  2023-03-01 18:35 [Bug c/108986] New: Incorrect warning for [static] array parameter Keith.S.Thompson at gmail dot com
                   ` (2 preceding siblings ...)
  2023-03-02 15:42 ` [Bug c/108986] [11/12/13 Regression] " jakub at gcc dot gnu.org
@ 2023-03-02 18:09 ` jakub at gcc dot gnu.org
  2023-03-03 15:12 ` cvs-commit at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-03-02 18:09 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

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

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 54571
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54571&action=edit
gcc13-pr108986.patch

Untested fix for both issues.

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

* [Bug c/108986] [11/12/13 Regression] Incorrect warning for [static] array parameter
  2023-03-01 18:35 [Bug c/108986] New: Incorrect warning for [static] array parameter Keith.S.Thompson at gmail dot com
                   ` (3 preceding siblings ...)
  2023-03-02 18:09 ` jakub at gcc dot gnu.org
@ 2023-03-03 15:12 ` cvs-commit at gcc dot gnu.org
  2023-03-03 15:19 ` [Bug c/108986] [11/12 " jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-03-03 15:12 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:1b0e3f8ca369f63d3e1a8e1c268d93530035503a

commit r13-6450-g1b0e3f8ca369f63d3e1a8e1c268d93530035503a
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Mar 3 16:11:11 2023 +0100

    waccess: Fix two -Wnonnull warning issues [PR108986]

    The following patch fixes 2 issues with the -Wnonnull warning.

    One, fixed by the second hunk, is that the warning wording is bogus
    since r11-3305, instead of printing
    warning: argument 1 to âint[static 7]â is null where non-null expected
[-Wnonnull]
    it prints
    warning: argument 1 to âint[static 28]â is null where non-null expected
[-Wnonnull]
    access_size is measured in bytes, so obviously will be correct as array
    number of elements only if it is 1 byte element array.
    In the function, access_nelts is either constant (if sizidx == -1) or
    when the array size is determined by some other parameter, I believe a
value
    passed to that argument.
    Later on we query the range of it:
          if (get_size_range (m_ptr_qry.rvals, access_nelts, stmt, sizrng, 1))
    which I bet must just return accesS_nelts in sizrng[0] and [1] if it is
    constant.  access_size is later computed as:
          tree access_size = NULL_TREE;
          if (tree_int_cst_sgn (sizrng[0]) >= 0)
            {
              if (COMPLETE_TYPE_P (argtype))
                {
    ...
                        wide_int minsize = wi::to_wide (sizrng[0], prec);
                        minsize *= wi::to_wide (argsize, prec);
                        access_size = wide_int_to_tree (sizetype, minsize);
                      }
                }
              else
                access_size = access_nelts;
            }
    and immediately after this the code does:
          if (integer_zerop (ptr))
            {
              if (sizidx >= 0 && tree_int_cst_sgn (sizrng[0]) > 0)
                {
    some other warning wording
                }
              else if (access_size && access.second.static_p)
                {
    this spot
                }
            }
    So, because argtype is complete, access_size has been multiplied by
    argsize, but in case of this exact warning ("this spot" above)
    I believe access_nelts must be really constant, otherwise
    "some other warning wording" would handle it.  So, I think access_nelts
    is exactly what we want to print there.

    The other problem is that since the introduction of -Wdangling-pointer
    in r12-6606, the pass has early and late instances and while lots of
    stuff in the pass is guarded on being done in the late pass only,
    this particular function is not, furthermore it is emitting two different
    warnings in a loop and already messes up with stuff like clearing
    warning suppression for one of the warning (ugh!).  The end effect is
    that we warn twice about the same problem, once in the early and once in
    the late pass.  Now, e.g. with -O2 -Wall we warn just once, during the
    early pass, as it is then optimized away, so I think just making this
    late warning only wouldn't be best.  This patch instead returns early
    if either of the warnings is suppressed on the call stmt already.
    I think if one of the passes warned on it already (even if say on some
other
    argument), then warning again (even on some other argument) is unnecessary,
    if both problems are visible in the same pass we'll still warn about both.

    2023-03-03  Jakub Jelinek  <jakub@redhat.com>

            PR c/108986
            * gimple-ssa-warn-access.cc
(pass_waccess::maybe_check_access_sizes):
            Return immediately if OPT_Wnonnull or OPT_Wstringop_overflow_ is
            suppressed on stmt.  For [static %E] warning, print access_nelts
            rather than access_size.  Fix up comment wording.

            * gcc.dg/Wnonnull-8.c: New test.

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

* [Bug c/108986] [11/12 Regression] Incorrect warning for [static] array parameter
  2023-03-01 18:35 [Bug c/108986] New: Incorrect warning for [static] array parameter Keith.S.Thompson at gmail dot com
                   ` (4 preceding siblings ...)
  2023-03-03 15:12 ` cvs-commit at gcc dot gnu.org
@ 2023-03-03 15:19 ` jakub at gcc dot gnu.org
  2023-03-19  5:30 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-03-03 15:19 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[11/12/13 Regression]       |[11/12 Regression]
                   |Incorrect warning for       |Incorrect warning for
                   |[static] array parameter    |[static] array parameter

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed on the trunk so far.

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

* [Bug c/108986] [11/12 Regression] Incorrect warning for [static] array parameter
  2023-03-01 18:35 [Bug c/108986] New: Incorrect warning for [static] array parameter Keith.S.Thompson at gmail dot com
                   ` (5 preceding siblings ...)
  2023-03-03 15:19 ` [Bug c/108986] [11/12 " jakub at gcc dot gnu.org
@ 2023-03-19  5:30 ` cvs-commit at gcc dot gnu.org
  2023-03-20 10:28 ` [Bug c/108986] [11 " jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-03-19  5:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-12 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:d0e59b5e849258ab00c82ccab37c5e8246a41c6a

commit r12-9283-gd0e59b5e849258ab00c82ccab37c5e8246a41c6a
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Mar 3 16:11:11 2023 +0100

    waccess: Fix two -Wnonnull warning issues [PR108986]

    The following patch fixes 2 issues with the -Wnonnull warning.

    One, fixed by the second hunk, is that the warning wording is bogus
    since r11-3305, instead of printing
    warning: argument 1 to âint[static 7]â is null where non-null expected
[-Wnonnull]
    it prints
    warning: argument 1 to âint[static 28]â is null where non-null expected
[-Wnonnull]
    access_size is measured in bytes, so obviously will be correct as array
    number of elements only if it is 1 byte element array.
    In the function, access_nelts is either constant (if sizidx == -1) or
    when the array size is determined by some other parameter, I believe a
value
    passed to that argument.
    Later on we query the range of it:
          if (get_size_range (m_ptr_qry.rvals, access_nelts, stmt, sizrng, 1))
    which I bet must just return accesS_nelts in sizrng[0] and [1] if it is
    constant.  access_size is later computed as:
          tree access_size = NULL_TREE;
          if (tree_int_cst_sgn (sizrng[0]) >= 0)
            {
              if (COMPLETE_TYPE_P (argtype))
                {
    ...
                        wide_int minsize = wi::to_wide (sizrng[0], prec);
                        minsize *= wi::to_wide (argsize, prec);
                        access_size = wide_int_to_tree (sizetype, minsize);
                      }
                }
              else
                access_size = access_nelts;
            }
    and immediately after this the code does:
          if (integer_zerop (ptr))
            {
              if (sizidx >= 0 && tree_int_cst_sgn (sizrng[0]) > 0)
                {
    some other warning wording
                }
              else if (access_size && access.second.static_p)
                {
    this spot
                }
            }
    So, because argtype is complete, access_size has been multiplied by
    argsize, but in case of this exact warning ("this spot" above)
    I believe access_nelts must be really constant, otherwise
    "some other warning wording" would handle it.  So, I think access_nelts
    is exactly what we want to print there.

    The other problem is that since the introduction of -Wdangling-pointer
    in r12-6606, the pass has early and late instances and while lots of
    stuff in the pass is guarded on being done in the late pass only,
    this particular function is not, furthermore it is emitting two different
    warnings in a loop and already messes up with stuff like clearing
    warning suppression for one of the warning (ugh!).  The end effect is
    that we warn twice about the same problem, once in the early and once in
    the late pass.  Now, e.g. with -O2 -Wall we warn just once, during the
    early pass, as it is then optimized away, so I think just making this
    late warning only wouldn't be best.  This patch instead returns early
    if either of the warnings is suppressed on the call stmt already.
    I think if one of the passes warned on it already (even if say on some
other
    argument), then warning again (even on some other argument) is unnecessary,
    if both problems are visible in the same pass we'll still warn about both.

    2023-03-03  Jakub Jelinek  <jakub@redhat.com>

            PR c/108986
            * gimple-ssa-warn-access.cc
(pass_waccess::maybe_check_access_sizes):
            Return immediately if OPT_Wnonnull or OPT_Wstringop_overflow_ is
            suppressed on stmt.  For [static %E] warning, print access_nelts
            rather than access_size.  Fix up comment wording.

            * gcc.dg/Wnonnull-8.c: New test.

    (cherry picked from commit 1b0e3f8ca369f63d3e1a8e1c268d93530035503a)

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

* [Bug c/108986] [11 Regression] Incorrect warning for [static] array parameter
  2023-03-01 18:35 [Bug c/108986] New: Incorrect warning for [static] array parameter Keith.S.Thompson at gmail dot com
                   ` (6 preceding siblings ...)
  2023-03-19  5:30 ` cvs-commit at gcc dot gnu.org
@ 2023-03-20 10:28 ` jakub at gcc dot gnu.org
  2023-05-08 12:26 ` rguenth at gcc dot gnu.org
  2023-08-04 14:06 ` muecker at gwdg dot de
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-03-20 10:28 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[11/12 Regression]          |[11 Regression] Incorrect
                   |Incorrect warning for       |warning for [static] array
                   |[static] array parameter    |parameter

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed for 12.3 too.

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

* [Bug c/108986] [11 Regression] Incorrect warning for [static] array parameter
  2023-03-01 18:35 [Bug c/108986] New: Incorrect warning for [static] array parameter Keith.S.Thompson at gmail dot com
                   ` (7 preceding siblings ...)
  2023-03-20 10:28 ` [Bug c/108986] [11 " jakub at gcc dot gnu.org
@ 2023-05-08 12:26 ` rguenth at gcc dot gnu.org
  2023-08-04 14:06 ` muecker at gwdg dot de
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-05-08 12:26 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|12.3                        |12.4

--- Comment #9 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 12.3 is being released, retargeting bugs to GCC 12.4.

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

* [Bug c/108986] [11 Regression] Incorrect warning for [static] array parameter
  2023-03-01 18:35 [Bug c/108986] New: Incorrect warning for [static] array parameter Keith.S.Thompson at gmail dot com
                   ` (8 preceding siblings ...)
  2023-05-08 12:26 ` rguenth at gcc dot gnu.org
@ 2023-08-04 14:06 ` muecker at gwdg dot de
  9 siblings, 0 replies; 11+ messages in thread
From: muecker at gwdg dot de @ 2023-08-04 14:06 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Uecker <muecker at gwdg dot de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |muecker at gwdg dot de

--- Comment #10 from Martin Uecker <muecker at gwdg dot de> ---
PATCH: https://gcc.gnu.org/pipermail/gcc-patches/2023-July/625559.html

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

end of thread, other threads:[~2023-08-04 14:06 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-01 18:35 [Bug c/108986] New: Incorrect warning for [static] array parameter Keith.S.Thompson at gmail dot com
2023-03-01 18:41 ` [Bug c/108986] " Keith.S.Thompson at gmail dot com
2023-03-02 10:11 ` rguenth at gcc dot gnu.org
2023-03-02 15:42 ` [Bug c/108986] [11/12/13 Regression] " jakub at gcc dot gnu.org
2023-03-02 18:09 ` jakub at gcc dot gnu.org
2023-03-03 15:12 ` cvs-commit at gcc dot gnu.org
2023-03-03 15:19 ` [Bug c/108986] [11/12 " jakub at gcc dot gnu.org
2023-03-19  5:30 ` cvs-commit at gcc dot gnu.org
2023-03-20 10:28 ` [Bug c/108986] [11 " jakub at gcc dot gnu.org
2023-05-08 12:26 ` rguenth at gcc dot gnu.org
2023-08-04 14:06 ` muecker at gwdg dot de

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).