Skip to content

[flang] Avoid forming a reference from null pointer #84787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 11, 2024

Conversation

kparzysz
Copy link
Contributor

Doing so is an undefined behavior.

This was detected by the undefined behavior sanitizer.

Doing so is an undefined behavior.

This was detected by the undefined behavior sanitizer.
@kparzysz kparzysz requested review from luporl and klausler March 11, 2024 16:25
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:parser labels Mar 11, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 11, 2024

@llvm/pr-subscribers-flang-parser

Author: Krzysztof Parzyszek (kparzysz)

Changes

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 --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) {

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use braced initialization in Flang front-end code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@kparzysz kparzysz merged commit 1def98d into llvm:main Mar 11, 2024
@kparzysz kparzysz deleted the users/kparzysz/ubsan-03 branch March 11, 2024 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:parser flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants