[PATCH] D132418: [tools][llvm-lipo] Fix off-by-one error in command-line argument parsing

Alexander Shaposhnikov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 22 15:36:02 PDT 2022


alexander-shaposhnikov created this revision.
alexander-shaposhnikov added reviewers: smeenai, rmaz.
alexander-shaposhnikov created this object with visibility "All Users".
Herald added a subscriber: ormris.
Herald added a project: All.
alexander-shaposhnikov requested review of this revision.
Herald added a project: LLVM.

makeArrayRef(argv + 1, argc) -> makeArrayRef(argv + 1, argc - 1)
The previous behavior resulted in propagation of the null pointer into later stages of arguments parsing
instead of being automatically handled by the existing check of MissingArgumentCount.

Test plan: ninja check-all


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132418

Files:
  llvm/test/tools/llvm-lipo/replace-invalid-input.test
  llvm/test/tools/llvm-lipo/segalign-invalid-input.test
  llvm/tools/llvm-lipo/llvm-lipo.cpp


Index: llvm/tools/llvm-lipo/llvm-lipo.cpp
===================================================================
--- llvm/tools/llvm-lipo/llvm-lipo.cpp
+++ llvm/tools/llvm-lipo/llvm-lipo.cpp
@@ -183,9 +183,7 @@
     C.InputFiles.push_back({None, Arg->getValue()});
   for (auto Arg : InputArgs.filtered(LIPO_arch)) {
     validateArchitectureName(Arg->getValue(0));
-    if (!Arg->getValue(1))
-      reportError(
-          "arch is missing an argument: expects -arch arch_type file_name");
+    assert(Arg->getValue(1) && "file_name is missing");
     C.InputFiles.push_back({StringRef(Arg->getValue(0)), Arg->getValue(1)});
   }
 
@@ -294,10 +292,7 @@
 
   case LIPO_replace:
     for (auto Action : ActionArgs) {
-      if (!Action->getValue(1))
-        reportError(
-            "replace is missing an argument: expects -replace arch_type "
-            "file_name");
+      assert(Action->getValue(1) && "file_name is missing");
       validateArchitectureName(Action->getValue(0));
       C.ReplacementFiles.push_back(
           {StringRef(Action->getValue(0)), Action->getValue(1)});
@@ -725,7 +720,7 @@
 
 int main(int argc, char **argv) {
   InitLLVM X(argc, argv);
-  Config C = parseLipoOptions(makeArrayRef(argv + 1, argc));
+  Config C = parseLipoOptions(makeArrayRef(argv + 1, argc - 1));
   LLVMContext LLVMCtx;
   SmallVector<OwningBinary<Binary>, 1> InputBinaries =
       readInputBinaries(LLVMCtx, C.InputFiles);
Index: llvm/test/tools/llvm-lipo/segalign-invalid-input.test
===================================================================
--- llvm/test/tools/llvm-lipo/segalign-invalid-input.test
+++ llvm/test/tools/llvm-lipo/segalign-invalid-input.test
@@ -2,7 +2,7 @@
 # RUN: yaml2obj %p/Inputs/armv7-slice.yaml -o %t-armv7.o
 
 # RUN: not llvm-lipo %t-armv7.o %t-arm64.o -create -o %t.o -segalign a 2>&1 | FileCheck --check-prefix=MISSING_ARG %s
-# MISSING_ARG: error: segalign is missing an argument: expects -segalign arch_type alignment_value
+# MISSING_ARG: error: missing argument to -segalign option
 
 # RUN: not llvm-lipo %t-armv7.o %t-arm64.o -o %t.o -segalign arm64 10 2>&1 | FileCheck --check-prefix=MISSING_ACTION %s
 # MISSING_ACTION: error: at least one action should be specified
Index: llvm/test/tools/llvm-lipo/replace-invalid-input.test
===================================================================
--- llvm/test/tools/llvm-lipo/replace-invalid-input.test
+++ llvm/test/tools/llvm-lipo/replace-invalid-input.test
@@ -3,7 +3,7 @@
 # RUN: yaml2obj %p/Inputs/i386-x86_64-universal.yaml -o %t-universal.o
 
 # RUN: not llvm-lipo %t-universal.o -replace %t-32.o 2>&1 | FileCheck --check-prefix=MISSING_ARG %s
-# MISSING_ARG: error: replace is missing an argument: expects -replace arch_type file_name
+# MISSING_ARG: error: missing argument to -replace option
 
 # RUN: not llvm-lipo %t-universal.o -replace i386 %t-32.o 2>&1 | FileCheck --check-prefix=OUTPUT_FILE %s
 # OUTPUT_FILE: error: replace expects a single output file to be specified


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132418.454634.patch
Type: text/x-patch
Size: 2989 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220822/10e5460b/attachment.bin>


More information about the llvm-commits mailing list