[committed] analyzer: only look for named functions in root ns [PR107788]

Message ID 20221122223711.3308884-1-dmalcolm@redhat.com
State New
Headers
Series [committed] analyzer: only look for named functions in root ns [PR107788] |

Commit Message

David Malcolm Nov. 22, 2022, 10:37 p.m. UTC
  Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Pushed to trunk as r13-4249-gec7c796de020cb.

gcc/analyzer/ChangeLog:
	PR analyzer/107788
	* known-function-manager.cc (known_function_manager::get_match):
	Don't look up fndecls by name when they're not in the root
	namespace.

gcc/testsuite/ChangeLog:
	PR analyzer/107788
	* g++.dg/analyzer/named-functions.C: New test.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
---
 gcc/analyzer/known-function-manager.cc          | 15 ++++++++++++---
 gcc/testsuite/g++.dg/analyzer/named-functions.C | 12 ++++++++++++
 2 files changed, 24 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/analyzer/named-functions.C
  

Patch

diff --git a/gcc/analyzer/known-function-manager.cc b/gcc/analyzer/known-function-manager.cc
index e17350da5ec..c1074bcb6e5 100644
--- a/gcc/analyzer/known-function-manager.cc
+++ b/gcc/analyzer/known-function-manager.cc
@@ -91,6 +91,7 @@  known_function_manager::add (enum internal_fn ifn,
 const known_function *
 known_function_manager::get_match (tree fndecl, const call_details &cd) const
 {
+  /* Look for a matching built-in.  */
   if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
     {
       if (const known_function *candidate
@@ -99,10 +100,18 @@  known_function_manager::get_match (tree fndecl, const call_details &cd) const
 						    fndecl))
 	  return candidate;
     }
+
+  /* Look for a match by name.  */
+
+  /* Reject fndecls that aren't in the root namespace.  */
+  if (DECL_CONTEXT (fndecl)
+      && TREE_CODE (DECL_CONTEXT (fndecl)) != TRANSLATION_UNIT_DECL)
+    return NULL;
   if (tree identifier = DECL_NAME (fndecl))
-      if (const known_function *candidate = get_by_identifier (identifier))
-	if (candidate->matches_call_types_p (cd))
-	  return candidate;
+    if (const known_function *candidate = get_by_identifier (identifier))
+      if (candidate->matches_call_types_p (cd))
+	return candidate;
+
   return NULL;
 }
 
diff --git a/gcc/testsuite/g++.dg/analyzer/named-functions.C b/gcc/testsuite/g++.dg/analyzer/named-functions.C
new file mode 100644
index 00000000000..661a9307b81
--- /dev/null
+++ b/gcc/testsuite/g++.dg/analyzer/named-functions.C
@@ -0,0 +1,12 @@ 
+#define NULL ((void *)0)
+
+namespace my
+{
+  int socket (int, int, int);
+};
+
+void test_my_socket ()
+{
+  /* This shouldn't match the known function "::socket".  */
+  my::socket (0, 0, 0); /* { dg-bogus "leak" } */
+}