clang-tools 23.0.0git
UnsafeFunctionsCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Analysis/AnnexKDetection.h"
14#include "clang/Lex/PPCallbacks.h"
15#include "clang/Lex/Preprocessor.h"
16#include <cassert>
17
18using namespace clang::ast_matchers;
19using namespace llvm;
20
21namespace clang::tidy::bugprone {
22
23static constexpr StringRef OptionNameCustomFunctions = "CustomFunctions";
24static constexpr StringRef OptionNameReportDefaultFunctions =
25 "ReportDefaultFunctions";
26static constexpr StringRef OptionNameReportMoreUnsafeFunctions =
27 "ReportMoreUnsafeFunctions";
28
29static constexpr StringRef FunctionNamesWithAnnexKReplacementId =
30 "FunctionNamesWithAnnexKReplacement";
31static constexpr StringRef FunctionNamesId = "FunctionsNames";
32static constexpr StringRef AdditionalFunctionNamesId =
33 "AdditionalFunctionsNames";
34static constexpr StringRef CustomFunctionNamesId = "CustomFunctionNames";
35static constexpr StringRef DeclRefId = "DRE";
36
37static std::optional<std::string>
38getAnnexKReplacementFor(StringRef FunctionName) {
39 return StringSwitch<std::string>(FunctionName)
40 .Case("strlen", "strnlen_s")
41 .Case("wcslen", "wcsnlen_s")
42 .Default((Twine{FunctionName} + "_s").str());
43}
44
45static StringRef getReplacementFor(StringRef FunctionName,
46 bool IsAnnexKAvailable) {
47 if (IsAnnexKAvailable) {
48 // Try to find a better replacement from Annex K first.
49 StringRef AnnexKReplacementFunction =
50 StringSwitch<StringRef>(FunctionName)
51 .Cases({"asctime", "asctime_r"}, "asctime_s")
52 .Case("gets", "gets_s")
53 .Default({});
54 if (!AnnexKReplacementFunction.empty())
55 return AnnexKReplacementFunction;
56 }
57
58 // FIXME: Some of these functions are available in C++ under "std::", and
59 // should be matched and suggested.
60 return StringSwitch<StringRef>(FunctionName)
61 .Cases({"asctime", "asctime_r"}, "strftime")
62 .Case("gets", "fgets")
63 .Case("rewind", "fseek")
64 .Case("setbuf", "setvbuf")
65 .Case("get_temporary_buffer", "operator new[]");
66}
67
68static StringRef getReplacementForAdditional(StringRef FunctionName,
69 bool IsAnnexKAvailable) {
70 if (IsAnnexKAvailable) {
71 // Try to find a better replacement from Annex K first.
72 StringRef AnnexKReplacementFunction = StringSwitch<StringRef>(FunctionName)
73 .Case("bcopy", "memcpy_s")
74 .Case("bzero", "memset_s")
75 .Default({});
76
77 if (!AnnexKReplacementFunction.empty())
78 return AnnexKReplacementFunction;
79 }
80
81 return StringSwitch<StringRef>(FunctionName)
82 .Case("bcmp", "memcmp")
83 .Case("bcopy", "memcpy")
84 .Case("bzero", "memset")
85 .Case("getpw", "getpwuid")
86 .Case("vfork", "posix_spawn");
87}
88
89/// \returns The rationale for replacing the function \p FunctionName with the
90/// safer alternative.
91static StringRef getRationaleFor(StringRef FunctionName) {
92 return StringSwitch<StringRef>(FunctionName)
93 .Cases({"asctime", "asctime_r", "ctime"},
94 "is not bounds-checking and non-reentrant")
95 .Cases({"bcmp", "bcopy", "bzero"}, "is deprecated")
96 .Cases({"fopen", "freopen"}, "has no exclusive access to the opened file")
97 .Case("gets", "is insecure, was deprecated and removed in C11 and C++14")
98 .Case("getpw", "is dangerous as it may overflow the provided buffer")
99 .Cases({"rewind", "setbuf"}, "has no error detection")
100 .Case("vfork", "is insecure as it can lead to denial of service "
101 "situations in the parent process")
102 .Case("get_temporary_buffer", "returns uninitialized memory without "
103 "performance advantages, was deprecated in "
104 "C++17 and removed in C++20")
105 .Default("is not bounds-checking");
106}
107
108/// Calculates whether Annex K is available for the current translation unit
109/// based on the macro definitions and the language options.
110///
111/// The result is cached and saved in \p CacheVar.
112static bool isAnnexKAvailable(std::optional<bool> &CacheVar, Preprocessor *PP,
113 const LangOptions &LO) {
114 if (CacheVar.has_value())
115 return *CacheVar;
116
117 CacheVar = analysis::isAnnexKAvailable(PP, LO);
118 return CacheVar.value();
119}
120
121static std::vector<UnsafeFunctionsCheck::CheckedFunction>
122parseCheckedFunctions(StringRef Option, ClangTidyContext *Context) {
123 const std::vector<StringRef> Functions =
125 std::vector<UnsafeFunctionsCheck::CheckedFunction> Result;
126 Result.reserve(Functions.size());
127
128 for (const StringRef Function : Functions) {
129 if (Function.empty())
130 continue;
131
132 const auto [Name, Rest] = Function.split(',');
133 const auto [Replacement, Reason] = Rest.split(',');
134
135 if (Name.trim().empty()) {
136 Context->configurationDiag("invalid configuration value for option '%0'; "
137 "expected the name of an unsafe function")
139 continue;
140 }
141
142 Result.push_back(
143 {Name.trim().str(),
145 Replacement.trim().str(), Reason.trim().str()});
146 }
147
148 return Result;
149}
150
151static std::string serializeCheckedFunctions(
152 const std::vector<UnsafeFunctionsCheck::CheckedFunction> &Functions) {
153 std::vector<std::string> Result;
154 Result.reserve(Functions.size());
155
156 for (const auto &Entry : Functions)
157 if (Entry.Reason.empty())
158 Result.push_back(Entry.Name + "," + Entry.Replacement);
159 else
160 Result.push_back(Entry.Name + "," + Entry.Replacement + "," +
161 Entry.Reason);
162
163 return llvm::join(Result, ";");
164}
165
167 ClangTidyContext *Context)
168 : ClangTidyCheck(Name, Context),
169 CustomFunctions(parseCheckedFunctions(
170 Options.get(OptionNameCustomFunctions, ""), Context)),
171 ReportDefaultFunctions(
172 Options.get(OptionNameReportDefaultFunctions, true)),
173 ReportMoreUnsafeFunctions(
174 Options.get(OptionNameReportMoreUnsafeFunctions, true)) {}
175
177 Options.store(Opts, OptionNameCustomFunctions,
178 serializeCheckedFunctions(CustomFunctions));
179 Options.store(Opts, OptionNameReportDefaultFunctions, ReportDefaultFunctions);
180 Options.store(Opts, OptionNameReportMoreUnsafeFunctions,
181 ReportMoreUnsafeFunctions);
182}
183
184void UnsafeFunctionsCheck::registerMatchers(MatchFinder *Finder) {
185 if (ReportDefaultFunctions) {
186 if (getLangOpts().C11) {
187 // Matching functions with safe replacements only in Annex K.
188 auto FunctionNamesWithAnnexKReplacementMatcher = hasAnyName(
189 "::bsearch", "::ctime", "::fopen", "::fprintf", "::freopen",
190 "::fscanf", "::fwprintf", "::fwscanf", "::getenv", "::gmtime",
191 "::localtime", "::mbsrtowcs", "::mbstowcs", "::memcpy", "::memmove",
192 "::memset", "::printf", "::qsort", "::scanf", "::snprintf",
193 "::sprintf", "::sscanf", "::strcat", "::strcpy", "::strerror",
194 "::strlen", "::strncat", "::strncpy", "::strtok", "::swprintf",
195 "::swscanf", "::vfprintf", "::vfscanf", "::vfwprintf", "::vfwscanf",
196 "::vprintf", "::vscanf", "::vsnprintf", "::vsprintf", "::vsscanf",
197 "::vswprintf", "::vswscanf", "::vwprintf", "::vwscanf", "::wcrtomb",
198 "::wcscat", "::wcscpy", "::wcslen", "::wcsncat", "::wcsncpy",
199 "::wcsrtombs", "::wcstok", "::wcstombs", "::wctomb", "::wmemcpy",
200 "::wmemmove", "::wprintf", "::wscanf");
201 Finder->addMatcher(
202 declRefExpr(to(functionDecl(FunctionNamesWithAnnexKReplacementMatcher)
204 .bind(DeclRefId),
205 this);
206 }
207
208 // Matching functions with replacements without Annex K.
209 auto FunctionNamesMatcher =
210 hasAnyName("::asctime", "asctime_r", "::gets", "::rewind", "::setbuf",
211 "::std::get_temporary_buffer");
212 Finder->addMatcher(
213 declRefExpr(
214 to(functionDecl(FunctionNamesMatcher).bind(FunctionNamesId)))
215 .bind(DeclRefId),
216 this);
217
218 if (ReportMoreUnsafeFunctions) {
219 // Matching functions with replacements without Annex K, at user request.
220 auto AdditionalFunctionNamesMatcher =
221 hasAnyName("::bcmp", "::bcopy", "::bzero", "::getpw", "::vfork");
222 Finder->addMatcher(
223 declRefExpr(to(functionDecl(AdditionalFunctionNamesMatcher)
225 .bind(DeclRefId),
226 this);
227 }
228 }
229
230 if (!CustomFunctions.empty()) {
231 std::vector<llvm::StringRef> FunctionNames;
232 FunctionNames.reserve(CustomFunctions.size());
233
234 for (const auto &Entry : CustomFunctions)
235 FunctionNames.emplace_back(Entry.Name);
236
237 auto CustomFunctionsMatcher =
239
240 Finder->addMatcher(declRefExpr(to(functionDecl(CustomFunctionsMatcher)
241 .bind(CustomFunctionNamesId)))
242 .bind(DeclRefId),
243 this);
244 // C++ member calls do not contain a DeclRefExpr to the function decl.
245 // Instead, they contain a MemberExpr that refers to the decl.
246 Finder->addMatcher(memberExpr(member(functionDecl(CustomFunctionsMatcher)
247 .bind(CustomFunctionNamesId)))
248 .bind(DeclRefId),
249 this);
250 }
251}
252
253void UnsafeFunctionsCheck::check(const MatchFinder::MatchResult &Result) {
254 const Expr *SourceExpr = nullptr;
255 const FunctionDecl *FuncDecl = nullptr;
256
257 if (const auto *DeclRef = Result.Nodes.getNodeAs<DeclRefExpr>(DeclRefId)) {
258 SourceExpr = DeclRef;
259 FuncDecl = cast<FunctionDecl>(DeclRef->getDecl());
260 } else if (const auto *Member =
261 Result.Nodes.getNodeAs<MemberExpr>(DeclRefId)) {
262 SourceExpr = Member;
263 FuncDecl = cast<FunctionDecl>(Member->getMemberDecl());
264 } else {
265 llvm_unreachable("No valid matched node in check()");
266 return;
267 }
268
269 assert(SourceExpr && FuncDecl && "No valid matched node in check()");
270
271 // Only one of these are matched at a time.
272 const auto *AnnexK = Result.Nodes.getNodeAs<FunctionDecl>(
274 const auto *Normal = Result.Nodes.getNodeAs<FunctionDecl>(FunctionNamesId);
275 const auto *Additional =
276 Result.Nodes.getNodeAs<FunctionDecl>(AdditionalFunctionNamesId);
277 const auto *Custom =
278 Result.Nodes.getNodeAs<FunctionDecl>(CustomFunctionNamesId);
279 assert((AnnexK || Normal || Additional || Custom) &&
280 "No valid match category.");
281
282 bool AnnexKIsAvailable =
283 isAnnexKAvailable(IsAnnexKAvailable, PP, getLangOpts());
284 StringRef FunctionName = FuncDecl->getName();
285
286 if (Custom) {
287 for (const auto &Entry : CustomFunctions) {
288 if (Entry.Pattern.match(*FuncDecl)) {
289 StringRef Reason =
290 Entry.Reason.empty() ? "is marked as unsafe" : Entry.Reason.c_str();
291
292 // Omit the replacement, when a fully-custom reason is given.
293 if (Reason.consume_front(">")) {
294 diag(SourceExpr->getExprLoc(), "function %0 %1")
295 << FuncDecl << Reason.trim() << SourceExpr->getSourceRange();
296 // Do not recommend a replacement when it is not present.
297 } else if (Entry.Replacement.empty()) {
298 diag(SourceExpr->getExprLoc(),
299 "function %0 %1; it should not be used")
300 << FuncDecl << Reason << Entry.Replacement
301 << SourceExpr->getSourceRange();
302 // Otherwise, emit the replacement.
303 } else {
304 diag(SourceExpr->getExprLoc(),
305 "function %0 %1; '%2' should be used instead")
306 << FuncDecl << Reason << Entry.Replacement
307 << SourceExpr->getSourceRange();
308 }
309
310 return;
311 }
312 }
313
314 llvm_unreachable("No custom function was matched.");
315 return;
316 }
317
318 const std::optional<std::string> ReplacementFunctionName =
319 [&]() -> std::optional<std::string> {
320 if (AnnexK) {
321 if (AnnexKIsAvailable)
322 return getAnnexKReplacementFor(FunctionName);
323 return std::nullopt;
324 }
325
326 if (Normal)
327 return getReplacementFor(FunctionName, AnnexKIsAvailable).str();
328
329 if (Additional)
330 return getReplacementForAdditional(FunctionName, AnnexKIsAvailable).str();
331
332 llvm_unreachable("Unhandled match category");
333 }();
334 if (!ReplacementFunctionName)
335 return;
336
337 diag(SourceExpr->getExprLoc(), "function %0 %1; '%2' should be used instead")
338 << FuncDecl << getRationaleFor(FunctionName)
339 << ReplacementFunctionName.value() << SourceExpr->getSourceRange();
340}
341
343 const SourceManager &SM, Preprocessor *PP,
344 Preprocessor * /*ModuleExpanderPP*/) {
345 this->PP = PP;
346}
347
349 this->PP = nullptr;
350 IsAnnexKAvailable.reset();
351}
352
353} // namespace clang::tidy::bugprone
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder configurationDiag(StringRef Message, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Report any errors to do with reading the configuration using this method.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
UnsafeFunctionsCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
static constexpr StringRef OptionNameReportMoreUnsafeFunctions
static StringRef getReplacementForAdditional(StringRef FunctionName, bool IsAnnexKAvailable)
static constexpr StringRef FunctionNamesId
static StringRef getRationaleFor(StringRef FunctionName)
static constexpr StringRef OptionNameCustomFunctions
static bool isAnnexKAvailable(std::optional< bool > &CacheVar, Preprocessor *PP, const LangOptions &LO)
Calculates whether Annex K is available for the current translation unit based on the macro definitio...
static std::optional< std::string > getAnnexKReplacementFor(StringRef FunctionName)
static StringRef getReplacementFor(StringRef FunctionName, bool IsAnnexKAvailable)
static constexpr StringRef AdditionalFunctionNamesId
static std::vector< UnsafeFunctionsCheck::CheckedFunction > parseCheckedFunctions(StringRef Option, ClangTidyContext *Context)
static constexpr StringRef CustomFunctionNamesId
static constexpr StringRef DeclRefId
static constexpr StringRef FunctionNamesWithAnnexKReplacementId
static constexpr StringRef OptionNameReportDefaultFunctions
static std::string serializeCheckedFunctions(const std::vector< UnsafeFunctionsCheck::CheckedFunction > &Functions)
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedRegexName(llvm::ArrayRef< StringRef > NameList)
std::vector< StringRef > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
Some operations such as code completion produce a set of candidates.
Definition Generators.h:145
llvm::StringMap< ClangTidyValue > OptionMap
static constexpr const char FuncDecl[]