Author: Rohan Jacob-Rao
Date: 2026-03-06T10:32:51-08:00
New Revision: 05fa39de0ef2e1574f3f4d32b1527cb3d9fdd0ca

URL: 
https://github.com/llvm/llvm-project/commit/05fa39de0ef2e1574f3f4d32b1527cb3d9fdd0ca
DIFF: 
https://github.com/llvm/llvm-project/commit/05fa39de0ef2e1574f3f4d32b1527cb3d9fdd0ca.diff

LOG: [NFC][Clang][UnsafeBufferUsage] Check optional string before access. 
(#184867)

Without this fix, the new test case results in a crash due to the
unchecked optional access.

Added: 
    

Modified: 
    clang/lib/Analysis/FixitUtil.cpp
    clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-parm-unsupported.cpp

Removed: 
    


################################################################################
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


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to