[clang-tools-extra] [clang-tidy] Fix `cert-err33-c` to ignore functions with same prefixes as target (PR #135160)

Björn Svensson via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 11 00:25:50 PDT 2025


https://github.com/bjosv updated https://github.com/llvm/llvm-project/pull/135160

>From 83abf99d0183e195aeae44e10e15a6fa081a50eb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Svensson?= <bjorn.a.svensson at est.tech>
Date: Thu, 10 Apr 2025 11:38:30 +0200
Subject: [PATCH 1/3] [clang-tidy] Add additional test for cert-err33-c
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Björn Svensson <bjorn.a.svensson at est.tech>
---
 .../test/clang-tidy/checkers/cert/err33-c.c           | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/clang-tools-extra/test/clang-tidy/checkers/cert/err33-c.c b/clang-tools-extra/test/clang-tidy/checkers/cert/err33-c.c
index 87ce0acf664e6..38faead57b9eb 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cert/err33-c.c
+++ b/clang-tools-extra/test/clang-tidy/checkers/cert/err33-c.c
@@ -23,3 +23,14 @@ void test_wscanf_s(void) {
   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
   // CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning
 }
+
+int remove(const char *path);
+int removeNonStdLibFunc(const char *path);
+void test_remove(void) {
+  remove("123");
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
+  // CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning
+  removeNonStdLibFunc("123");
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
+  // CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning
+}

>From 670f7e41a06ffbd5c10cf49005324f2280530379 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Svensson?= <bjorn.a.svensson at est.tech>
Date: Thu, 10 Apr 2025 13:01:03 +0200
Subject: [PATCH 2/3] [clang-tidy] Fix cert-err33-c to ignore functions with
 same prefixes as target
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

PR #82952 introduced regexes matching for CheckedFunctions used by this checker.
Fix false positives by adding end-of-string to target regexes.

Signed-off-by: Björn Svensson <bjorn.a.svensson at est.tech>
---
 .../clang-tidy/cert/CERTTidyModule.cpp        | 354 +++++++++---------
 .../test/clang-tidy/checkers/cert/err33-c.c   |   2 -
 2 files changed, 177 insertions(+), 179 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp
index 26befe0de59ae..cc092a9627c5f 100644
--- a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp
@@ -50,183 +50,183 @@ namespace {
 // with NULL argument and in this case the check is not applicable:
 // `mblen, mbrlen, mbrtowc, mbtowc, wctomb, wctomb_s`.
 // FIXME: The check can be improved to handle such cases.
-const llvm::StringRef CertErr33CCheckedFunctions = "^::aligned_alloc;"
-                                                   "^::asctime_s;"
-                                                   "^::at_quick_exit;"
-                                                   "^::atexit;"
-                                                   "^::bsearch;"
-                                                   "^::bsearch_s;"
-                                                   "^::btowc;"
-                                                   "^::c16rtomb;"
-                                                   "^::c32rtomb;"
-                                                   "^::calloc;"
-                                                   "^::clock;"
-                                                   "^::cnd_broadcast;"
-                                                   "^::cnd_init;"
-                                                   "^::cnd_signal;"
-                                                   "^::cnd_timedwait;"
-                                                   "^::cnd_wait;"
-                                                   "^::ctime_s;"
-                                                   "^::fclose;"
-                                                   "^::fflush;"
-                                                   "^::fgetc;"
-                                                   "^::fgetpos;"
-                                                   "^::fgets;"
-                                                   "^::fgetwc;"
-                                                   "^::fopen;"
-                                                   "^::fopen_s;"
-                                                   "^::fprintf;"
-                                                   "^::fprintf_s;"
-                                                   "^::fputc;"
-                                                   "^::fputs;"
-                                                   "^::fputwc;"
-                                                   "^::fputws;"
-                                                   "^::fread;"
-                                                   "^::freopen;"
-                                                   "^::freopen_s;"
-                                                   "^::fscanf;"
-                                                   "^::fscanf_s;"
-                                                   "^::fseek;"
-                                                   "^::fsetpos;"
-                                                   "^::ftell;"
-                                                   "^::fwprintf;"
-                                                   "^::fwprintf_s;"
-                                                   "^::fwrite;"
-                                                   "^::fwscanf;"
-                                                   "^::fwscanf_s;"
-                                                   "^::getc;"
-                                                   "^::getchar;"
-                                                   "^::getenv;"
-                                                   "^::getenv_s;"
-                                                   "^::gets_s;"
-                                                   "^::getwc;"
-                                                   "^::getwchar;"
-                                                   "^::gmtime;"
-                                                   "^::gmtime_s;"
-                                                   "^::localtime;"
-                                                   "^::localtime_s;"
-                                                   "^::malloc;"
-                                                   "^::mbrtoc16;"
-                                                   "^::mbrtoc32;"
-                                                   "^::mbsrtowcs;"
-                                                   "^::mbsrtowcs_s;"
-                                                   "^::mbstowcs;"
-                                                   "^::mbstowcs_s;"
-                                                   "^::memchr;"
-                                                   "^::mktime;"
-                                                   "^::mtx_init;"
-                                                   "^::mtx_lock;"
-                                                   "^::mtx_timedlock;"
-                                                   "^::mtx_trylock;"
-                                                   "^::mtx_unlock;"
-                                                   "^::printf_s;"
-                                                   "^::putc;"
-                                                   "^::putwc;"
-                                                   "^::raise;"
-                                                   "^::realloc;"
-                                                   "^::remove;"
-                                                   "^::rename;"
-                                                   "^::scanf;"
-                                                   "^::scanf_s;"
-                                                   "^::setlocale;"
-                                                   "^::setvbuf;"
-                                                   "^::signal;"
-                                                   "^::snprintf;"
-                                                   "^::snprintf_s;"
-                                                   "^::sprintf;"
-                                                   "^::sprintf_s;"
-                                                   "^::sscanf;"
-                                                   "^::sscanf_s;"
-                                                   "^::strchr;"
-                                                   "^::strerror_s;"
-                                                   "^::strftime;"
-                                                   "^::strpbrk;"
-                                                   "^::strrchr;"
-                                                   "^::strstr;"
-                                                   "^::strtod;"
-                                                   "^::strtof;"
-                                                   "^::strtoimax;"
-                                                   "^::strtok;"
-                                                   "^::strtok_s;"
-                                                   "^::strtol;"
-                                                   "^::strtold;"
-                                                   "^::strtoll;"
-                                                   "^::strtoul;"
-                                                   "^::strtoull;"
-                                                   "^::strtoumax;"
-                                                   "^::strxfrm;"
-                                                   "^::swprintf;"
-                                                   "^::swprintf_s;"
-                                                   "^::swscanf;"
-                                                   "^::swscanf_s;"
-                                                   "^::thrd_create;"
-                                                   "^::thrd_detach;"
-                                                   "^::thrd_join;"
-                                                   "^::thrd_sleep;"
-                                                   "^::time;"
-                                                   "^::timespec_get;"
-                                                   "^::tmpfile;"
-                                                   "^::tmpfile_s;"
-                                                   "^::tmpnam;"
-                                                   "^::tmpnam_s;"
-                                                   "^::tss_create;"
-                                                   "^::tss_get;"
-                                                   "^::tss_set;"
-                                                   "^::ungetc;"
-                                                   "^::ungetwc;"
-                                                   "^::vfprintf;"
-                                                   "^::vfprintf_s;"
-                                                   "^::vfscanf;"
-                                                   "^::vfscanf_s;"
-                                                   "^::vfwprintf;"
-                                                   "^::vfwprintf_s;"
-                                                   "^::vfwscanf;"
-                                                   "^::vfwscanf_s;"
-                                                   "^::vprintf_s;"
-                                                   "^::vscanf;"
-                                                   "^::vscanf_s;"
-                                                   "^::vsnprintf;"
-                                                   "^::vsnprintf_s;"
-                                                   "^::vsprintf;"
-                                                   "^::vsprintf_s;"
-                                                   "^::vsscanf;"
-                                                   "^::vsscanf_s;"
-                                                   "^::vswprintf;"
-                                                   "^::vswprintf_s;"
-                                                   "^::vswscanf;"
-                                                   "^::vswscanf_s;"
-                                                   "^::vwprintf_s;"
-                                                   "^::vwscanf;"
-                                                   "^::vwscanf_s;"
-                                                   "^::wcrtomb;"
-                                                   "^::wcschr;"
-                                                   "^::wcsftime;"
-                                                   "^::wcspbrk;"
-                                                   "^::wcsrchr;"
-                                                   "^::wcsrtombs;"
-                                                   "^::wcsrtombs_s;"
-                                                   "^::wcsstr;"
-                                                   "^::wcstod;"
-                                                   "^::wcstof;"
-                                                   "^::wcstoimax;"
-                                                   "^::wcstok;"
-                                                   "^::wcstok_s;"
-                                                   "^::wcstol;"
-                                                   "^::wcstold;"
-                                                   "^::wcstoll;"
-                                                   "^::wcstombs;"
-                                                   "^::wcstombs_s;"
-                                                   "^::wcstoul;"
-                                                   "^::wcstoull;"
-                                                   "^::wcstoumax;"
-                                                   "^::wcsxfrm;"
-                                                   "^::wctob;"
-                                                   "^::wctrans;"
-                                                   "^::wctype;"
-                                                   "^::wmemchr;"
-                                                   "^::wprintf_s;"
-                                                   "^::wscanf;"
-                                                   "^::wscanf_s;";
+const llvm::StringRef CertErr33CCheckedFunctions = "^::aligned_alloc$;"
+                                                   "^::asctime_s$;"
+                                                   "^::at_quick_exit$;"
+                                                   "^::atexit$;"
+                                                   "^::bsearch$;"
+                                                   "^::bsearch_s$;"
+                                                   "^::btowc$;"
+                                                   "^::c16rtomb$;"
+                                                   "^::c32rtomb$;"
+                                                   "^::calloc$;"
+                                                   "^::clock$;"
+                                                   "^::cnd_broadcast$;"
+                                                   "^::cnd_init$;"
+                                                   "^::cnd_signal$;"
+                                                   "^::cnd_timedwait$;"
+                                                   "^::cnd_wait$;"
+                                                   "^::ctime_s$;"
+                                                   "^::fclose$;"
+                                                   "^::fflush$;"
+                                                   "^::fgetc$;"
+                                                   "^::fgetpos$;"
+                                                   "^::fgets$;"
+                                                   "^::fgetwc$;"
+                                                   "^::fopen$;"
+                                                   "^::fopen_s$;"
+                                                   "^::fprintf$;"
+                                                   "^::fprintf_s$;"
+                                                   "^::fputc$;"
+                                                   "^::fputs$;"
+                                                   "^::fputwc$;"
+                                                   "^::fputws$;"
+                                                   "^::fread$;"
+                                                   "^::freopen$;"
+                                                   "^::freopen_s$;"
+                                                   "^::fscanf$;"
+                                                   "^::fscanf_s$;"
+                                                   "^::fseek$;"
+                                                   "^::fsetpos$;"
+                                                   "^::ftell$;"
+                                                   "^::fwprintf$;"
+                                                   "^::fwprintf_s$;"
+                                                   "^::fwrite$;"
+                                                   "^::fwscanf$;"
+                                                   "^::fwscanf_s$;"
+                                                   "^::getc$;"
+                                                   "^::getchar$;"
+                                                   "^::getenv$;"
+                                                   "^::getenv_s$;"
+                                                   "^::gets_s$;"
+                                                   "^::getwc$;"
+                                                   "^::getwchar$;"
+                                                   "^::gmtime$;"
+                                                   "^::gmtime_s$;"
+                                                   "^::localtime$;"
+                                                   "^::localtime_s$;"
+                                                   "^::malloc$;"
+                                                   "^::mbrtoc16$;"
+                                                   "^::mbrtoc32$;"
+                                                   "^::mbsrtowcs$;"
+                                                   "^::mbsrtowcs_s$;"
+                                                   "^::mbstowcs$;"
+                                                   "^::mbstowcs_s$;"
+                                                   "^::memchr$;"
+                                                   "^::mktime$;"
+                                                   "^::mtx_init$;"
+                                                   "^::mtx_lock$;"
+                                                   "^::mtx_timedlock$;"
+                                                   "^::mtx_trylock$;"
+                                                   "^::mtx_unlock$;"
+                                                   "^::printf_s$;"
+                                                   "^::putc$;"
+                                                   "^::putwc$;"
+                                                   "^::raise$;"
+                                                   "^::realloc$;"
+                                                   "^::remove$;"
+                                                   "^::rename$;"
+                                                   "^::scanf$;"
+                                                   "^::scanf_s$;"
+                                                   "^::setlocale$;"
+                                                   "^::setvbuf$;"
+                                                   "^::signal$;"
+                                                   "^::snprintf$;"
+                                                   "^::snprintf_s$;"
+                                                   "^::sprintf$;"
+                                                   "^::sprintf_s$;"
+                                                   "^::sscanf$;"
+                                                   "^::sscanf_s$;"
+                                                   "^::strchr$;"
+                                                   "^::strerror_s$;"
+                                                   "^::strftime$;"
+                                                   "^::strpbrk$;"
+                                                   "^::strrchr$;"
+                                                   "^::strstr$;"
+                                                   "^::strtod$;"
+                                                   "^::strtof$;"
+                                                   "^::strtoimax$;"
+                                                   "^::strtok$;"
+                                                   "^::strtok_s$;"
+                                                   "^::strtol$;"
+                                                   "^::strtold$;"
+                                                   "^::strtoll$;"
+                                                   "^::strtoul$;"
+                                                   "^::strtoull$;"
+                                                   "^::strtoumax$;"
+                                                   "^::strxfrm$;"
+                                                   "^::swprintf$;"
+                                                   "^::swprintf_s$;"
+                                                   "^::swscanf$;"
+                                                   "^::swscanf_s$;"
+                                                   "^::thrd_create$;"
+                                                   "^::thrd_detach$;"
+                                                   "^::thrd_join$;"
+                                                   "^::thrd_sleep$;"
+                                                   "^::time$;"
+                                                   "^::timespec_get$;"
+                                                   "^::tmpfile$;"
+                                                   "^::tmpfile_s$;"
+                                                   "^::tmpnam$;"
+                                                   "^::tmpnam_s$;"
+                                                   "^::tss_create$;"
+                                                   "^::tss_get$;"
+                                                   "^::tss_set$;"
+                                                   "^::ungetc$;"
+                                                   "^::ungetwc$;"
+                                                   "^::vfprintf$;"
+                                                   "^::vfprintf_s$;"
+                                                   "^::vfscanf$;"
+                                                   "^::vfscanf_s$;"
+                                                   "^::vfwprintf$;"
+                                                   "^::vfwprintf_s$;"
+                                                   "^::vfwscanf$;"
+                                                   "^::vfwscanf_s$;"
+                                                   "^::vprintf_s$;"
+                                                   "^::vscanf$;"
+                                                   "^::vscanf_s$;"
+                                                   "^::vsnprintf$;"
+                                                   "^::vsnprintf_s$;"
+                                                   "^::vsprintf$;"
+                                                   "^::vsprintf_s$;"
+                                                   "^::vsscanf$;"
+                                                   "^::vsscanf_s$;"
+                                                   "^::vswprintf$;"
+                                                   "^::vswprintf_s$;"
+                                                   "^::vswscanf$;"
+                                                   "^::vswscanf_s$;"
+                                                   "^::vwprintf_s$;"
+                                                   "^::vwscanf$;"
+                                                   "^::vwscanf_s$;"
+                                                   "^::wcrtomb$;"
+                                                   "^::wcschr$;"
+                                                   "^::wcsftime$;"
+                                                   "^::wcspbrk$;"
+                                                   "^::wcsrchr$;"
+                                                   "^::wcsrtombs$;"
+                                                   "^::wcsrtombs_s$;"
+                                                   "^::wcsstr$;"
+                                                   "^::wcstod$;"
+                                                   "^::wcstof$;"
+                                                   "^::wcstoimax$;"
+                                                   "^::wcstok$;"
+                                                   "^::wcstok_s$;"
+                                                   "^::wcstol$;"
+                                                   "^::wcstold$;"
+                                                   "^::wcstoll$;"
+                                                   "^::wcstombs$;"
+                                                   "^::wcstombs_s$;"
+                                                   "^::wcstoul$;"
+                                                   "^::wcstoull$;"
+                                                   "^::wcstoumax$;"
+                                                   "^::wcsxfrm$;"
+                                                   "^::wctob$;"
+                                                   "^::wctrans$;"
+                                                   "^::wctype$;"
+                                                   "^::wmemchr$;"
+                                                   "^::wprintf_s$;"
+                                                   "^::wscanf$;"
+                                                   "^::wscanf_s$;";
 
 } // namespace
 
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cert/err33-c.c b/clang-tools-extra/test/clang-tidy/checkers/cert/err33-c.c
index 38faead57b9eb..5d8b6d11e5cdd 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cert/err33-c.c
+++ b/clang-tools-extra/test/clang-tidy/checkers/cert/err33-c.c
@@ -31,6 +31,4 @@ void test_remove(void) {
   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
   // CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning
   removeNonStdLibFunc("123");
-  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
-  // CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning
 }

>From 6c2e4c8eb9978252401700417fc64fb91c13aaad Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Svensson?= <bjorn.a.svensson at est.tech>
Date: Fri, 11 Apr 2025 09:24:42 +0200
Subject: [PATCH 3/3] fixup: update ReleaseNotes.rst
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Björn Svensson <bjorn.a.svensson at est.tech>
---
 clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index a8ae35c7f744e..761c1d3a80359 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -151,6 +151,10 @@ Changes in existing checks
   <clang-tidy/checks/bugprone/unsafe-functions>` check to allow specifying
   additional C++ member functions to match.
 
+- Improved :doc:`cert-err33-c
+  <clang-tidy/checks/cert/err33-c>` check by fixing false positives when
+  a function name is just prefixed with a targeted function name.
+
 - Improved :doc:`misc-const-correctness
   <clang-tidy/checks/misc/const-correctness>` check by adding the option
   `AllowedTypes`, that excludes specified types from const-correctness



More information about the cfe-commits mailing list