[PATCH] D65346: [llvm-objcopy] Improve --add-section argument string parsing
Sergey Dmitriev via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 26 14:36:12 PDT 2019
sdmitriev updated this revision to Diff 212008.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D65346/new/
https://reviews.llvm.org/D65346
Files:
llvm/test/tools/llvm-objcopy/COFF/add-section.test
llvm/test/tools/llvm-objcopy/ELF/add-section.test
llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
llvm/tools/llvm-objcopy/CopyConfig.cpp
Index: llvm/tools/llvm-objcopy/CopyConfig.cpp
===================================================================
--- llvm/tools/llvm-objcopy/CopyConfig.cpp
+++ llvm/tools/llvm-objcopy/CopyConfig.cpp
@@ -617,8 +617,17 @@
Config.KeepSection.emplace_back(Arg->getValue(), UseRegex);
for (auto Arg : InputArgs.filtered(OBJCOPY_only_section))
Config.OnlySection.emplace_back(Arg->getValue(), UseRegex);
- for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
- Config.AddSection.push_back(Arg->getValue());
+ for (auto Arg : InputArgs.filtered(OBJCOPY_add_section)) {
+ StringRef ArgValue(Arg->getValue());
+ if (!ArgValue.contains('='))
+ return createStringError(errc::invalid_argument,
+ "bad format for --add-section: missing '='");
+ if (ArgValue.split("=").second.empty())
+ return createStringError(
+ errc::invalid_argument,
+ "bad format for --add-section: missing file name");
+ Config.AddSection.push_back(ArgValue);
+ }
for (auto Arg : InputArgs.filtered(OBJCOPY_dump_section))
Config.DumpSection.push_back(Arg->getValue());
Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
Index: llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
===================================================================
--- llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
+++ llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
@@ -186,9 +186,6 @@
StringRef SecName, FileName;
std::tie(SecName, FileName) = Flag.split("=");
- if (FileName.empty())
- return createStringError(llvm::errc::invalid_argument,
- "bad format for --add-section");
auto BufOrErr = MemoryBuffer::getFile(FileName);
if (!BufOrErr)
return createFileError(FileName, errorCodeToError(BufOrErr.getError()));
Index: llvm/test/tools/llvm-objcopy/ELF/add-section.test
===================================================================
--- llvm/test/tools/llvm-objcopy/ELF/add-section.test
+++ llvm/test/tools/llvm-objcopy/ELF/add-section.test
@@ -35,3 +35,19 @@
# CHECK: SectionData (
# CHECK-NEXT: 0000: DEADBEEF
# CHECK-NEXT: )
+
+## Test that llvm-objcopy produces an error if the file with section contents
+## to be added does not exist.
+# RUN: not llvm-objcopy --add-section=.section.name=%t.missing %t %t.out 2>&1 | FileCheck -DFILE=%t -DFILE1=%t.missing %s --check-prefixes=CHECK-ERR1
+
+# CHECK-ERR1: llvm-objcopy{{(.exe)?}}: error: '[[FILE]]': '[[FILE1]]': {{[Nn]}}o such file or directory
+
+## Negative test for invalid --add-sections argument - missing '='.
+# RUN: not llvm-objcopy --add-section=.section.name %t %t.out 2>&1 | FileCheck -DFILE=%t %s --check-prefixes=CHECK-ERR2
+
+# CHECK-ERR2: llvm-objcopy{{(.exe)?}}: error: bad format for --add-section: missing '='
+
+## Negative test for invalid --add-sections argument - missing file name.
+# RUN: not llvm-objcopy --add-section=.section.name= %t %t.out 2>&1 | FileCheck -DFILE=%t %s --check-prefixes=CHECK-ERR3
+
+# CHECK-ERR3: llvm-objcopy{{(.exe)?}}: error: bad format for --add-section: missing file name
Index: llvm/test/tools/llvm-objcopy/COFF/add-section.test
===================================================================
--- llvm/test/tools/llvm-objcopy/COFF/add-section.test
+++ llvm/test/tools/llvm-objcopy/COFF/add-section.test
@@ -41,7 +41,12 @@
## Another negative test for invalid --add-sections command line argument.
# RUN: not llvm-objcopy --add-section=.another.section %t %t3 2>&1 | FileCheck -DFILE=%t %s --check-prefixes=CHECK-ERR2
-# CHECK-ERR2: llvm-objcopy{{(.exe)?}}: error: '[[FILE]]': bad format for --add-section
+# CHECK-ERR2: llvm-objcopy{{(.exe)?}}: error: bad format for --add-section: missing '='
+
+## Negative test for invalid --add-sections argument - missing file name.
+# RUN: not llvm-objcopy --add-section=.section.name= %t %t.out 2>&1 | FileCheck -DFILE=%t %s --check-prefixes=CHECK-ERR3
+
+# CHECK-ERR3: llvm-objcopy{{(.exe)?}}: error: bad format for --add-section: missing file name
--- !COFF
header:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65346.212008.patch
Type: text/x-patch
Size: 4049 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190726/6f04e0b2/attachment.bin>
More information about the llvm-commits
mailing list