Skip to content

Commit

Permalink
[ELF] Do not allow -- for a single-letter option
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed May 3, 2022
1 parent 31ba99a commit 232dafa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
26 changes: 7 additions & 19 deletions elf/cmdline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ mold: supported targets: elf32-i386 elf64-x86-64 elf64-littleaarch64
mold: supported emulations: elf_i386 elf_x86_64 aarch64linux aarch64elf)";

static std::vector<std::string> add_dashes(std::string name) {
// Single-letter option
if (name.size() == 1)
return {"-" + name};

// Multi-letter linker options can be preceded by either a single
// dash or double dashes except ones starting with "o", which must
// be preceded by double dashes. For example, "-omagic" is
Expand All @@ -192,23 +196,6 @@ static std::vector<std::string> add_dashes(std::string name) {
template <typename E>
bool read_arg(Context<E> &ctx, std::span<std::string_view> &args,
std::string_view &arg, std::string name) {
if (name.size() == 1) {
if (args[0] == "-" + name) {
if (args.size() == 1)
Fatal(ctx) << "option -" << name << ": argument missing";
arg = args[1];
args = args.subspan(2);
return true;
}

if (args[0].starts_with("-" + name)) {
arg = args[0].substr(name.size() + 1);
args = args.subspan(1);
return true;
}
return false;
}

for (std::string opt : add_dashes(name)) {
if (args[0] == opt) {
if (args.size() == 1)
Expand All @@ -218,8 +205,9 @@ bool read_arg(Context<E> &ctx, std::span<std::string_view> &args,
return true;
}

if (args[0].starts_with(opt + "=")) {
arg = args[0].substr(opt.size() + 1);
std::string prefix = (name.size() == 1) ? opt : opt + "=";
if (args[0].starts_with(prefix)) {
arg = args[0].substr(prefix.size());
args = args.subspan(1);
return true;
}
Expand Down
3 changes: 3 additions & 0 deletions test/elf/version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ $CC -B. -Wl,--version -o $t/exe $t/a.o 2>&1 | grep -q mold
$CC -B. -Wl,-v -o $t/exe $t/a.o 2>&1 | grep -q mold
$QEMU $t/exe | grep -q 'Hello world'

! $mold --v >& $t/log
grep -q 'unknown command line option:' $t/log

echo OK

0 comments on commit 232dafa

Please sign in to comment.