[flang-commits] [PATCH] D158451: [flang] Fix crash from empty -DMACRO= (bug #64837)

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Mon Aug 21 13:28:53 PDT 2023


klausler created this revision.
klausler added a reviewer: clementval.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

Some vector indexing code in the preprocessor fails with empty
tokens or token sequences in predefined macros.

Fixes https://github.com/llvm/llvm-project/issues/64837.


https://reviews.llvm.org/D158451

Files:
  flang/lib/Parser/preprocessor.cpp
  flang/lib/Parser/token-sequence.h


Index: flang/lib/Parser/token-sequence.h
===================================================================
--- flang/lib/Parser/token-sequence.h
+++ flang/lib/Parser/token-sequence.h
@@ -61,11 +61,17 @@
   std::size_t SizeInTokens() const { return start_.size(); }
   std::size_t SizeInChars() const { return char_.size(); }
 
-  CharBlock ToCharBlock() const { return {&char_[0], char_.size()}; }
+  CharBlock ToCharBlock() const {
+    return char_.empty() ? CharBlock{} : CharBlock{&char_[0], char_.size()};
+  }
   std::string ToString() const { return ToCharBlock().ToString(); }
 
   CharBlock TokenAt(std::size_t token) const {
-    return {&char_[start_.at(token)], TokenBytes(token)};
+    if (auto bytes{TokenBytes(token)}) {
+      return {&char_[start_.at(token)], bytes};
+    } else { // char_ could be empty
+      return {};
+    }
   }
   char CharAt(std::size_t j) const { return char_.at(j); }
   CharBlock CurrentOpenToken() const {
Index: flang/lib/Parser/preprocessor.cpp
===================================================================
--- flang/lib/Parser/preprocessor.cpp
+++ flang/lib/Parser/preprocessor.cpp
@@ -296,16 +296,18 @@
     if (!def->isFunctionLike()) {
       bool isRenaming{false};
       if (def->isPredefined()) {
-        std::string name{def->replacement().TokenAt(0).ToString()};
         std::string repl;
-        if (name == "__FILE__") {
-          repl = "\""s +
-              allSources_.GetPath(prescanner.GetCurrentProvenance()) + '"';
-        } else if (name == "__LINE__") {
-          std::string buf;
-          llvm::raw_string_ostream ss{buf};
-          ss << allSources_.GetLineNumber(prescanner.GetCurrentProvenance());
-          repl = ss.str();
+        if (!def->replacement().empty()) {
+          std::string name{def->replacement().TokenAt(0).ToString()};
+          if (name == "__FILE__") {
+            repl = "\""s +
+                allSources_.GetPath(prescanner.GetCurrentProvenance()) + '"';
+          } else if (name == "__LINE__") {
+            std::string buf;
+            llvm::raw_string_ostream ss{buf};
+            ss << allSources_.GetLineNumber(prescanner.GetCurrentProvenance());
+            repl = ss.str();
+          }
         }
         if (!repl.empty()) {
           ProvenanceRange insert{allSources_.AddCompilerInsertion(repl)};


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158451.552118.patch
Type: text/x-patch
Size: 2338 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230821/d95b3b21/attachment.bin>


More information about the flang-commits mailing list