public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] analyzer: bulletproof -Wanalyzer-file-leak [PR101547]
@ 2021-07-21 23:27 David Malcolm
  0 siblings, 0 replies; only message in thread
From: David Malcolm @ 2021-07-21 23:27 UTC (permalink / raw)
  To: gcc-patches

Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Pushed to trunk as r12-2460-g893b12cc12877aca1c9df6272123b26eddf12722.

gcc/analyzer/ChangeLog:
	PR analyzer/101547
	* sm-file.cc (file_leak::emit): Handle m_arg being NULL.
	(file_leak::describe_final_event): Handle ev.m_expr being NULL.

gcc/testsuite/ChangeLog:
	PR analyzer/101547
	* gcc.dg/analyzer/pr101547.c: New test.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
---
 gcc/analyzer/sm-file.cc                  | 27 ++++++++++++++++++------
 gcc/testsuite/gcc.dg/analyzer/pr101547.c | 11 ++++++++++
 2 files changed, 32 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/analyzer/pr101547.c

diff --git a/gcc/analyzer/sm-file.cc b/gcc/analyzer/sm-file.cc
index b40a9a1edb9..6a17019448e 100644
--- a/gcc/analyzer/sm-file.cc
+++ b/gcc/analyzer/sm-file.cc
@@ -193,9 +193,13 @@ public:
     /* CWE-775: "Missing Release of File Descriptor or Handle after
        Effective Lifetime". */
     m.add_cwe (775);
-    return warning_meta (rich_loc, m, OPT_Wanalyzer_file_leak,
-			 "leak of FILE %qE",
-			 m_arg);
+    if (m_arg)
+      return warning_meta (rich_loc, m, OPT_Wanalyzer_file_leak,
+			   "leak of FILE %qE",
+			   m_arg);
+    else
+      return warning_meta (rich_loc, m, OPT_Wanalyzer_file_leak,
+			   "leak of FILE");
   }
 
   label_text describe_state_change (const evdesc::state_change &change)
@@ -212,10 +216,21 @@ public:
   label_text describe_final_event (const evdesc::final_event &ev) FINAL OVERRIDE
   {
     if (m_fopen_event.known_p ())
-      return ev.formatted_print ("%qE leaks here; was opened at %@",
-				 ev.m_expr, &m_fopen_event);
+      {
+	if (ev.m_expr)
+	  return ev.formatted_print ("%qE leaks here; was opened at %@",
+				     ev.m_expr, &m_fopen_event);
+	else
+	  return ev.formatted_print ("leaks here; was opened at %@",
+				     &m_fopen_event);
+      }
     else
-      return ev.formatted_print ("%qE leaks here", ev.m_expr);
+      {
+	if (ev.m_expr)
+	  return ev.formatted_print ("%qE leaks here", ev.m_expr);
+	else
+	  return ev.formatted_print ("leaks here");
+      }
   }
 
 private:
diff --git a/gcc/testsuite/gcc.dg/analyzer/pr101547.c b/gcc/testsuite/gcc.dg/analyzer/pr101547.c
new file mode 100644
index 00000000000..8791cffa2b6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/analyzer/pr101547.c
@@ -0,0 +1,11 @@
+char *
+fopen (const char *restrict, const char *restrict);
+
+void
+k2 (void)
+{
+  char *setfiles[1];
+  int i;
+
+  setfiles[i] = fopen ("", ""); /* { dg-warning "use of uninitialized value 'i'" } */
+} /* { dg-warning "leak of FILE" } */
-- 
2.26.3


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-07-21 23:28 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-21 23:27 [committed] analyzer: bulletproof -Wanalyzer-file-leak [PR101547] David Malcolm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).