Bug 91584 - [9 Regression] Bogus warning from -Warray-bounds during string assignment
Summary: [9 Regression] Bogus warning from -Warray-bounds during string assignment
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 9.2.0
: P5 normal
Target Milestone: 10.0
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, patch
Depends on:
Blocks: Warray-bounds
  Show dependency treegraph
 
Reported: 2019-08-28 16:33 UTC by Rich Townsend
Modified: 2022-12-19 21:38 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 10.0
Known to fail: 9.2.0
Last reconfirmed: 2019-08-29 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Rich Townsend 2019-08-28 16:33:58 UTC
The following test program produces bogus -Warray-bounds warnings at compile time:

program test_bounds

  character(256) :: foo

  foo = '1234'

  print *, foo

end program test_bounds

Compiling with 'gfortran -O2 -Warray-bounds -o test_bounds test_bounds.f90' yields:
 
test_bounds.f90:5:0:

    5 |   foo = '1234'
      | 
Warning: array subscript 0 is outside array bounds of 'character(kind=1)[1:256]' [-Warray-bounds]
test_bounds.f90:3:0:

    3 |   character(256) :: foo
      | 
note: while referencing 'foo'

Compiling at a lower optimization level makes the warning go away.

cheers,

Rich
Comment 1 kargls 2019-08-28 17:45:33 UTC
In looking at the gcc.info and gfortran.info pages,
I'll suggest that one should avoid the -Warray-bounds
option.  It appears to be C/C++-centric, where bounds
start at 0 instead of one.  Use -fcheck=bounds, which
is gfortran specific.

PS: The only mention of -Warray-bounds in gfortran.info
is in the option summary.  There is no offer mention.
I'll be removing it from the summary.
Comment 2 Richard Biener 2019-08-29 07:31:02 UTC
There's nothing C specific about -Warray-bounds, it might be simply a bug ;)

The issue is that vrp_prop::check_mem_ref looks at the decl in MEM[&decl],
failing to account for the index-shift created by the MEM_REFs type
(domain [0, ...]) vs. the decls domain [1, ...].  Or simply by failing to
subtract the domain min value from arrbounds[] (there's no ARRAY_REF
here, so I wonder what it checks...).

Martin?
Comment 3 Martin Sebor 2019-08-29 22:39:03 UTC
Yes, the array domain checking wasn't correct for languages where the first element of an array is at a nonzero index.  I posted the following patch:
https://gcc.gnu.org/ml/gcc-patches/2019-08/msg02020.html
Comment 4 Martin Sebor 2019-08-30 17:43:28 UTC
Author: msebor
Date: Fri Aug 30 17:42:57 2019
New Revision: 275210

URL: https://gcc.gnu.org/viewcvs?rev=275210&root=gcc&view=rev
Log:
PR middle-end/91584 - Bogus warning from -Warray-bounds during string assignment

gcc/ChangeLog:

	PR middle-end/91584
	* tree-vrp.c (vrp_prop::check_mem_ref): Normalize type domain bounds
	before using them to validate MEM_REF offset.

gcc/testsuite/ChangeLog:
	* gfortran.dg/char_array_constructor_4.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/char_array_constructor_4.f90
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-vrp.c
Comment 5 Martin Sebor 2019-08-30 17:44:28 UTC
Fixed for GCC 10 in r275210.  Will backport to 9.0.
Comment 6 Jakub Jelinek 2020-05-07 11:56:29 UTC
GCC 10.1 has been released.
Comment 7 Richard Biener 2020-07-23 06:51:45 UTC
GCC 10.2 is released, adjusting target milestone.
Comment 8 Richard Biener 2021-04-08 12:02:24 UTC
GCC 10.3 is being released, retargeting bugs to GCC 10.4.
Comment 9 Martin Sebor 2022-03-17 19:56:52 UTC
I'm no longer planning to backport the fix.
Comment 10 Jakub Jelinek 2022-06-28 10:38:18 UTC
GCC 10.4 is being released, retargeting bugs to GCC 10.5.
Comment 11 Andrew Pinski 2022-12-19 21:38:03 UTC
Fixed by r10-2892-g5d69df7e929252 .