[llvm] 183beb3 - [llvm-objcopy] Check for missing argument values (#70710)

via llvm-commits llvm-commits at lists.llvm.org
Wed May 22 07:38:48 PDT 2024


Author: Alexey Karyakin
Date: 2024-05-22T09:38:44-05:00
New Revision: 183beb33d7f8847c04870e425d75f27db1cf5847

URL: https://github.com/llvm/llvm-project/commit/183beb33d7f8847c04870e425d75f27db1cf5847
DIFF: https://github.com/llvm/llvm-project/commit/183beb33d7f8847c04870e425d75f27db1cf5847.diff

LOG: [llvm-objcopy] Check for missing argument values (#70710)

Report an error if a required value for a command line argument is
missing.

Added: 
    llvm/test/tools/llvm-objcopy/tool-options.test

Modified: 
    llvm/tools/llvm-objcopy/ObjcopyOptions.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-objcopy/tool-options.test b/llvm/test/tools/llvm-objcopy/tool-options.test
new file mode 100644
index 0000000000000..8d2bb4476009d
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/tool-options.test
@@ -0,0 +1,6 @@
+## An error must be reported if a required argument value is missing.
+# RUN: not llvm-objcopy --only-section 2>&1 | FileCheck --check-prefix=CHECK-NO-VALUE-ONLY-SECTION %s
+# CHECK-NO-VALUE-ONLY-SECTION: error: argument to '--only-section' is missing (expected 1 value(s))
+
+# RUN: not llvm-objcopy -O 2>&1 | FileCheck --check-prefix=CHECK-NO-VALUE-O %s
+# CHECK-NO-VALUE-O: error: argument to '-O' is missing (expected 1 value(s))

diff  --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
index a1897334cff2e..4ab3b7265f2f6 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
+++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
@@ -571,6 +571,12 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
   llvm::opt::InputArgList InputArgs =
       T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
 
+  if (MissingArgumentCount)
+    return createStringError(
+        errc::invalid_argument,
+        "argument to '%s' is missing (expected %d value(s))",
+        InputArgs.getArgString(MissingArgumentIndex), MissingArgumentCount);
+
   if (InputArgs.size() == 0 && DashDash == RawArgsArr.end()) {
     printHelp(T, errs(), ToolType::Objcopy);
     exit(1);


        


More information about the llvm-commits mailing list