Author: Balázs Kéri
Date: 2026-07-10T11:19:43+02:00
New Revision: 83c5973a0a9e849efdec168a1b712f355e59ff4c

URL: 
https://github.com/llvm/llvm-project/commit/83c5973a0a9e849efdec168a1b712f355e59ff4c
DIFF: 
https://github.com/llvm/llvm-project/commit/83c5973a0a9e849efdec168a1b712f355e59ff4c.diff

LOG: [clang][analyzer] Fixed 'if_nameindex' in MallocChecker (#207726)

The function `if_nameindex` has 0 arguments but `MallocChecker` expected
it to have 1 argument and was not recognized by the checker correctly.

Added: 
    

Modified: 
    clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
    clang/test/Analysis/malloc-failure.c
    clang/test/Analysis/malloc.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
index 7ea028246a2ee..287824984623c 100644
--- a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
@@ -547,7 +547,7 @@ class MallocChecker
       {{CDM::CLibrary, {"strdup"}, 1}, &MallocChecker::checkStrdup},
       {{CDM::CLibrary, {"_strdup"}, 1}, &MallocChecker::checkStrdup},
       {{CDM::CLibrary, {"kmalloc"}, 2}, &MallocChecker::checkKernelMalloc},
-      {{CDM::CLibrary, {"if_nameindex"}, 1}, &MallocChecker::checkIfNameIndex},
+      {{CDM::CLibrary, {"if_nameindex"}, 0}, &MallocChecker::checkIfNameIndex},
       {{CDM::CLibrary, {"wcsdup"}, 1}, &MallocChecker::checkStrdup},
       {{CDM::CLibrary, {"_wcsdup"}, 1}, &MallocChecker::checkStrdup},
       {{CDM::CLibrary, {"g_malloc"}, 1}, &MallocChecker::checkBasicAlloc},

diff  --git a/clang/test/Analysis/malloc-failure.c 
b/clang/test/Analysis/malloc-failure.c
index bf421fe7672c8..aea58dad470fa 100644
--- a/clang/test/Analysis/malloc-failure.c
+++ b/clang/test/Analysis/malloc-failure.c
@@ -71,6 +71,6 @@ void test_strdup() {
 
 void test_ifnameindex() {
   struct if_nameindex *p = if_nameindex();
-  p->x = 1; //FIXME: if_nameindex is not recognized by the checker
+  p->x = 1; //expected-warning{{dereference of a null pointer}}
   if_freenameindex(p);
 }

diff  --git a/clang/test/Analysis/malloc.c b/clang/test/Analysis/malloc.c
index 849ab3a3a0f37..a75144f041de0 100644
--- a/clang/test/Analysis/malloc.c
+++ b/clang/test/Analysis/malloc.c
@@ -1018,6 +1018,21 @@ void testWinWcsdupContentIsDefined(const wchar_t *s, 
unsigned validIndex) {
   free(s2);
 }
 
+struct if_nameindex { char x; };
+struct if_nameindex *if_nameindex(void);
+void if_freenameindex(struct if_nameindex *ptr);
+
+char testIfnameindex() {
+  struct if_nameindex *i = if_nameindex();
+  return i->x; // expected-warning {{Potential leak of memory pointed to by}}
+}
+
+void testIffreenameindex() {
+  struct if_nameindex *i = if_nameindex();
+  char x = i->x;
+  if_freenameindex(i);
+} // no warning
+
 // ----------------------------------------------------------------------------
 // Test the system library functions to which the pointer can escape.
 // This tests false positive suppression.


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to