[clang] [NFC][Clang][FixitUtil] Check optional value before dereference. (PR #184867)
Rohan Jacob-Rao via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 5 16:54:59 PST 2026
https://github.com/rohanjr updated https://github.com/llvm/llvm-project/pull/184867
>From 4345ae2cf132909f9138e75ba8bbb41ad45edbf5 Mon Sep 17 00:00:00 2001
From: Rohan Jacob-Rao <rohanjr at google.com>
Date: Thu, 5 Mar 2026 19:51:15 +0000
Subject: [PATCH] [NFC][Clang][UnsafeBufferUsage] Check optional string before
access.
Without this fix, the new test case results in a crash due to the
unchecked optional access.
---
clang/lib/Analysis/FixitUtil.cpp | 8 ++++++--
.../warn-unsafe-buffer-usage-fixits-parm-unsupported.cpp | 8 ++++++++
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Analysis/FixitUtil.cpp b/clang/lib/Analysis/FixitUtil.cpp
index 4ac3f3acd8c62..ec1924f9c3e10 100644
--- a/clang/lib/Analysis/FixitUtil.cpp
+++ b/clang/lib/Analysis/FixitUtil.cpp
@@ -89,8 +89,12 @@ clang::getPointeeTypeText(const DeclaratorDecl *VD, const SourceManager &SM,
// `PteTy` via source ranges.
*QualifiersToAppend = PteTy.getQualifiers();
}
- return getRangeText({PteTyLoc.getBeginLoc(), PteEndOfTokenLoc}, SM, LangOpts)
- ->str();
+
+ std::optional<StringRef> RangeText =
+ getRangeText({PteTyLoc.getBeginLoc(), PteEndOfTokenLoc}, SM, LangOpts);
+ if (!RangeText)
+ return std::nullopt;
+ return RangeText->str();
}
// returns text of pointee to pointee (T*&)
diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-parm-unsupported.cpp b/clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-parm-unsupported.cpp
index 0c80da63f8291..84a36dd32fb07 100644
--- a/clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-parm-unsupported.cpp
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-parm-unsupported.cpp
@@ -3,6 +3,7 @@
typedef int * TYPEDEF_PTR;
#define MACRO_PTR int*
+#define MACRO_PTR_TO_CONST const int*
// We CANNOT fix a pointer whose type is defined in a typedef or a
// macro. Because if the typedef is changed after the fix, the fix
@@ -20,6 +21,13 @@ void macroPointer(MACRO_PTR p) { // expected-warning{{'p' is an unsafe pointer
}
}
+// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE+2]]
+void macroPointer2(const int *p) {
+ MACRO_PTR_TO_CONST *q = &p; // expected-warning{{'q' is an unsafe pointer used for buffer access}}
+ if (++q) { // expected-note{{used in pointer arithmetic here}}
+ }
+}
+
// The analysis requires accurate source location informations from
// `TypeLoc`s of types of variable (parameter) declarations in order
// to generate fix-its for them. But those information is not always
More information about the cfe-commits
mailing list