[llvm] 73738fa - Guard against self-assignment in InputArgList

Andy Kaylor via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 24 14:45:12 PDT 2023


Author: Andy Kaylor
Date: 2023-08-24T14:44:50-07:00
New Revision: 73738fa504bba4993db70c0ecac693d2fc72e171

URL: https://github.com/llvm/llvm-project/commit/73738fa504bba4993db70c0ecac693d2fc72e171
DIFF: https://github.com/llvm/llvm-project/commit/73738fa504bba4993db70c0ecac693d2fc72e171.diff

LOG: Guard against self-assignment in InputArgList

The InputArgList move assignment operator releases memory before copying
fields, but it doesn't check against self-assignment. I haven't seen the
self-assignment happen, but it doesn't look safe.

Differential Revision: https://reviews.llvm.org/D158134

Added: 
    

Modified: 
    llvm/include/llvm/Option/ArgList.h
    llvm/unittests/Option/OptionParsingTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Option/ArgList.h b/llvm/include/llvm/Option/ArgList.h
index 310c8900af9ef5..c3e0147f8b8930 100644
--- a/llvm/include/llvm/Option/ArgList.h
+++ b/llvm/include/llvm/Option/ArgList.h
@@ -419,6 +419,8 @@ class InputArgList final : public ArgList {
         NumInputArgStrings(RHS.NumInputArgStrings) {}
 
   InputArgList &operator=(InputArgList &&RHS) {
+    if (this == &RHS)
+      return *this;
     releaseMemory();
     ArgList::operator=(std::move(RHS));
     ArgStrings = std::move(RHS.ArgStrings);

diff  --git a/llvm/unittests/Option/OptionParsingTest.cpp b/llvm/unittests/Option/OptionParsingTest.cpp
index 465cddfc9e8e41..a7075e6eea2303 100644
--- a/llvm/unittests/Option/OptionParsingTest.cpp
+++ b/llvm/unittests/Option/OptionParsingTest.cpp
@@ -238,6 +238,23 @@ TYPED_TEST(OptTableTest, IgnoreCase) {
   EXPECT_TRUE(AL.hasArg(OPT_B));
 }
 
+TYPED_TEST(OptTableTest, InputArgListSelfAssign) {
+  TypeParam T;
+  unsigned MAI, MAC;
+  InputArgList AL = T.ParseArgs(Args, MAI, MAC,
+                                /*FlagsToInclude=*/0,
+                                /*FlagsToExclude=*/OptFlag3);
+  EXPECT_TRUE(AL.hasArg(OPT_A));
+  EXPECT_TRUE(AL.hasArg(OPT_C));
+  EXPECT_FALSE(AL.hasArg(OPT_SLASH_C));
+
+  AL = std::move(AL);
+
+  EXPECT_TRUE(AL.hasArg(OPT_A));
+  EXPECT_TRUE(AL.hasArg(OPT_C));
+  EXPECT_FALSE(AL.hasArg(OPT_SLASH_C));
+}
+
 TYPED_TEST(OptTableTest, DoNotIgnoreCase) {
   TypeParam T;
   unsigned MAI, MAC;


        


More information about the llvm-commits mailing list