Skip to content

[objcopy] Return an error in case of an invalid regex #74319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions llvm/lib/ObjCopy/CommonConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/ObjCopy/CommonConfig.h"
#include "llvm/Support/Errc.h"

namespace llvm {
namespace objcopy {
Expand Down Expand Up @@ -38,6 +39,12 @@ NameOrPattern::create(StringRef Pattern, MatchStyle MS,
IsPositiveMatch);
}
case MatchStyle::Regex: {
Regex RegEx(Pattern);
std::string Err;
if (!RegEx.isValid(Err))
return createStringError(errc::invalid_argument,
"cannot compile regular expression \'" +
Pattern + "\': " + Err);
SmallVector<char, 32> Data;
return NameOrPattern(std::make_shared<Regex>(
("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data)));
Expand Down
13 changes: 13 additions & 0 deletions llvm/test/tools/llvm-objcopy/regex-error.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Test if providing objcopy with an invalid regex generates an error.

# RUN: yaml2obj %s -o %t

# RUN: not llvm-objcopy --regex --strip-symbol='[^)\' %t /dev/null 2>&1 | FileCheck %s
# CHECK: error: cannot compile regular expression '[^)\'

!ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
Machine: EM_X86_64