clang-tools  16.0.0git
CERTTidyModule.cpp
Go to the documentation of this file.
1 //===--- CERTTidyModule.cpp - clang-tidy ----------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "../ClangTidy.h"
10 #include "../ClangTidyModule.h"
11 #include "../ClangTidyModuleRegistry.h"
12 #include "../bugprone/BadSignalToKillThreadCheck.h"
13 #include "../bugprone/ReservedIdentifierCheck.h"
14 #include "../bugprone/SignalHandlerCheck.h"
15 #include "../bugprone/SignedCharMisuseCheck.h"
16 #include "../bugprone/SpuriouslyWakeUpFunctionsCheck.h"
17 #include "../bugprone/SuspiciousMemoryComparisonCheck.h"
18 #include "../bugprone/UnhandledSelfAssignmentCheck.h"
19 #include "../bugprone/UnusedReturnValueCheck.h"
20 #include "../concurrency/ThreadCanceltypeAsynchronousCheck.h"
21 #include "../google/UnnamedNamespaceInHeaderCheck.h"
22 #include "../misc/NewDeleteOverloadsCheck.h"
23 #include "../misc/NonCopyableObjects.h"
24 #include "../misc/StaticAssertCheck.h"
25 #include "../misc/ThrowByValueCatchByReferenceCheck.h"
26 #include "../performance/MoveConstructorInitCheck.h"
27 #include "../readability/UppercaseLiteralSuffixCheck.h"
28 #include "CommandProcessorCheck.h"
31 #include "FloatLoopCounter.h"
32 #include "LimitedRandomnessCheck.h"
33 #include "MutatingCopyCheck.h"
35 #include "PostfixOperatorCheck.h"
37 #include "SetLongJmpCheck.h"
39 #include "StrToNumCheck.h"
42 
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 
230 namespace clang {
231 namespace tidy {
232 namespace cert {
233 
234 class CERTModule : public ClangTidyModule {
235 public:
236  void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
237  // C++ checkers
238  // CON
240  "cert-con54-cpp");
241  // DCL
242  CheckFactories.registerCheck<PostfixOperatorCheck>(
243  "cert-dcl21-cpp");
244  CheckFactories.registerCheck<VariadicFunctionDefCheck>("cert-dcl50-cpp");
246  "cert-dcl51-cpp");
248  "cert-dcl54-cpp");
250  "cert-dcl58-cpp");
252  "cert-dcl59-cpp");
253  // ERR
255  "cert-err09-cpp");
256  CheckFactories.registerCheck<SetLongJmpCheck>("cert-err52-cpp");
257  CheckFactories.registerCheck<StaticObjectExceptionCheck>("cert-err58-cpp");
258  CheckFactories.registerCheck<ThrownExceptionTypeCheck>("cert-err60-cpp");
260  "cert-err61-cpp");
261  // MEM
263  "cert-mem57-cpp");
264  // MSC
265  CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc50-cpp");
267  "cert-msc51-cpp");
269  "cert-msc54-cpp");
270  // OOP
272  "cert-oop11-cpp");
274  "cert-oop54-cpp");
276  "cert-oop57-cpp");
277  CheckFactories.registerCheck<MutatingCopyCheck>(
278  "cert-oop58-cpp");
279 
280  // C checkers
281  // CON
283  "cert-con36-c");
284  // DCL
285  CheckFactories.registerCheck<misc::StaticAssertCheck>("cert-dcl03-c");
287  "cert-dcl16-c");
289  "cert-dcl37-c");
290  // ENV
291  CheckFactories.registerCheck<CommandProcessorCheck>("cert-env33-c");
292  // ERR
294  "cert-err33-c");
295  CheckFactories.registerCheck<StrToNumCheck>("cert-err34-c");
296  // EXP
298  "cert-exp42-c");
299  // FLP
300  CheckFactories.registerCheck<FloatLoopCounter>("cert-flp30-c");
302  "cert-flp37-c");
303  // FIO
304  CheckFactories.registerCheck<misc::NonCopyableObjectsCheck>("cert-fio38-c");
305  // MSC
306  CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc30-c");
308  "cert-msc32-c");
309  // POS
311  "cert-pos44-c");
312  CheckFactories
314  "cert-pos47-c");
315  // SIG
316  CheckFactories.registerCheck<bugprone::SignalHandlerCheck>("cert-sig30-c");
317  // STR
319  "cert-str34-c");
320  }
321 
323  ClangTidyOptions Options;
325  Opts["cert-dcl16-c.NewSuffixes"] = "L;LL;LU;LLU";
326  Opts["cert-err33-c.CheckedFunctions"] = CertErr33CCheckedFunctions;
327  Opts["cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField"] = "false";
328  Opts["cert-str34-c.DiagnoseSignedUnsignedCharComparisons"] = "false";
329  return Options;
330  }
331 };
332 
333 } // namespace cert
334 
335 // Register the MiscTidyModule using this statically initialized variable.
336 static ClangTidyModuleRegistry::Add<cert::CERTModule>
337  X("cert-module",
338  "Adds lint checks corresponding to CERT secure coding guidelines.");
339 
340 // This anchor is used to force the linker to link in the generated object file
341 // and thus register the CERTModule.
342 volatile int CERTModuleAnchorSource = 0;
343 
344 } // namespace tidy
345 } // namespace clang
clang::tidy::bugprone::SignalHandlerCheck
Checker for signal handler functions.
Definition: SignalHandlerCheck.h:25
clang::tidy::misc::NewDeleteOverloadsCheck
Definition: NewDeleteOverloadsCheck.h:20
clang::tidy::misc::NonCopyableObjectsCheck
The check flags dereferences and non-pointer declarations of objects that are not meant to be passed ...
Definition: NonCopyableObjects.h:20
clang::tidy::cert::DefaultOperatorNewAlignmentCheck
Checks if an object of type with extended alignment is allocated by using the default operator new.
Definition: DefaultOperatorNewAlignmentCheck.h:23
clang::tidy::cert::CommandProcessorCheck
Execution of a command processor can lead to security vulnerabilities, and is generally not required.
Definition: CommandProcessorCheck.h:25
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
LimitedRandomnessCheck.h
CommandProcessorCheck.h
clang::tidy::cert::LimitedRandomnessCheck
Pseudorandom number generators are not genuinely random.
Definition: LimitedRandomnessCheck.h:25
clang::tidy::performance::MoveConstructorInitCheck
The check flags user-defined move constructors that have a ctor-initializer initializing a member or ...
Definition: MoveConstructorInitCheck.h:23
clang::tidy::bugprone::SuspiciousMemoryComparisonCheck
Finds potentially incorrect calls to memcmp() based on properties of the arguments.
Definition: SuspiciousMemoryComparisonCheck.h:23
clang::tidy::cert::PostfixOperatorCheck
Checks if the overloaded postfix ++ and – operator return a constant object.
Definition: PostfixOperatorCheck.h:23
clang::tidy::cert::VariadicFunctionDefCheck
Guards against any C-style variadic function definitions (not declarations).
Definition: VariadicFunctionDefCheck.h:22
clang::tidy::bugprone::BadSignalToKillThreadCheck
Finds pthread_kill function calls when thread is terminated by SIGTERM signal.
Definition: BadSignalToKillThreadCheck.h:22
clang::tidy::ClangTidyOptions::CheckOptions
OptionMap CheckOptions
Key-value mapping used to store check-specific options.
Definition: ClangTidyOptions.h:118
clang::tidy::cert::DontModifyStdNamespaceCheck
Modification of the std or posix namespace can result in undefined behavior.
Definition: DontModifyStdNamespaceCheck.h:23
ProperlySeededRandomGeneratorCheck.h
MutatingCopyCheck.h
clang::tidy::bugprone::SignedCharMisuseCheck
Finds those signed char -> integer conversions which might indicate a programming error.
Definition: SignedCharMisuseCheck.h:26
clang::tidy::ClangTidyOptions
Contains options for clang-tidy.
Definition: ClangTidyOptions.h:50
clang::tidy::bugprone::UnhandledSelfAssignmentCheck
Finds user-defined copy assignment operators which do not protect the code against self-assignment ei...
Definition: UnhandledSelfAssignmentCheck.h:24
clang::tidy::ClangTidyCheckFactories
A collection of ClangTidyCheckFactory instances.
Definition: ClangTidyModule.h:28
clang::tidy::readability::UppercaseLiteralSuffixCheck
Detects when the integral literal or floating point literal has non-uppercase suffix,...
Definition: UppercaseLiteralSuffixCheck.h:25
clang::tidy::bugprone::SpuriouslyWakeUpFunctionsCheck
Finds cnd_wait, cnd_timedwait, wait, wait_for, or wait_until function calls when the function is not ...
Definition: SpuriouslyWakeUpFunctionsCheck.h:25
clang::tidy::cert::StaticObjectExceptionCheck
Checks whether the constructor for a static or thread_local object will throw.
Definition: StaticObjectExceptionCheck.h:23
StrToNumCheck.h
clang::tidy::cert::ThrownExceptionTypeCheck
Checks whether a thrown object is nothrow copy constructible.
Definition: ThrownExceptionTypeCheck.h:22
clang::tidy::cert::FloatLoopCounter
This check diagnoses when the loop induction expression of a for loop has floating-point type.
Definition: FloatLoopCounter.h:24
clang::tidy::cert::ProperlySeededRandomGeneratorCheck
Random number generator must be seeded properly.
Definition: ProperlySeededRandomGeneratorCheck.h:26
clang::tidy::cert::MutatingCopyCheck
Finds assignments to the copied object and its direct or indirect members in copy constructors and co...
Definition: MutatingCopyCheck.h:23
clang::tidy::misc::StaticAssertCheck
Replaces assert() with static_assert() if the condition is evaluatable at compile time.
Definition: StaticAssertCheck.h:25
clang::tidy::cert::StrToNumCheck
Guards against use of string conversion functions that do not have reasonable error handling for conv...
Definition: StrToNumCheck.h:23
ThrownExceptionTypeCheck.h
clang::tidy::google::build::UnnamedNamespaceInHeaderCheck
Finds anonymous namespaces in headers.
Definition: UnnamedNamespaceInHeaderCheck.h:36
DefaultOperatorNewAlignmentCheck.h
NonTrivialTypesLibcMemoryCallsCheck.h
StaticObjectExceptionCheck.h
SetLongJmpCheck.h
clang::tidy::bugprone::ReservedIdentifierCheck
Checks for usages of identifiers reserved for use by the implementation.
Definition: ReservedIdentifierCheck.h:33
clang::tidy::cert::NonTrivialTypesLibcMemoryCallsCheck
Flags use of the C standard library functions 'memset', 'memcpy' and 'memcmp' and similar derivatives...
Definition: NonTrivialTypesLibcMemoryCallsCheck.h:23
clang::tidy::cert::CERTModule
Definition: CERTTidyModule.cpp:234
clang::tidy::cert::CERTModule::getModuleOptions
ClangTidyOptions getModuleOptions() override
Gets default options for checks defined in this module.
Definition: CERTTidyModule.cpp:322
clang::tidy::CERTModuleAnchorSource
volatile int CERTModuleAnchorSource
Definition: CERTTidyModule.cpp:342
FloatLoopCounter.h
PostfixOperatorCheck.h
clang::tidy::concurrency::ThreadCanceltypeAsynchronousCheck
Finds pthread_setcanceltype function calls where a thread's cancellation type is set to asynchronous.
Definition: ThreadCanceltypeAsynchronousCheck.h:22
clang::tidy::cert::SetLongJmpCheck
Guards against use of setjmp/longjmp in C++ code.
Definition: SetLongJmpCheck.h:22
clang::tidy::bugprone::UnusedReturnValueCheck
Detects function calls where the return value is unused.
Definition: UnusedReturnValueCheck.h:23
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::cert::CERTModule::addCheckFactories
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override
Implement this function in order to register all CheckFactories belonging to this module.
Definition: CERTTidyModule.cpp:236
clang::tidy::misc::ThrowByValueCatchByReferenceCheck
checks for locations that do not throw by value
Definition: ThrowByValueCatchByReferenceCheck.h:29
DontModifyStdNamespaceCheck.h
clang::tidy::X
static ClangTidyModuleRegistry::Add< altera::AlteraModule > X("altera-module", "Adds Altera FPGA OpenCL lint checks.")
clang::tidy::ClangTidyModule
A clang-tidy module groups a number of ClangTidyChecks and gives them a prefixed name.
Definition: ClangTidyModule.h:85
clang::tidy::ClangTidyCheckFactories::registerCheck
void registerCheck(llvm::StringRef CheckName)
Registers the CheckType with the name Name.
Definition: ClangTidyModule.h:59
VariadicFunctionDefCheck.h