aboutsummaryrefslogtreecommitdiff
path: root/gcc/analyzer/analyzer-selftests.cc
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2019-12-19 15:59:04 -0500
committerDavid Malcolm <dmalcolm@redhat.com>2020-01-14 20:39:26 -0500
commit697251b7a1bb7c14d3805de22248e83a23b90d1a (patch)
tree4411d9448340e818e2a373260460f56207f14087 /gcc/analyzer/analyzer-selftests.cc
parent81a68b9e3774401a99719ea29640d13125745b41 (diff)
downloadgcc-697251b7a1bb7c14d3805de22248e83a23b90d1a.zip
gcc-697251b7a1bb7c14d3805de22248e83a23b90d1a.tar.gz
gcc-697251b7a1bb7c14d3805de22248e83a23b90d1a.tar.bz2
analyzer: add known stdio functions to sm-file.cc (PR analyzer/58237)
The analyzer ought to report various file leaks for the reproducer in PR analyzer/58237, such as: void f1(const char *str) { FILE * fp = fopen(str, "r"); char buf[10]; while (fgets(buf, 10, fp) != NULL) { /* Do something with buf */ } /* Missing call to fclose. Need warning here for resource leak */ } but fails to do so, due to not recognizing fgets, and thus conservatively assuming that it could close "fp". This patch adds a function_set to sm-file.cc of numerous stdio.h functions that are known to not close the file (and which require a valid FILE *, but that's a matter for a followup), fixing the issue. gcc/analyzer/ChangeLog: PR analyzer/58237 * analyzer-selftests.cc (selftest::run_analyzer_selftests): Call selftest::analyzer_sm_file_cc_tests. * analyzer-selftests.h (selftest::analyzer_sm_file_cc_tests): New decl. * sm-file.cc: Include "analyzer/function-set.h" and "analyzer/analyzer-selftests.h". (get_file_using_fns): New function. (is_file_using_fn_p): New function. (fileptr_state_machine::on_stmt): Return true for known functions. (selftest::analyzer_sm_file_cc_tests): New function. gcc/testsuite/ChangeLog: PR analyzer/58237 * gcc.dg/analyzer/file-1.c (test_4): New. * gcc.dg/analyzer/file-pr58237.c: New test.
Diffstat (limited to 'gcc/analyzer/analyzer-selftests.cc')
-rw-r--r--gcc/analyzer/analyzer-selftests.cc1
1 files changed, 1 insertions, 0 deletions
diff --git a/gcc/analyzer/analyzer-selftests.cc b/gcc/analyzer/analyzer-selftests.cc
index 2b8fa81..1272936 100644
--- a/gcc/analyzer/analyzer-selftests.cc
+++ b/gcc/analyzer/analyzer-selftests.cc
@@ -54,6 +54,7 @@ run_analyzer_selftests ()
analyzer_program_point_cc_tests ();
analyzer_program_state_cc_tests ();
analyzer_region_model_cc_tests ();
+ analyzer_sm_file_cc_tests ();
analyzer_sm_signal_cc_tests ();
#endif /* #if ENABLE_ANALYZER */
}