Find a file
David Malcolm fe97f09a0c analyzer: replace -Wanalyzer-unterminated-string with scan_for_null_terminator [PR105899]
In r14-3169-g325f9e88802daa I added check_for_null_terminated_string_arg
to -fanalyzer, calling it in various places, with a sole check for
unterminated string constants, adding -Wanalyzer-unterminated-string for
this case.

This patch adds region_model::scan_for_null_terminator, which simulates
scanning memory for a zero byte, complaining about uninitiliazed bytes
and out-of-range accesses seen before any zero byte is seen.

This more flexible approach catches the issues we saw before with
-Wanalyzer-unterminated-string, and also catches uninitialized runs
of bytes, and I believe will be a better way to build checking of C
string operations in the analyzer.

Given that the patch makes -Wanalyzer-unterminated-string redundant
and that this option was only in trunk for 10 days and has no known
users, the patch simply removes the option without a compatibility
fallback.

The patch uses custom events and notes to provide context on where
the issues are coming from.  For example, given:

null-terminated-strings-1.c: In function ‘test_partially_initialized’:
null-terminated-strings-1.c:71:3: warning: use of uninitialized value ‘buf[1]’ [CWE-457] [-Wanalyzer-use-of-uninitialized-value]
   71 |   __analyzer_get_strlen (buf);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  ‘test_partially_initialized’: events 1-3
    |
    |   69 |   char buf[16];
    |      |        ^~~
    |      |        |
    |      |        (1) region created on stack here
    |   70 |   buf[0] = 'a';
    |   71 |   __analyzer_get_strlen (buf);
    |      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |   |
    |      |   (2) while looking for null terminator for argument 1 (‘&buf’) of ‘__analyzer_get_strlen’...
    |      |   (3) use of uninitialized value ‘buf[1]’ here
    |
analyzer-decls.h:59:22: note: argument 1 of ‘__analyzer_get_strlen’ must be a pointer to a null-terminated string
   59 | extern __SIZE_TYPE__ __analyzer_get_strlen (const char *ptr);
      |                      ^~~~~~~~~~~~~~~~~~~~~

gcc/analyzer/ChangeLog:
	PR analyzer/105899
	* analyzer.opt (Wanalyzer-unterminated-string): Delete.
	* call-details.cc
	(call_details::check_for_null_terminated_string_arg): Convert
	return type from void to const svalue *.  Add param "out_sval".
	* call-details.h
	(call_details::check_for_null_terminated_string_arg): Likewise.
	* kf-analyzer.cc (kf_analyzer_get_strlen::impl_call_pre): Wire up
	to result of check_for_null_terminated_string_arg.
	* region-model.cc (get_strlen): Delete.
	(class unterminated_string_arg): Delete.
	(struct fragment): New.
	(class iterable_cluster): New.
	(region_model::get_store_bytes): New.
	(get_tree_for_byte_offset): New.
	(region_model::scan_for_null_terminator): New.
	(region_model::check_for_null_terminated_string_arg): Convert
	return type from void to const svalue *.  Add param "out_sval".
	Reimplement in terms of scan_for_null_terminator, dropping the
	special-case for -Wanalyzer-unterminated-string.
	* region-model.h (region_model::get_store_bytes): New decl.
	(region_model::scan_for_null_terminator): New decl.
	(region_model::check_for_null_terminated_string_arg): Convert
	return type from void to const svalue *.  Add param "out_sval".
	* store.cc (concrete_binding::get_byte_range): New.
	* store.h (concrete_binding::get_byte_range): New decl.
	(store_manager::get_concrete_binding): New overload.

gcc/ChangeLog:
	PR analyzer/105899
	* doc/invoke.texi: Remove -Wanalyzer-unterminated-string.

gcc/testsuite/ChangeLog:
	PR analyzer/105899
	* gcc.dg/analyzer/error-1.c: Update expected results to reflect
	reimplementation of unterminated string detection.  Add test
	coverage for uninitialized buffers.
	* gcc.dg/analyzer/null-terminated-strings-1.c: Likewise.
	* gcc.dg/analyzer/putenv-1.c: Likewise.
	* gcc.dg/analyzer/strchr-1.c: Likewise.
	* gcc.dg/analyzer/strcpy-1.c: Likewise.
	* gcc.dg/analyzer/strdup-1.c: Likewise.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
2023-08-21 21:13:19 -04:00
c++tools Daily bump. 2023-06-23 00:16:38 +00:00
config Daily bump. 2023-08-12 00:17:36 +00:00
contrib Daily bump. 2023-08-17 00:17:21 +00:00
fixincludes Daily bump. 2023-08-18 00:16:52 +00:00
gcc analyzer: replace -Wanalyzer-unterminated-string with scan_for_null_terminator [PR105899] 2023-08-21 21:13:19 -04:00
gnattools Daily bump. 2023-04-26 00:17:46 +00:00
gotools Daily bump. 2022-08-31 00:16:45 +00:00
include Daily bump. 2023-08-08 00:17:37 +00:00
INSTALL
intl Daily bump. 2023-08-08 00:17:37 +00:00
libada Daily bump. 2023-08-08 00:17:37 +00:00
libatomic Daily bump. 2023-08-08 00:17:37 +00:00
libbacktrace Daily bump. 2023-08-08 00:17:37 +00:00
libcc1 Daily bump. 2023-08-12 00:17:36 +00:00
libcody Daily bump. 2023-06-16 00:17:18 +00:00
libcpp Daily bump. 2023-08-08 00:17:37 +00:00
libdecnumber Daily bump. 2023-06-16 00:17:18 +00:00
libffi Daily bump. 2023-08-08 00:17:37 +00:00
libgcc Daily bump. 2023-08-12 00:17:36 +00:00
libgfortran Daily bump. 2023-08-08 00:17:37 +00:00
libgm2 Daily bump. 2023-08-13 00:16:46 +00:00
libgo cmd/go: don't collect package CGOLDFLAGS when using gccgo 2023-07-20 12:29:11 -07:00
libgomp Daily bump. 2023-08-20 00:17:38 +00:00
libiberty Daily bump. 2023-08-08 00:17:37 +00:00
libitm Daily bump. 2023-08-08 00:17:37 +00:00
libobjc Daily bump. 2023-08-08 00:17:37 +00:00
libphobos Daily bump. 2023-08-21 00:17:21 +00:00
libquadmath Daily bump. 2023-08-08 00:17:37 +00:00
libsanitizer Daily bump. 2023-08-08 00:17:37 +00:00
libssp Daily bump. 2023-08-08 00:17:37 +00:00
libstdc++-v3 Daily bump. 2023-08-22 00:18:05 +00:00
libvtv Daily bump. 2023-08-08 00:17:37 +00:00
lto-plugin Daily bump. 2023-08-08 00:17:37 +00:00
maintainer-scripts Daily bump. 2023-07-08 00:16:53 +00:00
zlib Daily bump. 2023-08-08 00:17:37 +00:00
.dir-locals.el
.gitattributes
.gitignore
ABOUT-NLS
ar-lib
ChangeLog Daily bump. 2023-08-12 00:17:36 +00:00
ChangeLog.jit
ChangeLog.tree-ssa
compile
config-ml.in
config.guess
config.rpath
config.sub
configure Add support for the haiku operating system 2023-08-07 22:59:40 +02:00
configure.ac Add support for the haiku operating system 2023-08-07 22:59:40 +02:00
COPYING
COPYING.LIB
COPYING.RUNTIME
COPYING3
COPYING3.LIB
depcomp
install-sh
libtool-ldflags
libtool.m4 libtool.m4: augment symcode for Solaris 11 2023-08-07 22:59:41 +02:00
ltgcc.m4
ltmain.sh
ltoptions.m4
ltsugar.m4
ltversion.m4
lt~obsolete.m4
MAINTAINERS MAINTAINERS: Add myself to write after approval 2023-08-11 13:07:23 -04:00
Makefile.def toplevel: Makefile.def: add install-strip dependency on libsframe 2023-08-07 22:59:42 +02:00
Makefile.in toplevel: Makefile.def: add install-strip dependency on libsframe 2023-08-07 22:59:42 +02:00
Makefile.tpl Pass PKG_CONFIG_PATH down from top-level Makefile 2023-08-07 22:59:38 +02:00
missing
mkdep
mkinstalldirs
move-if-change
multilib.am
README
symlink-tree
test-driver
ylwrap

This directory contains the GNU Compiler Collection (GCC).

The GNU Compiler Collection is free software.  See the files whose
names start with COPYING for copying permission.  The manuals, and
some of the runtime libraries, are under different terms; see the
individual source files for details.

The directory INSTALL contains copies of the installation information
as HTML and plain text.  The source of this information is
gcc/doc/install.texi.  The installation information includes details
of what is included in the GCC sources and what files GCC installs.

See the file gcc/doc/gcc.texi (together with other files that it
includes) for usage and porting information.  An online readable
version of the manual is in the files gcc/doc/gcc.info*.

See http://gcc.gnu.org/bugs/ for how to report bugs usefully.

Copyright years on GCC source files may be listed using range
notation, e.g., 1987-2012, indicating that every year in the range,
inclusive, is a copyrightable year that could otherwise be listed
individually.