[llvm] [RISCV] Detect empty extension name after parsing MajorVersion in parseNormalizedArchString. (PR #90790)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Wed May 1 15:16:11 PDT 2024


https://github.com/topperc created https://github.com/llvm/llvm-project/pull/90790

If the string is just a version, we will end up adding an empty string as an extension which crashes in the compare function for the std::map.

>From c91af96ebfe25b195c2084571e08b72d1e482a6d Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Wed, 1 May 2024 15:12:50 -0700
Subject: [PATCH] [RISCV] Detect empty extension name after parsing
 MajorVersion in parseNormalizedArchString.

If the string is just a version, we will end up adding an empty
string as an extension which crashes in the compare function for
the std::map.
---
 llvm/lib/TargetParser/RISCVISAInfo.cpp           | 4 ++++
 llvm/unittests/TargetParser/RISCVISAInfoTest.cpp | 8 ++++++++
 2 files changed, 12 insertions(+)

diff --git a/llvm/lib/TargetParser/RISCVISAInfo.cpp b/llvm/lib/TargetParser/RISCVISAInfo.cpp
index e8172ebb259720..0863d9319acb4e 100644
--- a/llvm/lib/TargetParser/RISCVISAInfo.cpp
+++ b/llvm/lib/TargetParser/RISCVISAInfo.cpp
@@ -470,6 +470,10 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
       return createStringError(errc::invalid_argument,
                                "extension lacks version in expected format");
 
+    if (VersionStart == 0)
+      return createStringError(errc::invalid_argument,
+                               "missing extension name");
+
     StringRef ExtName = Prefix.slice(0, VersionStart);
     StringRef MajorVersionStr = Prefix.slice(VersionStart, StringRef::npos);
     if (MajorVersionStr.getAsInteger(10, MajorVersion))
diff --git a/llvm/unittests/TargetParser/RISCVISAInfoTest.cpp b/llvm/unittests/TargetParser/RISCVISAInfoTest.cpp
index 3aa0178100abf4..9e88b87df4e8a7 100644
--- a/llvm/unittests/TargetParser/RISCVISAInfoTest.cpp
+++ b/llvm/unittests/TargetParser/RISCVISAInfoTest.cpp
@@ -45,6 +45,14 @@ TEST(ParseNormalizedArchString, RejectsMalformedInputs) {
   }
 }
 
+TEST(ParseNormalizedArchString, OnlyVersion) {
+  for (StringRef Input : {"rv64i2p0_1p0", "rv32i2p0_1p0"}) {
+    EXPECT_EQ(
+        toString(RISCVISAInfo::parseNormalizedArchString(Input).takeError()),
+        "missing extension name");
+  }
+}
+
 TEST(ParseNormalizedArchString, AcceptsValidBaseISAsAndSetsXLen) {
   auto MaybeRV32I = RISCVISAInfo::parseNormalizedArchString("rv32i2p0");
   ASSERT_THAT_EXPECTED(MaybeRV32I, Succeeded());



More information about the llvm-commits mailing list