https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94195
Dmitry G. Dyachenko <dimhen at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |dimhen at gmail dot com
--- Comment #3 from Dmitry G. Dyachenko <dimhen at gmail dot com> ---
(In reply to CVS Commits from comment #2)
> The master branch has been updated by Martin Sebor <[email protected]>:
>
> https://gcc.gnu.org/g:3f9a497d1b0dd9da87908a11b59bf364ad40ddca
>
> commit r11-3306-g3f9a497d1b0dd9da87908a11b59bf364ad40ddca
> Author: Martin Sebor <[email protected]>
> Date: Sat Sep 19 17:47:29 2020 -0600
>
> Extend -Warray-bounds to detect out-of-bounds accesses to array
> parameters.
>
> gcc/ChangeLog:
>
> PR middle-end/82608
> PR middle-end/94195
> PR c/50584
> PR middle-end/84051
> * gimple-array-bounds.cc (get_base_decl): New function.
> (get_ref_size): New function.
> (trailing_array): New function.
> (array_bounds_checker::check_array_ref): Call them. Handle
> arrays
> declared in function parameters.
> (array_bounds_checker::check_mem_ref): Same. Handle references
> to
> dynamically allocated arrays.
>
> gcc/testsuite/ChangeLog:
>
> PR middle-end/82608
> PR middle-end/94195
> PR c/50584
> PR middle-end/84051
> * c-c++-common/Warray-bounds.c: Adjust.
> * gcc.dg/Wbuiltin-declaration-mismatch-9.c: Adjust.
> * gcc.dg/Warray-bounds-63.c: New test.
> * gcc.dg/Warray-bounds-64.c: New test.
> * gcc.dg/Warray-bounds-65.c: New test.
> * gcc.dg/Warray-bounds-66.c: New test.
> * gcc.dg/Warray-bounds-67.c: New test.
I am a bit confused -- now gcc produces warning.
But access is not out of allocated memory.
Is it expected?
$ cat x.c
#include <stdlib.h>
struct S1 {
unsigned x;
};
struct S {
struct S1 s1;
int z;
};
void f1()
{
struct S *pS = (struct S*) calloc(sizeof(struct S1),1);
if(pS->s1.x == 0)
return;
free(pS);
}
$ gcc -O2 -Wall -c x.i
x.c: In function 'f1':
x.c:18:8: warning: array subscript 'struct S[0]' is partly outside array bounds
of 'unsigned char[4]' [-Warray-bounds]
18 | if(pS->s1.x == 0)
| ^~
x.c:17:30: note: referencing an object of size 4 allocated by 'calloc'
17 | struct S *pS = (struct S*) calloc(sizeof(struct S1),1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~