Skip to content

Commit 65f9c08

Browse files
authored
[clang-tidy] Rename 'cert-err34-c' to 'bugprone-unchecked-string-to-number-conversion' (#157285)
Part of the work in #157287. Closes #157302.
1 parent ad49111 commit 65f9c08

File tree

13 files changed

+118
-88
lines changed

13 files changed

+118
-88
lines changed

clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
#include "ThrowKeywordMissingCheck.h"
9090
#include "TooSmallLoopVariableCheck.h"
9191
#include "UncheckedOptionalAccessCheck.h"
92+
#include "UncheckedStringToNumberConversionCheck.h"
9293
#include "UndefinedMemoryManipulationCheck.h"
9394
#include "UndelegatedConstructorCheck.h"
9495
#include "UnhandledExceptionAtNewCheck.h"
@@ -261,6 +262,8 @@ class BugproneModule : public ClangTidyModule {
261262
"bugprone-too-small-loop-variable");
262263
CheckFactories.registerCheck<UncheckedOptionalAccessCheck>(
263264
"bugprone-unchecked-optional-access");
265+
CheckFactories.registerCheck<UncheckedStringToNumberConversionCheck>(
266+
"bugprone-unchecked-string-to-number-conversion");
264267
CheckFactories.registerCheck<UndefinedMemoryManipulationCheck>(
265268
"bugprone-undefined-memory-manipulation");
266269
CheckFactories.registerCheck<UndelegatedConstructorCheck>(

clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ add_clang_library(clangTidyBugproneModule STATIC
9191
ThrowKeywordMissingCheck.cpp
9292
TooSmallLoopVariableCheck.cpp
9393
UncheckedOptionalAccessCheck.cpp
94+
UncheckedStringToNumberConversionCheck.cpp
9495
UndefinedMemoryManipulationCheck.cpp
9596
UndelegatedConstructorCheck.cpp
9697
UnhandledExceptionAtNewCheck.cpp

clang-tools-extra/clang-tidy/cert/StrToNumCheck.cpp renamed to clang-tools-extra/clang-tidy/bugprone/UncheckedStringToNumberConversionCheck.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
//===-- StrToNumCheck.cpp - clang-tidy ------------------------------------===//
1+
//===----------------------------------------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "StrToNumCheck.h"
9+
#include "UncheckedStringToNumberConversionCheck.h"
1010
#include "clang/AST/ASTContext.h"
1111
#include "clang/AST/FormatString.h"
1212
#include "clang/ASTMatchers/ASTMatchFinder.h"
@@ -15,9 +15,10 @@
1515

1616
using namespace clang::ast_matchers;
1717

18-
namespace clang::tidy::cert {
18+
namespace clang::tidy::bugprone {
1919

20-
void StrToNumCheck::registerMatchers(MatchFinder *Finder) {
20+
void UncheckedStringToNumberConversionCheck::registerMatchers(
21+
MatchFinder *Finder) {
2122
// Match any function call to the C standard library string conversion
2223
// functions that do no error checking.
2324
Finder->addMatcher(
@@ -176,7 +177,8 @@ static StringRef classifyReplacement(ConversionKind K) {
176177
llvm_unreachable("Unknown conversion kind");
177178
}
178179

179-
void StrToNumCheck::check(const MatchFinder::MatchResult &Result) {
180+
void UncheckedStringToNumberConversionCheck::check(
181+
const MatchFinder::MatchResult &Result) {
180182
const auto *Call = Result.Nodes.getNodeAs<CallExpr>("expr");
181183
const FunctionDecl *FuncDecl = nullptr;
182184
ConversionKind Conversion = ConversionKind::None;
@@ -228,4 +230,4 @@ void StrToNumCheck::check(const MatchFinder::MatchResult &Result) {
228230
<< classifyReplacement(Conversion);
229231
}
230232

231-
} // namespace clang::tidy::cert
233+
} // namespace clang::tidy::bugprone
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_UNCHECKEDSTRINGTONUMBERCONVERSIONCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_UNCHECKEDSTRINGTONUMBERCONVERSIONCHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang::tidy::bugprone {
15+
16+
/// Guards against use of string conversion functions that do not have
17+
/// reasonable error handling for conversion errors.
18+
///
19+
/// For the user-facing documentation see:
20+
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/unchecked-string-to-number-conversion.html
21+
class UncheckedStringToNumberConversionCheck : public ClangTidyCheck {
22+
public:
23+
UncheckedStringToNumberConversionCheck(StringRef Name,
24+
ClangTidyContext *Context)
25+
: ClangTidyCheck(Name, Context) {}
26+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
27+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
28+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
29+
return LangOpts.CPlusPlus || LangOpts.C99;
30+
}
31+
};
32+
33+
} // namespace clang::tidy::bugprone
34+
35+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_UNCHECKEDSTRINGTONUMBERCONVERSIONCHECK_H

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "../bugprone/SizeofExpressionCheck.h"
1818
#include "../bugprone/SpuriouslyWakeUpFunctionsCheck.h"
1919
#include "../bugprone/SuspiciousMemoryComparisonCheck.h"
20+
#include "../bugprone/UncheckedStringToNumberConversionCheck.h"
2021
#include "../bugprone/UnhandledSelfAssignmentCheck.h"
2122
#include "../bugprone/UnsafeFunctionsCheck.h"
2223
#include "../bugprone/UnusedReturnValueCheck.h"
@@ -39,7 +40,6 @@
3940
#include "ProperlySeededRandomGeneratorCheck.h"
4041
#include "SetLongJmpCheck.h"
4142
#include "StaticObjectExceptionCheck.h"
42-
#include "StrToNumCheck.h"
4343
#include "ThrownExceptionTypeCheck.h"
4444
#include "VariadicFunctionDefCheck.h"
4545

@@ -297,7 +297,9 @@ class CERTModule : public ClangTidyModule {
297297
// ERR
298298
CheckFactories.registerCheck<bugprone::UnusedReturnValueCheck>(
299299
"cert-err33-c");
300-
CheckFactories.registerCheck<StrToNumCheck>("cert-err34-c");
300+
CheckFactories
301+
.registerCheck<bugprone::UncheckedStringToNumberConversionCheck>(
302+
"cert-err34-c");
301303
// EXP
302304
CheckFactories.registerCheck<bugprone::SuspiciousMemoryComparisonCheck>(
303305
"cert-exp42-c");

clang-tools-extra/clang-tidy/cert/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ add_clang_library(clangTidyCERTModule STATIC
1515
ProperlySeededRandomGeneratorCheck.cpp
1616
SetLongJmpCheck.cpp
1717
StaticObjectExceptionCheck.cpp
18-
StrToNumCheck.cpp
1918
ThrownExceptionTypeCheck.cpp
2019
VariadicFunctionDefCheck.cpp
2120

clang-tools-extra/clang-tidy/cert/StrToNumCheck.h

Lines changed: 0 additions & 31 deletions
This file was deleted.

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ New checks
176176
New check aliases
177177
^^^^^^^^^^^^^^^^^
178178

179+
- Renamed :doc:`cert-err34-c <clang-tidy/checks/cert/err34-c>` to
180+
:doc:`bugprone-unchecked-string-to-number-conversion
181+
<clang-tidy/checks/bugprone/unchecked-string-to-number-conversion>`
182+
keeping initial check as an alias to the new one.
183+
179184
Changes in existing checks
180185
^^^^^^^^^^^^^^^^^^^^^^^^^^
181186

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
.. title:: clang-tidy - bugprone-unchecked-string-to-number-conversion
2+
3+
bugprone-unchecked-string-to-number-conversion
4+
==============================================
5+
6+
This check flags calls to string-to-number conversion functions that do not
7+
verify the validity of the conversion, such as ``atoi()`` or ``scanf()``. It
8+
does not flag calls to ``strtol()``, or other, related conversion functions that
9+
do perform better error checking.
10+
11+
.. code-block:: c
12+
13+
#include <stdlib.h>
14+
15+
void func(const char *buff) {
16+
int si;
17+
18+
if (buff) {
19+
si = atoi(buff); /* 'atoi' used to convert a string to an integer, but function will
20+
not report conversion errors; consider using 'strtol' instead. */
21+
} else {
22+
/* Handle error */
23+
}
24+
}
25+
26+
References
27+
----------
28+
29+
This check corresponds to the CERT C Coding Standard rule
30+
`ERR34-C. Detect errors when converting a string to a number
31+
<https://www.securecoding.cert.org/confluence/display/c/ERR34-C.+Detect+errors+when+converting+a+string+to+a+number>`_.
Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
11
.. title:: clang-tidy - cert-err34-c
2+
.. meta::
3+
:http-equiv=refresh: 5;URL=../bugprone/unchecked-string-to-number-conversion.html
24

35
cert-err34-c
46
============
57

6-
This check flags calls to string-to-number conversion functions that do not
7-
verify the validity of the conversion, such as ``atoi()`` or ``scanf()``. It
8-
does not flag calls to ``strtol()``, or other, related conversion functions that
9-
do perform better error checking.
10-
11-
.. code-block:: c
12-
13-
#include <stdlib.h>
14-
15-
void func(const char *buff) {
16-
int si;
17-
18-
if (buff) {
19-
si = atoi(buff); /* 'atoi' used to convert a string to an integer, but function will
20-
not report conversion errors; consider using 'strtol' instead. */
21-
} else {
22-
/* Handle error */
23-
}
24-
}
25-
26-
This check corresponds to the CERT C Coding Standard rule
27-
`ERR34-C. Detect errors when converting a string to a number
28-
<https://www.securecoding.cert.org/confluence/display/c/ERR34-C.+Detect+errors+when+converting+a+string+to+a+number>`_.
8+
The cert-err34-c check is an alias, please see
9+
`bugprone-unchecked-string-to-number-conversion <../bugprone/unchecked-string-to-number-conversion.html>`_
10+
for more information.

0 commit comments

Comments
 (0)