[clang-tools-extra] [clang-tidy] Avoid overflow when dumping unsigned integer values (PR #85060)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 8 08:05:43 PDT 2024
https://github.com/ealcdan updated https://github.com/llvm/llvm-project/pull/85060
>From 12a841c53b5a42fd1954fd81a3aad76c9ccbd45d Mon Sep 17 00:00:00 2001
From: Daniel Alcaide Nombela <daniel.alcaide.nombela at ericsson.com>
Date: Wed, 13 Mar 2024 11:28:34 +0100
Subject: [PATCH] [clang-tidy] Avoid overflow when dumping unsigned integer
values
Some options take the maximum unsigned integer value as default, but
they are being dumped to a string as integers. This makes -dump-config
write invalid '-1' values for these options. This change fixes this
issue by using utostr if the option is unsigned.
Change-Id: I551e6bc616071cf7aa10a8c0220d2076ed3d40e6
---
clang-tools-extra/clang-tidy/ClangTidyCheck.cpp | 6 ++++++
clang-tools-extra/clang-tidy/ClangTidyCheck.h | 11 +++++++++--
2 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/ClangTidyCheck.cpp b/clang-tools-extra/clang-tidy/ClangTidyCheck.cpp
index 3e926236adb451..710b361e16c0a7 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidyCheck.cpp
@@ -139,6 +139,12 @@ void ClangTidyCheck::OptionsView::storeInt(ClangTidyOptions::OptionMap &Options,
store(Options, LocalName, llvm::itostr(Value));
}
+void ClangTidyCheck::OptionsView::storeUnsigned(
+ ClangTidyOptions::OptionMap &Options, StringRef LocalName,
+ uint64_t Value) const {
+ store(Options, LocalName, llvm::utostr(Value));
+}
+
template <>
void ClangTidyCheck::OptionsView::store<bool>(
ClangTidyOptions::OptionMap &Options, StringRef LocalName,
diff --git a/clang-tools-extra/clang-tidy/ClangTidyCheck.h b/clang-tools-extra/clang-tidy/ClangTidyCheck.h
index 656a2f008f6e0e..31cd7b529a49ce 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyCheck.h
+++ b/clang-tools-extra/clang-tidy/ClangTidyCheck.h
@@ -411,7 +411,10 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
std::enable_if_t<std::is_integral_v<T>>
store(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
T Value) const {
- storeInt(Options, LocalName, Value);
+ if(constexpr(std::is_signed_v<T>))
+ storeInt(Options, LocalName, Value);
+ else
+ storeUnsigned(Options, LocalName, Value);
}
/// Stores an option with the check-local name \p LocalName with
@@ -421,8 +424,10 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
std::enable_if_t<std::is_integral_v<T>>
store(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
std::optional<T> Value) const {
- if (Value)
+ if (Value && constexpr(std::is_signed_v<T>))
storeInt(Options, LocalName, *Value);
+ else if (Value)
+ storeUnsigned(Options, LocalName, *Value);
else
store(Options, LocalName, "none");
}
@@ -470,6 +475,8 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
void storeInt(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
int64_t Value) const;
+ void storeUnsigned(ClangTidyOptions::OptionMap &Options,
+ StringRef LocalName, uint64_t Value) const;
std::string NamePrefix;
const ClangTidyOptions::OptionMap &CheckOptions;
More information about the cfe-commits
mailing list