diff options
author | Qing Zhao <qing.zhao@oracle.com> | 2022-12-06 18:50:04 +0000 |
---|---|---|
committer | Qing Zhao <qing.zhao@oracle.com> | 2022-12-06 18:50:04 +0000 |
commit | 710c9676520dfd38b4bfdcc937ce026ed89921d6 (patch) | |
tree | 81a6bc70c710cba93ab79a61180ce822fe31ae13 /gcc/doc/invoke.texi | |
parent | dcfc7ac94dbcf6c86c0c58ce6dc1d8bd853e4093 (diff) |
Update -Warray-bounds with -fstrict-flex-arrays.
A. add the following to clarify the relationship between -Warray-bounds
and the LEVEL of -fstrict-flex-array:
By default, the trailing array of a structure will be treated as a
flexible array member by '-Warray-bounds' or '-Warray-bounds=N' if
it is declared as either a flexible array member per C99 standard
onwards ('[]'), a GCC zero-length array extension ('[0]'), or an
one-element array ('[1]'). As a result, out of bounds subscripts
or offsets into zero-length arrays or one-element arrays are not
warned by default.
You can add the option '-fstrict-flex-arrays' or
'-fstrict-flex-arrays=LEVEL' to control how this option treat
trailing array of a structure as a flexible array member.
when LEVEL<=1, no change to the default behavior.
when LEVEL=2, additional warnings will be issued for out of bounds
subscripts or offsets into one-element arrays;
when LEVEL=3, in addition to LEVEL=2, additional warnings will be
issued for out of bounds subscripts or offsets into zero-length
arrays.
B. change -Warray-bounds=2 to exclude its control on how to treat
trailing arrays as flexible array members:
'-Warray-bounds=2'
This warning level also warns about the intermediate results
of pointer arithmetic that may yield out of bounds values.
This warning level may give a larger number of false positives
and is deactivated by default.
gcc/ChangeLog:
* attribs.cc (strict_flex_array_level_of): New function.
* attribs.h (strict_flex_array_level_of): Prototype for new function.
* doc/invoke.texi: Update -Warray-bounds by specifying the impact from
-fstrict-flex-arrays. Also update -Warray-bounds=2 by eliminating its
impact on treating trailing arrays as flexible array members.
* gimple-array-bounds.cc (get_up_bounds_for_array_ref): New function.
(check_out_of_bounds_and_warn): New function.
(array_bounds_checker::check_array_ref): Update with call to the above
new functions.
* tree.cc (array_ref_flexible_size_p): Add one new argument.
(component_ref_sam_type): New function.
(component_ref_size): Control with level of strict-flex-array.
* tree.h (array_ref_flexible_size_p): Update prototype.
(enum struct special_array_member): Add two new enum values.
(component_ref_sam_type): New prototype.
gcc/c/ChangeLog:
* c-decl.cc (is_flexible_array_member_p): Call new function
strict_flex_array_level_of.
gcc/testsuite/ChangeLog:
* gcc.dg/Warray-bounds-11.c: Update warnings for -Warray-bounds=2.
* gcc.dg/Warray-bounds-flex-arrays-1.c: New test.
* gcc.dg/Warray-bounds-flex-arrays-2.c: New test.
* gcc.dg/Warray-bounds-flex-arrays-3.c: New test.
* gcc.dg/Warray-bounds-flex-arrays-4.c: New test.
* gcc.dg/Warray-bounds-flex-arrays-5.c: New test.
* gcc.dg/Warray-bounds-flex-arrays-6.c: New test.
Diffstat (limited to 'gcc/doc/invoke.texi')
-rw-r--r-- | gcc/doc/invoke.texi | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 277ac35ad16..726392409b6 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -8040,17 +8040,34 @@ is enabled by @option{-Wall}. It is more effective when @option{-ftree-vrp} is active (the default for @option{-O2} and above) but a subset of instances are issued even without optimization. +By default, the trailing array of a structure will be treated as a flexible +array member by @option{-Warray-bounds} or @option{-Warray-bounds=@var{n}} +if it is declared as either a flexible array member per C99 standard onwards +(@samp{[]}), a GCC zero-length array extension (@samp{[0]}), or an one-element +array (@samp{[1]}). As a result, out of bounds subscripts or offsets into +zero-length arrays or one-element arrays are not warned by default. + +You can add the option @option{-fstrict-flex-arrays} or +@option{-fstrict-flex-arrays=@var{level}} to control how this +option treat trailing array of a structure as a flexible array member: + +when @var{level}<=1, no change to the default behavior. + +when @var{level}=2, additional warnings will be issued for out of bounds +subscripts or offsets into one-element arrays; + +when @var{level}=3, in addition to @var{level}=2, additional warnings will be +issued for out of bounds subscripts or offsets into zero-length arrays. + @table @gcctabopt @item -Warray-bounds=1 This is the default warning level of @option{-Warray-bounds} and is enabled by @option{-Wall}; higher levels are not, and must be explicitly requested. @item -Warray-bounds=2 -This warning level also warns about out of bounds accesses to trailing -struct members of one-element array types (@pxref{Zero Length}) and about -the intermediate results of pointer arithmetic that may yield out of bounds -values. This warning level may give a larger number of false positives and -is deactivated by default. +This warning level also warns about the intermediate results of pointer +arithmetic that may yield out of bounds values. This warning level may +give a larger number of false positives and is deactivated by default. @end table @item -Warray-compare |