[PATCH] D158134: Guard against self-assignment in InputArgList

Andy Kaylor via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 23 08:29:11 PDT 2023


andrew.w.kaylor updated this revision to Diff 552726.
andrew.w.kaylor added a comment.

Add unit test and refactor to use early return


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158134/new/

https://reviews.llvm.org/D158134

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


Index: llvm/unittests/Option/OptionParsingTest.cpp
===================================================================
--- llvm/unittests/Option/OptionParsingTest.cpp
+++ llvm/unittests/Option/OptionParsingTest.cpp
@@ -238,6 +238,23 @@
   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;
Index: llvm/include/llvm/Option/ArgList.h
===================================================================
--- llvm/include/llvm/Option/ArgList.h
+++ llvm/include/llvm/Option/ArgList.h
@@ -419,6 +419,8 @@
         NumInputArgStrings(RHS.NumInputArgStrings) {}
 
   InputArgList &operator=(InputArgList &&RHS) {
+    if (this == &RHS)
+      return *this;
     releaseMemory();
     ArgList::operator=(std::move(RHS));
     ArgStrings = std::move(RHS.ArgStrings);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158134.552726.patch
Type: text/x-patch
Size: 1331 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230823/03a4525b/attachment.bin>


More information about the llvm-commits mailing list