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

PR #82952 introduced regex matching for `CheckedFunctions` in
`UnusedReturnValueCheck` which is used by the checker `cert-err33-c`.

Add a testcase and fix false positives by adding end-of-string to target
regex's.

---------

Signed-off-by: Björn Svensson <bjorn.a.svensson@est.tech>
This commit is contained in:
Björn Svensson 2025-04-11 10:08:29 +02:00 committed by GitHub
parent aeb06c6152
commit 0276915a6c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 190 additions and 177 deletions

View File

@ -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

View File

@ -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

View File

@ -23,3 +23,12 @@ 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");
}