Skip to content

Commit 4bcbb3d

Browse files
committed
[clang-tidy] Add check 'cert-err33-c'.
The CERT rule ERR33-C can be modeled partially by the existing check 'bugprone-unused-return-value'. The existing check is reused with a fixed set of checked functions. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D112409
1 parent 1ca35fc commit 4bcbb3d

File tree

6 files changed

+426
-2
lines changed

6 files changed

+426
-2
lines changed

clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp

Lines changed: 193 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "../bugprone/SpuriouslyWakeUpFunctionsCheck.h"
1717
#include "../bugprone/SuspiciousMemoryComparisonCheck.h"
1818
#include "../bugprone/UnhandledSelfAssignmentCheck.h"
19+
#include "../bugprone/UnusedReturnValueCheck.h"
1920
#include "../concurrency/ThreadCanceltypeAsynchronousCheck.h"
2021
#include "../google/UnnamedNamespaceInHeaderCheck.h"
2122
#include "../misc/NewDeleteOverloadsCheck.h"
@@ -39,6 +40,193 @@
3940
#include "ThrownExceptionTypeCheck.h"
4041
#include "VariadicFunctionDefCheck.h"
4142

43+
namespace {
44+
45+
// Checked functions for cert-err33-c.
46+
// The following functions are deliberately excluded because they can be called
47+
// with NULL argument and in this case the check is not applicable:
48+
// `mblen, mbrlen, mbrtowc, mbtowc, wctomb, wctomb_s`.
49+
// FIXME: The check can be improved to handle such cases.
50+
const llvm::StringRef CertErr33CCheckedFunctions = "::aligned_alloc;"
51+
"::asctime_s;"
52+
"::at_quick_exit;"
53+
"::atexit;"
54+
"::bsearch;"
55+
"::bsearch_s;"
56+
"::btowc;"
57+
"::c16rtomb;"
58+
"::c32rtomb;"
59+
"::calloc;"
60+
"::clock;"
61+
"::cnd_broadcast;"
62+
"::cnd_init;"
63+
"::cnd_signal;"
64+
"::cnd_timedwait;"
65+
"::cnd_wait;"
66+
"::ctime_s;"
67+
"::fclose;"
68+
"::fflush;"
69+
"::fgetc;"
70+
"::fgetpos;"
71+
"::fgets;"
72+
"::fgetwc;"
73+
"::fopen;"
74+
"::fopen_s;"
75+
"::fprintf;"
76+
"::fprintf_s;"
77+
"::fputc;"
78+
"::fputs;"
79+
"::fputwc;"
80+
"::fputws;"
81+
"::fread;"
82+
"::freopen;"
83+
"::freopen_s;"
84+
"::fscanf;"
85+
"::fscanf_s;"
86+
"::fseek;"
87+
"::fsetpos;"
88+
"::ftell;"
89+
"::fwprintf;"
90+
"::fwprintf_s;"
91+
"::fwrite;"
92+
"::fwscanf;"
93+
"::fwscanf_s;"
94+
"::getc;"
95+
"::getchar;"
96+
"::getenv;"
97+
"::getenv_s;"
98+
"::gets_s;"
99+
"::getwc;"
100+
"::getwchar;"
101+
"::gmtime;"
102+
"::gmtime_s;"
103+
"::localtime;"
104+
"::localtime_s;"
105+
"::malloc;"
106+
"::mbrtoc16;"
107+
"::mbrtoc32;"
108+
"::mbsrtowcs;"
109+
"::mbsrtowcs_s;"
110+
"::mbstowcs;"
111+
"::mbstowcs_s;"
112+
"::memchr;"
113+
"::mktime;"
114+
"::mtx_init;"
115+
"::mtx_lock;"
116+
"::mtx_timedlock;"
117+
"::mtx_trylock;"
118+
"::mtx_unlock;"
119+
"::printf_s;"
120+
"::putc;"
121+
"::putwc;"
122+
"::raise;"
123+
"::realloc;"
124+
"::remove;"
125+
"::rename;"
126+
"::scanf;"
127+
"::scanf_s;"
128+
"::setlocale;"
129+
"::setvbuf;"
130+
"::signal;"
131+
"::snprintf;"
132+
"::snprintf_s;"
133+
"::sprintf;"
134+
"::sprintf_s;"
135+
"::sscanf;"
136+
"::sscanf_s;"
137+
"::strchr;"
138+
"::strerror_s;"
139+
"::strftime;"
140+
"::strpbrk;"
141+
"::strrchr;"
142+
"::strstr;"
143+
"::strtod;"
144+
"::strtof;"
145+
"::strtoimax;"
146+
"::strtok;"
147+
"::strtok_s;"
148+
"::strtol;"
149+
"::strtold;"
150+
"::strtoll;"
151+
"::strtoul;"
152+
"::strtoull;"
153+
"::strtoumax;"
154+
"::strxfrm;"
155+
"::swprintf;"
156+
"::swprintf_s;"
157+
"::swscanf;"
158+
"::swscanf_s;"
159+
"::thrd_create;"
160+
"::thrd_detach;"
161+
"::thrd_join;"
162+
"::thrd_sleep;"
163+
"::time;"
164+
"::timespec_get;"
165+
"::tmpfile;"
166+
"::tmpfile_s;"
167+
"::tmpnam;"
168+
"::tmpnam_s;"
169+
"::tss_create;"
170+
"::tss_get;"
171+
"::tss_set;"
172+
"::ungetc;"
173+
"::ungetwc;"
174+
"::vfprintf;"
175+
"::vfprintf_s;"
176+
"::vfscanf;"
177+
"::vfscanf_s;"
178+
"::vfwprintf;"
179+
"::vfwprintf_s;"
180+
"::vfwscanf;"
181+
"::vfwscanf_s;"
182+
"::vprintf_s;"
183+
"::vscanf;"
184+
"::vscanf_s;"
185+
"::vsnprintf;"
186+
"::vsnprintf_s;"
187+
"::vsprintf;"
188+
"::vsprintf_s;"
189+
"::vsscanf;"
190+
"::vsscanf_s;"
191+
"::vswprintf;"
192+
"::vswprintf_s;"
193+
"::vswscanf;"
194+
"::vswscanf_s;"
195+
"::vwprintf_s;"
196+
"::vwscanf;"
197+
"::vwscanf_s;"
198+
"::wcrtomb;"
199+
"::wcschr;"
200+
"::wcsftime;"
201+
"::wcspbrk;"
202+
"::wcsrchr;"
203+
"::wcsrtombs;"
204+
"::wcsrtombs_s;"
205+
"::wcsstr;"
206+
"::wcstod;"
207+
"::wcstof;"
208+
"::wcstoimax;"
209+
"::wcstok;"
210+
"::wcstok_s;"
211+
"::wcstol;"
212+
"::wcstold;"
213+
"::wcstoll;"
214+
"::wcstombs;"
215+
"::wcstombs_s;"
216+
"::wcstoul;"
217+
"::wcstoull;"
218+
"::wcstoumax;"
219+
"::wcsxfrm;"
220+
"::wctob;"
221+
"::wctrans;"
222+
"::wctype;"
223+
"::wmemchr;"
224+
"::wprintf_s;"
225+
"::wscanf;"
226+
"::wscanf_s;";
227+
228+
} // namespace
229+
42230
namespace clang {
43231
namespace tidy {
44232
namespace cert {
@@ -99,6 +287,10 @@ class CERTModule : public ClangTidyModule {
99287
"cert-dcl37-c");
100288
// ENV
101289
CheckFactories.registerCheck<CommandProcessorCheck>("cert-env33-c");
290+
// ERR
291+
CheckFactories.registerCheck<bugprone::UnusedReturnValueCheck>(
292+
"cert-err33-c");
293+
CheckFactories.registerCheck<StrToNumCheck>("cert-err34-c");
102294
// EXP
103295
CheckFactories.registerCheck<bugprone::SuspiciousMemoryComparisonCheck>(
104296
"cert-exp42-c");
@@ -108,8 +300,6 @@ class CERTModule : public ClangTidyModule {
108300
"cert-flp37-c");
109301
// FIO
110302
CheckFactories.registerCheck<misc::NonCopyableObjectsCheck>("cert-fio38-c");
111-
// ERR
112-
CheckFactories.registerCheck<StrToNumCheck>("cert-err34-c");
113303
// MSC
114304
CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc30-c");
115305
CheckFactories.registerCheck<ProperlySeededRandomGeneratorCheck>(
@@ -131,6 +321,7 @@ class CERTModule : public ClangTidyModule {
131321
ClangTidyOptions Options;
132322
ClangTidyOptions::OptionMap &Opts = Options.CheckOptions;
133323
Opts["cert-dcl16-c.NewSuffixes"] = "L;LL;LU;LLU";
324+
Opts["cert-err33-c.CheckedFunctions"] = CertErr33CCheckedFunctions;
134325
Opts["cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField"] = "false";
135326
Opts["cert-str34-c.DiagnoseSignedUnsignedCharComparisons"] = "false";
136327
return Options;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ New checks
103103
New check aliases
104104
^^^^^^^^^^^^^^^^^
105105

106+
- New alias :doc:`cert-err33-c
107+
<clang-tidy/checks/cert-err33-c>` to
108+
:doc:`bugprone-unused-return-value
109+
<clang-tidy/checks/bugprone-unused-return-value>` was added.
110+
106111
- New alias :doc:`cert-exp42-c
107112
<clang-tidy/checks/cert-exp42-c>` to
108113
:doc:`bugprone-suspicious-memory-comparison

clang-tools-extra/docs/clang-tidy/checks/bugprone-unused-return-value.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ Options
4545
- ``std::basic_string::empty()`` and ``std::vector::empty()``. Not using the
4646
return value often indicates that the programmer confused the function with
4747
``clear()``.
48+
49+
`cert-err33-c <cert-err33-c.html>`_ is an alias of this check that checks a
50+
fixed and large set of standard library functions.

0 commit comments

Comments
 (0)