Skip to content

Commit 82f5ae1

Browse files
committed
python: fix segfault on empty argv in main()
1 parent 7f72775 commit 82f5ae1

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

bindings/python/google_benchmark/benchmark.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ std::vector<std::string> Initialize(const std::vector<std::string>& argv) {
1717
// The `argv` pointers here become invalid when this function returns, but
1818
// benchmark holds the pointer to `argv[0]`. We create a static copy of it
1919
// so it persists, and replace the pointer below.
20-
static std::string executable_name(argv[0]);
21-
std::vector<char*> ptrs;
22-
ptrs.reserve(argv.size());
23-
for (auto& arg : argv) {
24-
ptrs.push_back(const_cast<char*>(arg.c_str()));
20+
static std::string executable_name(argv.empty() ? "unknown" : argv[0]);
21+
int argc = static_cast<int>(std::max(std::size_t{1}, argv.size()));
22+
std::vector<char*> ptrs(argc);
23+
for (size_t i = 0; i < argv.size(); ++i) {
24+
ptrs[i] = const_cast<char*>(argv[i].c_str());
2525
}
2626
ptrs[0] = const_cast<char*>(executable_name.c_str());
27-
int argc = static_cast<int>(argv.size());
2827
benchmark::Initialize(&argc, ptrs.data());
2928
std::vector<std::string> remaining_argv;
3029
remaining_argv.reserve(argc);

0 commit comments

Comments
 (0)