https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/157285
>From d94e0b12914e64a851f5a41cd5bbfb982ed285de Mon Sep 17 00:00:00 2001 From: Victor Baranov <bar.victor.2...@gmail.com> Date: Sat, 6 Sep 2025 19:16:53 +0300 Subject: [PATCH 1/2] [clang-tidy] Add new alias 'bugprone-unchecked-string-to-number-conversion' for 'cert-err34-c' --- .../bugprone/BugproneTidyModule.cpp | 3 ++ .../clang-tidy/bugprone/CMakeLists.txt | 1 + ...ncheckedStringToNumberConversionCheck.cpp} | 14 ++++---- .../UncheckedStringToNumberConversionCheck.h | 35 +++++++++++++++++++ .../clang-tidy/cert/CERTTidyModule.cpp | 6 ++-- .../clang-tidy/cert/CMakeLists.txt | 1 - .../clang-tidy/cert/StrToNumCheck.h | 31 ---------------- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../unchecked-string-to-number-conversion.rst | 31 ++++++++++++++++ .../docs/clang-tidy/checks/cert/err34-c.rst | 28 +++------------ .../docs/clang-tidy/checks/list.rst | 5 +-- .../unchecked-string-to-number-conversion.c} | 34 +++++++++--------- ...unchecked-string-to-number-conversion.cpp} | 14 ++++---- 13 files changed, 118 insertions(+), 89 deletions(-) rename clang-tools-extra/clang-tidy/{cert/StrToNumCheck.cpp => bugprone/UncheckedStringToNumberConversionCheck.cpp} (95%) create mode 100644 clang-tools-extra/clang-tidy/bugprone/UncheckedStringToNumberConversionCheck.h delete mode 100644 clang-tools-extra/clang-tidy/cert/StrToNumCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/bugprone/unchecked-string-to-number-conversion.rst rename clang-tools-extra/test/clang-tidy/checkers/{cert/err34-c.c => bugprone/unchecked-string-to-number-conversion.c} (77%) rename clang-tools-extra/test/clang-tidy/checkers/{cert/err34-c.cpp => bugprone/unchecked-string-to-number-conversion.cpp} (77%) diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index 824ebdfbd00dc..fe261e729539c 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -89,6 +89,7 @@ #include "ThrowKeywordMissingCheck.h" #include "TooSmallLoopVariableCheck.h" #include "UncheckedOptionalAccessCheck.h" +#include "UncheckedStringToNumberConversionCheck.h" #include "UndefinedMemoryManipulationCheck.h" #include "UndelegatedConstructorCheck.h" #include "UnhandledExceptionAtNewCheck.h" @@ -261,6 +262,8 @@ class BugproneModule : public ClangTidyModule { "bugprone-too-small-loop-variable"); CheckFactories.registerCheck<UncheckedOptionalAccessCheck>( "bugprone-unchecked-optional-access"); + CheckFactories.registerCheck<UncheckedStringToNumberConversionCheck>( + "bugprone-unchecked-string-to-number-conversion"); CheckFactories.registerCheck<UndefinedMemoryManipulationCheck>( "bugprone-undefined-memory-manipulation"); CheckFactories.registerCheck<UndelegatedConstructorCheck>( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index 59928e5e47a09..46bc8efd44bc5 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -91,6 +91,7 @@ add_clang_library(clangTidyBugproneModule STATIC ThrowKeywordMissingCheck.cpp TooSmallLoopVariableCheck.cpp UncheckedOptionalAccessCheck.cpp + UncheckedStringToNumberConversionCheck.cpp UndefinedMemoryManipulationCheck.cpp UndelegatedConstructorCheck.cpp UnhandledExceptionAtNewCheck.cpp diff --git a/clang-tools-extra/clang-tidy/cert/StrToNumCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UncheckedStringToNumberConversionCheck.cpp similarity index 95% rename from clang-tools-extra/clang-tidy/cert/StrToNumCheck.cpp rename to clang-tools-extra/clang-tidy/bugprone/UncheckedStringToNumberConversionCheck.cpp index 95536bb1cfdb2..d1e7b895f9a35 100644 --- a/clang-tools-extra/clang-tidy/cert/StrToNumCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/UncheckedStringToNumberConversionCheck.cpp @@ -1,4 +1,4 @@ -//===-- StrToNumCheck.cpp - clang-tidy ------------------------------------===// +//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -#include "StrToNumCheck.h" +#include "UncheckedStringToNumberConversionCheck.h" #include "clang/AST/ASTContext.h" #include "clang/AST/FormatString.h" #include "clang/ASTMatchers/ASTMatchFinder.h" @@ -15,9 +15,10 @@ using namespace clang::ast_matchers; -namespace clang::tidy::cert { +namespace clang::tidy::bugprone { -void StrToNumCheck::registerMatchers(MatchFinder *Finder) { +void UncheckedStringToNumberConversionCheck::registerMatchers( + MatchFinder *Finder) { // Match any function call to the C standard library string conversion // functions that do no error checking. Finder->addMatcher( @@ -176,7 +177,8 @@ static StringRef classifyReplacement(ConversionKind K) { llvm_unreachable("Unknown conversion kind"); } -void StrToNumCheck::check(const MatchFinder::MatchResult &Result) { +void UncheckedStringToNumberConversionCheck::check( + const MatchFinder::MatchResult &Result) { const auto *Call = Result.Nodes.getNodeAs<CallExpr>("expr"); const FunctionDecl *FuncDecl = nullptr; ConversionKind Conversion = ConversionKind::None; @@ -228,4 +230,4 @@ void StrToNumCheck::check(const MatchFinder::MatchResult &Result) { << classifyReplacement(Conversion); } -} // namespace clang::tidy::cert +} // namespace clang::tidy::bugprone diff --git a/clang-tools-extra/clang-tidy/bugprone/UncheckedStringToNumberConversionCheck.h b/clang-tools-extra/clang-tidy/bugprone/UncheckedStringToNumberConversionCheck.h new file mode 100644 index 0000000000000..365b409f8311c --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/UncheckedStringToNumberConversionCheck.h @@ -0,0 +1,35 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_UNCHECKEDSTRINGTONUMBERCONVERSIONCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_UNCHECKEDSTRINGTONUMBERCONVERSIONCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::bugprone { + +/// Guards against use of string conversion functions that do not have +/// reasonable error handling for conversion errors. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/unchecked-string-to-number-conversion.html +class UncheckedStringToNumberConversionCheck : public ClangTidyCheck { +public: + UncheckedStringToNumberConversionCheck(StringRef Name, + ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.CPlusPlus || LangOpts.C99; + } +}; + +} // namespace clang::tidy::bugprone + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_UNCHECKEDSTRINGTONUMBERCONVERSIONCHECK_H diff --git a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp index 66fedabaf3ca6..a0d0ac1007c3e 100644 --- a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp @@ -17,6 +17,7 @@ #include "../bugprone/SizeofExpressionCheck.h" #include "../bugprone/SpuriouslyWakeUpFunctionsCheck.h" #include "../bugprone/SuspiciousMemoryComparisonCheck.h" +#include "../bugprone/UncheckedStringToNumberConversionCheck.h" #include "../bugprone/UnhandledSelfAssignmentCheck.h" #include "../bugprone/UnsafeFunctionsCheck.h" #include "../bugprone/UnusedReturnValueCheck.h" @@ -39,7 +40,6 @@ #include "ProperlySeededRandomGeneratorCheck.h" #include "SetLongJmpCheck.h" #include "StaticObjectExceptionCheck.h" -#include "StrToNumCheck.h" #include "ThrownExceptionTypeCheck.h" #include "VariadicFunctionDefCheck.h" @@ -297,7 +297,9 @@ class CERTModule : public ClangTidyModule { // ERR CheckFactories.registerCheck<bugprone::UnusedReturnValueCheck>( "cert-err33-c"); - CheckFactories.registerCheck<StrToNumCheck>("cert-err34-c"); + CheckFactories + .registerCheck<bugprone::UncheckedStringToNumberConversionCheck>( + "cert-err34-c"); // EXP CheckFactories.registerCheck<bugprone::SuspiciousMemoryComparisonCheck>( "cert-exp42-c"); diff --git a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt index e3187b28399c7..eebbf907cc94e 100644 --- a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt @@ -15,7 +15,6 @@ add_clang_library(clangTidyCERTModule STATIC ProperlySeededRandomGeneratorCheck.cpp SetLongJmpCheck.cpp StaticObjectExceptionCheck.cpp - StrToNumCheck.cpp ThrownExceptionTypeCheck.cpp VariadicFunctionDefCheck.cpp diff --git a/clang-tools-extra/clang-tidy/cert/StrToNumCheck.h b/clang-tools-extra/clang-tidy/cert/StrToNumCheck.h deleted file mode 100644 index 5306bde77f2be..0000000000000 --- a/clang-tools-extra/clang-tidy/cert/StrToNumCheck.h +++ /dev/null @@ -1,31 +0,0 @@ -//===--- StrToNumCheck.h - clang-tidy----------------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_STRTONUMCHECK_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_STRTONUMCHECK_H - -#include "../ClangTidyCheck.h" - -namespace clang::tidy::cert { - -/// Guards against use of string conversion functions that do not have -/// reasonable error handling for conversion errors. -/// -/// For the user-facing documentation see: -/// http://clang.llvm.org/extra/clang-tidy/checks/cert/err34-c.html -class StrToNumCheck : public ClangTidyCheck { -public: - StrToNumCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} - void registerMatchers(ast_matchers::MatchFinder *Finder) override; - void check(const ast_matchers::MatchFinder::MatchResult &Result) override; -}; - -} // namespace clang::tidy::cert - -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_STRTONUMCHECK_H diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 1df8b98232147..e0217cb344614 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -166,6 +166,10 @@ New checks New check aliases ^^^^^^^^^^^^^^^^^ +- New alias :doc:`bugprone-unchecked-string-to-number-conversion + <clang-tidy/checks/bugprone/unchecked-string-to-number-conversion>` to + :doc:`cert-err34-c <clang-tidy/checks/cert/err34-c>` was added. + Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unchecked-string-to-number-conversion.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unchecked-string-to-number-conversion.rst new file mode 100644 index 0000000000000..ff807e6dea5c0 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unchecked-string-to-number-conversion.rst @@ -0,0 +1,31 @@ +.. title:: clang-tidy - bugprone-unchecked-string-to-number-conversion + +bugprone-unchecked-string-to-number-conversion +========================================= + +This check flags calls to string-to-number conversion functions that do not +verify the validity of the conversion, such as ``atoi()`` or ``scanf()``. It +does not flag calls to ``strtol()``, or other, related conversion functions that +do perform better error checking. + +.. code-block:: c + + #include <stdlib.h> + + void func(const char *buff) { + int si; + + if (buff) { + si = atoi(buff); /* 'atoi' used to convert a string to an integer, but function will + not report conversion errors; consider using 'strtol' instead. */ + } else { + /* Handle error */ + } + } + +References +---------- + +This check corresponds to the CERT C Coding Standard rule +`ERR34-C. Detect errors when converting a string to a number +<https://www.securecoding.cert.org/confluence/display/c/ERR34-C.+Detect+errors+when+converting+a+string+to+a+number>`_. diff --git a/clang-tools-extra/docs/clang-tidy/checks/cert/err34-c.rst b/clang-tools-extra/docs/clang-tidy/checks/cert/err34-c.rst index 362aef2098d49..dc3f6329dfb67 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/cert/err34-c.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/cert/err34-c.rst @@ -1,28 +1,10 @@ .. title:: clang-tidy - cert-err34-c +.. meta:: + :http-equiv=refresh: 5;URL=../bugprone/unchecked-string-to-number-conversion.html cert-err34-c ============ -This check flags calls to string-to-number conversion functions that do not -verify the validity of the conversion, such as ``atoi()`` or ``scanf()``. It -does not flag calls to ``strtol()``, or other, related conversion functions that -do perform better error checking. - -.. code-block:: c - - #include <stdlib.h> - - void func(const char *buff) { - int si; - - if (buff) { - si = atoi(buff); /* 'atoi' used to convert a string to an integer, but function will - not report conversion errors; consider using 'strtol' instead. */ - } else { - /* Handle error */ - } - } - -This check corresponds to the CERT C Coding Standard rule -`ERR34-C. Detect errors when converting a string to a number -<https://www.securecoding.cert.org/confluence/display/c/ERR34-C.+Detect+errors+when+converting+a+string+to+a+number>`_. +The cert-err34-c check is an alias, please see +`bugprone-unchecked-string-to-number-conversion <../bugprone/unchecked-string-to-number-conversion.html>`_ +for more information. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index 5e3ffc4f8aca3..e6d6c3e1792f3 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -157,6 +157,7 @@ Clang-Tidy Checks :doc:`bugprone-throw-keyword-missing <bugprone/throw-keyword-missing>`, :doc:`bugprone-too-small-loop-variable <bugprone/too-small-loop-variable>`, :doc:`bugprone-unchecked-optional-access <bugprone/unchecked-optional-access>`, + :doc:`bugprone-unchecked-string-to-number-conversion <bugprone/unchecked-string-to-number-conversion>`, :doc:`bugprone-undefined-memory-manipulation <bugprone/undefined-memory-manipulation>`, :doc:`bugprone-undelegated-constructor <bugprone/undelegated-constructor>`, :doc:`bugprone-unhandled-exception-at-new <bugprone/unhandled-exception-at-new>`, @@ -173,7 +174,6 @@ Clang-Tidy Checks :doc:`cert-dcl58-cpp <cert/dcl58-cpp>`, :doc:`cert-env33-c <cert/env33-c>`, :doc:`cert-err33-c <cert/err33-c>`, - :doc:`cert-err34-c <cert/err34-c>`, :doc:`cert-err52-cpp <cert/err52-cpp>`, :doc:`cert-err58-cpp <cert/err58-cpp>`, :doc:`cert-err60-cpp <cert/err60-cpp>`, @@ -249,12 +249,12 @@ Clang-Tidy Checks :doc:`linuxkernel-must-check-errs <linuxkernel/must-check-errs>`, :doc:`llvm-header-guard <llvm/header-guard>`, :doc:`llvm-include-order <llvm/include-order>`, "Yes" - :doc:`llvm-use-new-mlir-op-builder <llvm/use-new-mlir-op-builder>`, "Yes" :doc:`llvm-namespace-comment <llvm/namespace-comment>`, :doc:`llvm-prefer-isa-or-dyn-cast-in-conditionals <llvm/prefer-isa-or-dyn-cast-in-conditionals>`, "Yes" :doc:`llvm-prefer-register-over-unsigned <llvm/prefer-register-over-unsigned>`, "Yes" :doc:`llvm-prefer-static-over-anonymous-namespace <llvm/prefer-static-over-anonymous-namespace>`, :doc:`llvm-twine-local <llvm/twine-local>`, "Yes" + :doc:`llvm-use-new-mlir-op-builder <llvm/use-new-mlir-op-builder>`, "Yes" :doc:`llvmlibc-callee-namespace <llvmlibc/callee-namespace>`, :doc:`llvmlibc-implementation-in-namespace <llvmlibc/implementation-in-namespace>`, :doc:`llvmlibc-inline-function-decl <llvmlibc/inline-function-decl>`, "Yes" @@ -436,6 +436,7 @@ Check aliases :doc:`cert-dcl54-cpp <cert/dcl54-cpp>`, :doc:`misc-new-delete-overloads <misc/new-delete-overloads>`, :doc:`cert-dcl59-cpp <cert/dcl59-cpp>`, :doc:`google-build-namespaces <google/build-namespaces>`, :doc:`cert-err09-cpp <cert/err09-cpp>`, :doc:`misc-throw-by-value-catch-by-reference <misc/throw-by-value-catch-by-reference>`, + :doc:`cert-err34-c <cert/err34-c>`, :doc:`bugprone-unchecked-string-to-number-conversion <bugprone/unchecked-string-to-number-conversion>`, :doc:`cert-err61-cpp <cert/err61-cpp>`, :doc:`misc-throw-by-value-catch-by-reference <misc/throw-by-value-catch-by-reference>`, :doc:`cert-exp42-c <cert/exp42-c>`, :doc:`bugprone-suspicious-memory-comparison <bugprone/suspicious-memory-comparison>`, :doc:`cert-fio38-c <cert/fio38-c>`, :doc:`misc-non-copyable-objects <misc/non-copyable-objects>`, diff --git a/clang-tools-extra/test/clang-tidy/checkers/cert/err34-c.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unchecked-string-to-number-conversion.c similarity index 77% rename from clang-tools-extra/test/clang-tidy/checkers/cert/err34-c.c rename to clang-tools-extra/test/clang-tidy/checkers/bugprone/unchecked-string-to-number-conversion.c index e2cfc2182b3ef..0546e0c2091d5 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/cert/err34-c.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unchecked-string-to-number-conversion.c @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s cert-err34-c %t -- -- -std=c11 +// RUN: %check_clang_tidy %s bugprone-unchecked-string-to-number-conversion %t typedef __SIZE_TYPE__ size_t; typedef signed ptrdiff_t; @@ -8,9 +8,9 @@ typedef void * FILE; extern FILE *stdin; -extern int fscanf(FILE * restrict stream, const char * restrict format, ...); -extern int scanf(const char * restrict format, ...); -extern int sscanf(const char * restrict s, const char * restrict format, ...); +extern int fscanf(FILE *stream, const char *format, ...); +extern int scanf(const char *format, ...); +extern int sscanf(const char *s, const char *format, ...); extern double atof(const char *nptr); extern int atoi(const char *nptr); @@ -28,23 +28,23 @@ void f1(const char *in) { double d; long double ld; - // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'sscanf' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'sscanf' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead [bugprone-unchecked-string-to-number-conversion] sscanf(in, "%d", &i); - // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'fscanf' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtoll' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'fscanf' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtoll' instead fscanf(stdin, "%lld", &ll); - // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'sscanf' used to convert a string to an unsigned integer value, but function will not report conversion errors; consider using 'strtoul' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'sscanf' used to convert a string to an unsigned integer value, but function will not report conversion errors; consider using 'strtoul' instead sscanf(in, "%u", &ui); - // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'fscanf' used to convert a string to an unsigned integer value, but function will not report conversion errors; consider using 'strtoull' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'fscanf' used to convert a string to an unsigned integer value, but function will not report conversion errors; consider using 'strtoull' instead fscanf(stdin, "%llu", &ull); - // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'scanf' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtoimax' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'scanf' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtoimax' instead scanf("%jd", &im); - // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'fscanf' used to convert a string to an unsigned integer value, but function will not report conversion errors; consider using 'strtoumax' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'fscanf' used to convert a string to an unsigned integer value, but function will not report conversion errors; consider using 'strtoumax' instead fscanf(stdin, "%ju", &uim); - // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'sscanf' used to convert a string to a floating-point value, but function will not report conversion errors; consider using 'strtof' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'sscanf' used to convert a string to a floating-point value, but function will not report conversion errors; consider using 'strtof' instead sscanf(in, "%f", &f); // to float - // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'fscanf' used to convert a string to a floating-point value, but function will not report conversion errors; consider using 'strtod' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'fscanf' used to convert a string to a floating-point value, but function will not report conversion errors; consider using 'strtod' instead fscanf(stdin, "%lg", &d); - // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'sscanf' used to convert a string to a floating-point value, but function will not report conversion errors; consider using 'strtold' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'sscanf' used to convert a string to a floating-point value, but function will not report conversion errors; consider using 'strtold' instead sscanf(in, "%Le", &ld); // These are conversions with other modifiers @@ -70,13 +70,13 @@ void f1(const char *in) { } void f2(const char *in) { - // CHECK-MESSAGES: :[[@LINE+1]]:11: warning: 'atoi' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:11: warning: 'atoi' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead int i = atoi(in); // to int - // CHECK-MESSAGES: :[[@LINE+1]]:12: warning: 'atol' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:12: warning: 'atol' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead long l = atol(in); // to long - // CHECK-MESSAGES: :[[@LINE+1]]:18: warning: 'atoll' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtoll' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:18: warning: 'atoll' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtoll' instead long long ll = atoll(in); // to long long - // CHECK-MESSAGES: :[[@LINE+1]]:14: warning: 'atof' used to convert a string to a floating-point value, but function will not report conversion errors; consider using 'strtod' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:14: warning: 'atof' used to convert a string to a floating-point value, but function will not report conversion errors; consider using 'strtod' instead double d = atof(in); // to double } diff --git a/clang-tools-extra/test/clang-tidy/checkers/cert/err34-c.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unchecked-string-to-number-conversion.cpp similarity index 77% rename from clang-tools-extra/test/clang-tidy/checkers/cert/err34-c.cpp rename to clang-tools-extra/test/clang-tidy/checkers/bugprone/unchecked-string-to-number-conversion.cpp index 1c358bc3fb54c..c3e502113c8b7 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/cert/err34-c.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unchecked-string-to-number-conversion.cpp @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s cert-err34-c %t +// RUN: %check_clang_tidy %s bugprone-unchecked-string-to-number-conversion %t typedef void * FILE; @@ -22,22 +22,22 @@ void f1(const char *in) { int i; long long ll; - // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'sscanf' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'sscanf' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead std::sscanf(in, "%d", &i); - // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'fscanf' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtoll' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'fscanf' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtoll' instead std::fscanf(std::stdin, "%lld", &ll); } void f2(const char *in) { - // CHECK-MESSAGES: :[[@LINE+1]]:11: warning: 'atoi' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:11: warning: 'atoi' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead int i = std::atoi(in); // to int - // CHECK-MESSAGES: :[[@LINE+1]]:12: warning: 'atol' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:12: warning: 'atol' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead long l = std::atol(in); // to long using namespace std; - // CHECK-MESSAGES: :[[@LINE+1]]:18: warning: 'atoll' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtoll' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:18: warning: 'atoll' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtoll' instead long long ll = atoll(in); // to long long - // CHECK-MESSAGES: :[[@LINE+1]]:14: warning: 'atof' used to convert a string to a floating-point value, but function will not report conversion errors; consider using 'strtod' instead [cert-err34-c] + // CHECK-MESSAGES: :[[@LINE+1]]:14: warning: 'atof' used to convert a string to a floating-point value, but function will not report conversion errors; consider using 'strtod' instead double d = atof(in); // to double } >From 5218da96daa74fe25b2aee7a0ac6d61dfa67fd3f Mon Sep 17 00:00:00 2001 From: Victor Baranov <bar.victor.2...@gmail.com> Date: Sat, 6 Sep 2025 21:08:37 +0300 Subject: [PATCH 2/2] fix pr comments --- clang-tools-extra/docs/ReleaseNotes.rst | 6 +++--- .../bugprone/unchecked-string-to-number-conversion.rst | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index e0217cb344614..3a55a0c2565b8 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -166,9 +166,9 @@ New checks New check aliases ^^^^^^^^^^^^^^^^^ -- New alias :doc:`bugprone-unchecked-string-to-number-conversion - <clang-tidy/checks/bugprone/unchecked-string-to-number-conversion>` to - :doc:`cert-err34-c <clang-tidy/checks/cert/err34-c>` was added. +- Renamed :doc:`cert-err34-c <clang-tidy/checks/cert/err34-c>` to + <clang-tidy/checks/bugprone/unchecked-string-to-number-conversion>` keeping + initial check as an alias to the new one. Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unchecked-string-to-number-conversion.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unchecked-string-to-number-conversion.rst index ff807e6dea5c0..c3ea196511367 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unchecked-string-to-number-conversion.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unchecked-string-to-number-conversion.rst @@ -1,7 +1,7 @@ .. title:: clang-tidy - bugprone-unchecked-string-to-number-conversion bugprone-unchecked-string-to-number-conversion -========================================= +============================================== This check flags calls to string-to-number conversion functions that do not verify the validity of the conversion, such as ``atoi()`` or ``scanf()``. It _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits