public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug sanitizer/98609] New: sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension
@ 2021-01-08 21:46 muecker at gwdg dot de
  2021-01-11  8:15 ` [Bug sanitizer/98609] " marxin at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: muecker at gwdg dot de @ 2021-01-08 21:46 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98609
           Summary: sanitizer diagnoses VLAs with length zero although
                    zero-length arrays are a GNU extension
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: sanitizer
          Assignee: unassigned at gcc dot gnu.org
          Reporter: muecker at gwdg dot de
                CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org,
                    jakub at gcc dot gnu.org, kcc at gcc dot gnu.org, marxin at gcc dot gnu.org
  Target Milestone: ---

The following code is diagnosed with -fsanitize=undefined:

int main()
{
        int n = 0;
        double x[n];
}


runtime error: variable length array bound evaluates to non-positive value 0


But arrays of zero length are often useful and also generally support by GCC as
an extension. So it would be useful if this would get diagnosed by default. At
least there should be a way to turn this off.

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

* [Bug sanitizer/98609] sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension
  2021-01-08 21:46 [Bug sanitizer/98609] New: sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension muecker at gwdg dot de
@ 2021-01-11  8:15 ` marxin at gcc dot gnu.org
  2021-01-11  9:04 ` jakub at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: marxin at gcc dot gnu.org @ 2021-01-11  8:15 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
                 CC|                            |mpolacek at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-01-11

--- Comment #1 from Martin Liška <marxin at gcc dot gnu.org> ---
Confirmed.
@Marek: Can you please take a look?

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

* [Bug sanitizer/98609] sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension
  2021-01-08 21:46 [Bug sanitizer/98609] New: sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension muecker at gwdg dot de
  2021-01-11  8:15 ` [Bug sanitizer/98609] " marxin at gcc dot gnu.org
@ 2021-01-11  9:04 ` jakub at gcc dot gnu.org
  2021-01-11  9:11 ` muecker at gwdg dot de
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-01-11  9:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
The sanitizers generally diagnose what the C or C++ language spec say, not what
the various extensions allow, it is the same also for shifts etc.
So I think it is correct that this is diagnosed by default.

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

* [Bug sanitizer/98609] sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension
  2021-01-08 21:46 [Bug sanitizer/98609] New: sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension muecker at gwdg dot de
  2021-01-11  8:15 ` [Bug sanitizer/98609] " marxin at gcc dot gnu.org
  2021-01-11  9:04 ` jakub at gcc dot gnu.org
@ 2021-01-11  9:11 ` muecker at gwdg dot de
  2021-01-11  9:19 ` jakub at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: muecker at gwdg dot de @ 2021-01-11  9:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Martin Uecker <muecker at gwdg dot de> ---
Fair enough. But there is also no way to selectively turn it off (or I am not
aware of it).  The warning for < 0 is important and useful while the warning
for == 0 is pedantic and not useful by itself when we generally allow zero
sized arrays elsewhere.

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

* [Bug sanitizer/98609] sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension
  2021-01-08 21:46 [Bug sanitizer/98609] New: sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension muecker at gwdg dot de
                   ` (2 preceding siblings ...)
  2021-01-11  9:11 ` muecker at gwdg dot de
@ 2021-01-11  9:19 ` jakub at gcc dot gnu.org
  2021-01-11  9:24 ` marxin at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-01-11  9:19 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I'd say it is similar with shifts, the diagnostics about the shift count being
negative or too large is highly useful, the diagnostics about the various
lshift properties shifting into sign bit or shifting left negative values etc.)
are pedantic, at least GCC doesn't really take any advantage of that, because
there are so many different variants of the restrictions.
But for shifts we indeed do have a way to selectively turn one or the other on
or off.
So perhaps -fsanitize=vla can be a union of two subooptions too.
Though, we can't really do anything about this until upstream libsanitizer is
changed, because the diagnostics comes from that library.
And it would be really confusing to just do this on the GCC side, allow
VLA checks of <= 0 (=vla), < 0 (=vla-negative), == 0 (=vla-zero) and use the
same library routine in all cases that would always talk about non-positive
value.

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

* [Bug sanitizer/98609] sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension
  2021-01-08 21:46 [Bug sanitizer/98609] New: sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension muecker at gwdg dot de
                   ` (3 preceding siblings ...)
  2021-01-11  9:19 ` jakub at gcc dot gnu.org
@ 2021-01-11  9:24 ` marxin at gcc dot gnu.org
  2021-01-11  9:30 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: marxin at gcc dot gnu.org @ 2021-01-11  9:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Martin Liška <marxin at gcc dot gnu.org> ---
I would recommend changing just libsanitizer to allow something like
UBSAN_OPTIONS=vla_bounds_allow_zero=1. Should be relatively small change.

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

* [Bug sanitizer/98609] sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension
  2021-01-08 21:46 [Bug sanitizer/98609] New: sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension muecker at gwdg dot de
                   ` (4 preceding siblings ...)
  2021-01-11  9:24 ` marxin at gcc dot gnu.org
@ 2021-01-11  9:30 ` jakub at gcc dot gnu.org
  2021-01-11  9:42 ` marxin at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-01-11  9:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
That is not really possible, as the compiler assumes for
-fno-sanitize-recover=vla that when we call the library routine, it never
returns (it is noreturn).

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

* [Bug sanitizer/98609] sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension
  2021-01-08 21:46 [Bug sanitizer/98609] New: sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension muecker at gwdg dot de
                   ` (5 preceding siblings ...)
  2021-01-11  9:30 ` jakub at gcc dot gnu.org
@ 2021-01-11  9:42 ` marxin at gcc dot gnu.org
  2021-01-11 17:58 ` msebor at gcc dot gnu.org
  2023-07-30 10:15 ` muecker at gwdg dot de
  8 siblings, 0 replies; 10+ messages in thread
From: marxin at gcc dot gnu.org @ 2021-01-11  9:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Martin Liška <marxin at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #6)
> That is not really possible, as the compiler assumes for
> -fno-sanitize-recover=vla

which is not the default value/

> that when we call the library routine, it never
> returns (it is noreturn).

Then it can be allowed only for recover UBSAN entry points.

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

* [Bug sanitizer/98609] sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension
  2021-01-08 21:46 [Bug sanitizer/98609] New: sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension muecker at gwdg dot de
                   ` (6 preceding siblings ...)
  2021-01-11  9:42 ` marxin at gcc dot gnu.org
@ 2021-01-11 17:58 ` msebor at gcc dot gnu.org
  2023-07-30 10:15 ` muecker at gwdg dot de
  8 siblings, 0 replies; 10+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-01-11 17:58 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #8 from Martin Sebor <msebor at gcc dot gnu.org> ---
Zero allocations should be diagnosed by -Walloc-zero.  The option is disabled
by default to avoid false positives for calls to malloc(0) emitted by GCC in
some cases.  The test case in comment #0 isn't diagnosed even when -Walloc-zero
is explicitly set is a bug.  It should be diagnosed, and I think for VLAs the
warning should be enabled in -Wall to help detect potential aliasing
violations).

Some uses of zero length arrays that aren't VLAs are diagnosed by
-Wzero-length-bounds (enabled by -Warray-bounds).  I posted a patch in November
to enhance their detection, including VLAs, but it never got reviewed.  I
expect to resubmit it for GCC 12.  With that patch, the test case in comment #0
is diagnosed as long as the array is either accessed or passed as an argument
to a function that might use it.

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

* [Bug sanitizer/98609] sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension
  2021-01-08 21:46 [Bug sanitizer/98609] New: sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension muecker at gwdg dot de
                   ` (7 preceding siblings ...)
  2021-01-11 17:58 ` msebor at gcc dot gnu.org
@ 2023-07-30 10:15 ` muecker at gwdg dot de
  8 siblings, 0 replies; 10+ messages in thread
From: muecker at gwdg dot de @ 2023-07-30 10:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Martin Uecker <muecker at gwdg dot de> ---

I just ran into this problem again while trying to fix PR98608 where the
problem is that instrumentation of parameters is missing.  In parameters people
often use n == 0 null and some of the -Wnonnull warnings we have currently even
require setting it 0.

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

end of thread, other threads:[~2023-07-30 10:15 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-08 21:46 [Bug sanitizer/98609] New: sanitizer diagnoses VLAs with length zero although zero-length arrays are a GNU extension muecker at gwdg dot de
2021-01-11  8:15 ` [Bug sanitizer/98609] " marxin at gcc dot gnu.org
2021-01-11  9:04 ` jakub at gcc dot gnu.org
2021-01-11  9:11 ` muecker at gwdg dot de
2021-01-11  9:19 ` jakub at gcc dot gnu.org
2021-01-11  9:24 ` marxin at gcc dot gnu.org
2021-01-11  9:30 ` jakub at gcc dot gnu.org
2021-01-11  9:42 ` marxin at gcc dot gnu.org
2021-01-11 17:58 ` msebor at gcc dot gnu.org
2023-07-30 10:15 ` 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).