Skip to content

Commit c8e8b27

Browse files
committedJun 25, 2019
Reapply "Fix a crash in option parsing."
with an additional read-out-of-bounds bugfix applied. Differential Revision: https://reviews.llvm.org/D63110 llvm-svn: 364260
1 parent 49885b1 commit c8e8b27

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed
 

Diff for: ‎lldb/lit/Driver/Inputs/process_attach_pid.in

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
process attach --pid
2+

Diff for: ‎lldb/lit/Driver/TestProcessAttach.test

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# RUN: %lldb -x -b -S %S/Inputs/process_attach_pid.in 2>&1 | FileCheck %s
2+
# CHECK: last option requires an argument

Diff for: ‎lldb/source/Interpreter/Options.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -1355,13 +1355,23 @@ llvm::Expected<Args> Options::Parse(const Args &args,
13551355
}
13561356
}
13571357
std::vector<char *> argv = GetArgvForParsing(args);
1358+
// If the last option requires an argument but doesn't have one,
1359+
// some implementations of getopt_long will still try to read it.
1360+
char overflow = 0;
1361+
argv.push_back(&overflow);
13581362
std::unique_lock<std::mutex> lock;
13591363
OptionParser::Prepare(lock);
13601364
int val;
13611365
while (true) {
13621366
int long_options_index = -1;
1363-
val = OptionParser::Parse(argv.size(), &*argv.begin(), sstr.GetString(),
1367+
val = OptionParser::Parse(argv.size() - 1, &*argv.begin(), sstr.GetString(),
13641368
long_options, &long_options_index);
1369+
1370+
if ((size_t)OptionParser::GetOptionIndex() > argv.size() - 1) {
1371+
error.SetErrorStringWithFormat("last option requires an argument");
1372+
break;
1373+
}
1374+
13651375
if (val == -1)
13661376
break;
13671377

@@ -1439,6 +1449,7 @@ llvm::Expected<Args> Options::Parse(const Args &args,
14391449
if (error.Fail())
14401450
return error.ToError();
14411451

1452+
argv.pop_back();
14421453
argv.erase(argv.begin(), argv.begin() + OptionParser::GetOptionIndex());
14431454
return ReconstituteArgsAfterParsing(argv, args);
14441455
}

0 commit comments

Comments
 (0)
Please sign in to comment.