public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug other/107787] New: -Werror=array-bounds=X does not work as expected
@ 2022-11-21 10:08 newtonist at yandex dot ru
  2022-11-22  8:41 ` [Bug driver/107787] " rguenth at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: newtonist at yandex dot ru @ 2022-11-21 10:08 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 107787
           Summary: -Werror=array-bounds=X does not work as expected
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
          Assignee: unassigned at gcc dot gnu.org
          Reporter: newtonist at yandex dot ru
  Target Milestone: ---

Created attachment 53934
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53934&action=edit
Suggested fix patch

According to documentation the -Werror= option makes the specified warning into
an error and also automatically implies this option. Then it seems that the
behavior of the compiler when specifying -Werror=array-bounds=X should be the
same as specifying "-Werror=array-bounds -Warray-bounds=X", so we expect to
receive array-bounds pass triggers and they must be processed as errors. In
practice, we observe that the array-bounds pass is indeed called, but its
responses are processed as warnings, not errors.


$ cat testcase.c && gcc -O2 -Werror=array-bounds=1 -c testcase.c
int a[10];

int* f(void) {

    a[-1] = 0;

    return a;
}

testcase.c: In function ‘f’:
testcase.c:5:6: warning: array subscript -1 is below array bounds of ‘int[10]’
[-Warray-bounds]
    5 |     a[-1] = 0;
      |     ~^~~~
testcase.c:1:5: note: while referencing ‘a’
    1 | int a[10];
      |     ^

As I understand, this happens because Warray-bounds and Warray-bounds= are
declared as 2 different options in common.opt, so when
diagnostic_classify_diagnostic() (opts-common.cc:1880) is called, DK_ERROR is
set for the Warray-bounds= option, but in diagnostic_report_diagnostic()
(diagnostic.cc:1446) through warning_at() passes opt_index of Warray-bounds, so
information about DK_ERROR is lost. My suggestion is to use Alias in
declaration of Warray-bounds (similarly as in Wattribute-alias etc.)
I plan to send this patch to the mailing list.

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

* [Bug driver/107787] -Werror=array-bounds=X does not work as expected
  2022-11-21 10:08 [Bug other/107787] New: -Werror=array-bounds=X does not work as expected newtonist at yandex dot ru
@ 2022-11-22  8:41 ` rguenth at gcc dot gnu.org
  2022-11-30 13:31 ` cvs-commit at gcc dot gnu.org
  2022-11-30 13:32 ` amonakov at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-11-22  8:41 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-11-22
     Ever confirmed|0                           |1
          Component|other                       |driver

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
That looks good - I wonder if there's a reason it's not done already ...

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

* [Bug driver/107787] -Werror=array-bounds=X does not work as expected
  2022-11-21 10:08 [Bug other/107787] New: -Werror=array-bounds=X does not work as expected newtonist at yandex dot ru
  2022-11-22  8:41 ` [Bug driver/107787] " rguenth at gcc dot gnu.org
@ 2022-11-30 13:31 ` cvs-commit at gcc dot gnu.org
  2022-11-30 13:32 ` amonakov at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-11-30 13:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Alexander Monakov <amonakov@gcc.gnu.org>:

https://gcc.gnu.org/g:7c01d029fca669263b9c2dcb8837c42f7ae3a77f

commit r13-4410-g7c01d029fca669263b9c2dcb8837c42f7ae3a77f
Author: Iskander Shakirzyanov <iskander@ispras.ru>
Date:   Thu Nov 24 14:26:59 2022 +0000

    Make Warray-bounds alias to Warray-bounds= [PR107787]

    According to the documentation, the -Werror= option makes the specified
    warning into an error and also automatically implies that option. Then
    it seems that the behavior of the compiler when specifying
    -Werror=array-bounds=X should be the same as specifying
    "-Werror=array-bounds -Warray-bounds=X", so we expect to receive
    array-bounds pass diagnostics and they must be processed as errors.

    In practice, we observe that the array-bounds pass is indeed invoked,
    but its diagnostics are processed as warnings, not errors.

    This happens because Warray-bounds and Warray-bounds= are
    declared as two different options in common.opt, so when
    diagnostic_classify_diagnostic is called, DK_ERROR is set for
    the Warray-bounds= option, but diagnostic_report_diagnostic called from
    warning_at receives opt_index of Warray-bounds, so information about
    DK_ERROR is lost. Fix this by using Alias in declaration of
    Warray-bounds (similar to Wattribute-alias).

    Co-authored-by: Franz Sirl <Franz.Sirl-kernel@lauterbach.com>

    gcc/ChangeLog:

            PR driver/107787
            * common.opt (Warray-bounds): Turn into alias of
            -Warray-bounds=1.
            * builtins.cc (c_strlen): Use OPT_Warray_bounds_
            instead of OPT_Warray_bounds.
            * diagnostic-spec.cc (nowarn_spec_t::nowarn_spec_t): Ditto.
            * gimple-array-bounds.cc (array_bounds_checker::check_array_ref,
            array_bounds_checker::check_mem_ref,
            array_bounds_checker::check_addr_expr,
            array_bounds_checker::check_array_bounds): Ditto.
            * gimple-ssa-warn-restrict.cc (maybe_diag_access_bounds): Ditto.

    gcc/c-family/ChangeLog:

            PR driver/107787
            * c-common.cc (fold_offsetof,
            convert_vector_to_array_for_subscript): Use OPT_Warray_bounds_
            instead of OPT_Warray_bounds.

    gcc/testsuite/ChangeLog:

            PR driver/107787
            * gcc.dg/Warray-bounds-34.c: Correct the regular expression
            for -Warray-bounds=.
            * gcc.dg/Warray-bounds-43.c: Likewise.
            * gcc.dg/pr107787.c: New test.

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

* [Bug driver/107787] -Werror=array-bounds=X does not work as expected
  2022-11-21 10:08 [Bug other/107787] New: -Werror=array-bounds=X does not work as expected newtonist at yandex dot ru
  2022-11-22  8:41 ` [Bug driver/107787] " rguenth at gcc dot gnu.org
  2022-11-30 13:31 ` cvs-commit at gcc dot gnu.org
@ 2022-11-30 13:32 ` amonakov at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: amonakov at gcc dot gnu.org @ 2022-11-30 13:32 UTC (permalink / raw)
  To: gcc-bugs

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

Alexander Monakov <amonakov at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |amonakov at gcc dot gnu.org
         Resolution|---                         |FIXED

--- Comment #3 from Alexander Monakov <amonakov at gcc dot gnu.org> ---
Fixed for gcc-13.

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

end of thread, other threads:[~2022-11-30 13:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-21 10:08 [Bug other/107787] New: -Werror=array-bounds=X does not work as expected newtonist at yandex dot ru
2022-11-22  8:41 ` [Bug driver/107787] " rguenth at gcc dot gnu.org
2022-11-30 13:31 ` cvs-commit at gcc dot gnu.org
2022-11-30 13:32 ` amonakov 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).