From 510b3b6b248fc32d20651ad9c23bb5a20689d224 Mon Sep 17 00:00:00 2001 From: Sergey Dmitriev Date: Mon, 29 Jul 2019 16:22:40 +0000 Subject: [PATCH] [llvm-objcopy] Improve --add-section argument string parsing Differential Revision: https://reviews.llvm.org/D65346 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@367236 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/tools/llvm-objcopy/COFF/add-section.test | 13 +++++++++---- test/tools/llvm-objcopy/ELF/add-section.test | 16 ++++++++++++++++ tools/llvm-objcopy/COFF/COFFObjcopy.cpp | 3 --- tools/llvm-objcopy/CopyConfig.cpp | 13 +++++++++++-- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/test/tools/llvm-objcopy/COFF/add-section.test b/test/tools/llvm-objcopy/COFF/add-section.test index f2aa5ef093d..4422e31a99c 100644 --- a/test/tools/llvm-objcopy/COFF/add-section.test +++ b/test/tools/llvm-objcopy/COFF/add-section.test @@ -34,14 +34,19 @@ ## 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=.another.section=%t2 %t %t3 2>&1 | FileCheck -DFILE=%t -DFILE1=%t2 %s --check-prefixes=CHECK-ERR1 +# RUN: not llvm-objcopy --add-section=.another.section=%t2 %t %t3 2>&1 | FileCheck -DFILE1=%t -DFILE2=%t2 %s --check-prefixes=ERR1 -# CHECK-ERR1: llvm-objcopy{{(.exe)?}}: error: '[[FILE]]': '[[FILE1]]': {{[Nn]}}o such file or directory +# ERR1: error: '[[FILE1]]': '[[FILE2]]': {{[Nn]}}o such file or directory ## 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 +# RUN: not llvm-objcopy --add-section=.another.section %t %t3 2>&1 | FileCheck %s --check-prefixes=ERR2 -# CHECK-ERR2: llvm-objcopy{{(.exe)?}}: error: '[[FILE]]': bad format for --add-section +# ERR2: 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 %t3 2>&1 | FileCheck %s --check-prefixes=ERR3 + +# ERR3: error: bad format for --add-section: missing file name --- !COFF header: diff --git a/test/tools/llvm-objcopy/ELF/add-section.test b/test/tools/llvm-objcopy/ELF/add-section.test index bf3ffdb090e..4acbd9ae4e2 100644 --- a/test/tools/llvm-objcopy/ELF/add-section.test +++ b/test/tools/llvm-objcopy/ELF/add-section.test @@ -35,3 +35,19 @@ Sections: # 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 -DFILE1=%t -DFILE2=%t.missing %s --check-prefixes=ERR1 + +# ERR1: error: '[[FILE1]]': '[[FILE2]]': {{[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 %s --check-prefixes=ERR2 + +# ERR2: 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 %s --check-prefixes=ERR3 + +# ERR3: error: bad format for --add-section: missing file name diff --git a/tools/llvm-objcopy/COFF/COFFObjcopy.cpp b/tools/llvm-objcopy/COFF/COFFObjcopy.cpp index b0474a0f0f4..d30bea0b35f 100644 --- a/tools/llvm-objcopy/COFF/COFFObjcopy.cpp +++ b/tools/llvm-objcopy/COFF/COFFObjcopy.cpp @@ -186,9 +186,6 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) { 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())); diff --git a/tools/llvm-objcopy/CopyConfig.cpp b/tools/llvm-objcopy/CopyConfig.cpp index 8d6431b3044..4001aac4ed4 100644 --- a/tools/llvm-objcopy/CopyConfig.cpp +++ b/tools/llvm-objcopy/CopyConfig.cpp @@ -617,8 +617,17 @@ Expected parseObjcopyOptions(ArrayRef ArgsArr) { 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); -- 2.40.0