[flang-commits] [flang] [flang] Avoid forming a reference from null pointer (PR #84787)

via flang-commits flang-commits at lists.llvm.org
Mon Mar 11 09:26:24 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-parser

Author: Krzysztof Parzyszek (kparzysz)

<details>
<summary>Changes</summary>

Doing so is an undefined behavior.

This was detected by the undefined behavior sanitizer.

---
Full diff: https://github.com/llvm/llvm-project/pull/84787.diff


1 Files Affected:

- (modified) flang/lib/Parser/token-sequence.cpp (+4-1) 


``````````diff
diff --git a/flang/lib/Parser/token-sequence.cpp b/flang/lib/Parser/token-sequence.cpp
index c5a630c471d16e..a53205b35f0761 100644
--- a/flang/lib/Parser/token-sequence.cpp
+++ b/flang/lib/Parser/token-sequence.cpp
@@ -136,7 +136,10 @@ void TokenSequence::Put(
 }
 
 void TokenSequence::Put(const CharBlock &t, Provenance provenance) {
-  Put(&t[0], t.size(), provenance);
+  // Avoid t[0] if t is empty: it would create a reference to nullptr,
+  // which is UB.
+  const char *addr = t.size() ? &t[0] : nullptr;
+  Put(addr, t.size(), provenance);
 }
 
 void TokenSequence::Put(const std::string &s, Provenance provenance) {

``````````

</details>


https://github.com/llvm/llvm-project/pull/84787


More information about the flang-commits mailing list