Skip to content

Commit 91c8495

Browse files
committed
python: fix segfault on empty argv in main()
1 parent f30ba8b commit 91c8495

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

bindings/python/google_benchmark/benchmark.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ namespace {
1414
namespace nb = nanobind;
1515

1616
std::vector<std::string> Initialize(const std::vector<std::string>& argv) {
17-
// The `argv` pointers here become invalid when this function returns, but
18-
// benchmark holds the pointer to `argv[0]`. We create a static copy of it
19-
// so it persists, and replace the pointer below.
20-
static std::string executable_name(argv[0]);
2117
std::vector<char*> ptrs;
2218
ptrs.reserve(argv.size());
2319
for (auto& arg : argv) {
2420
ptrs.push_back(const_cast<char*>(arg.c_str()));
2521
}
26-
ptrs[0] = const_cast<char*>(executable_name.c_str());
22+
if (!ptrs.empty()) {
23+
// The `argv` pointers here become invalid when this function returns, but
24+
// benchmark holds the pointer to `argv[0]`. We create a static copy of it
25+
// so it persists, and replace the pointer below.
26+
static std::string executable_name(argv[0]);
27+
ptrs[0] = const_cast<char*>(executable_name.c_str());
28+
}
2729
int argc = static_cast<int>(argv.size());
2830
benchmark::Initialize(&argc, ptrs.data());
2931
std::vector<std::string> remaining_argv;

0 commit comments

Comments
 (0)