Skip to content

Commit 1def98d

Browse files
authored
[flang] Avoid forming a reference from null pointer (#84787)
Doing so is an undefined behavior. This was detected by the undefined behavior sanitizer.
1 parent a70d729 commit 1def98d

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

flang/lib/Parser/token-sequence.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ void TokenSequence::Put(
136136
}
137137

138138
void TokenSequence::Put(const CharBlock &t, Provenance provenance) {
139-
Put(&t[0], t.size(), provenance);
139+
// Avoid t[0] if t is empty: it would create a reference to nullptr,
140+
// which is UB.
141+
const char *addr{t.size() ? &t[0] : nullptr};
142+
Put(addr, t.size(), provenance);
140143
}
141144

142145
void TokenSequence::Put(const std::string &s, Provenance provenance) {

0 commit comments

Comments
 (0)