Skip to content

Commit 0d52283

Browse files
authored
python binding: add range check (#1990)
fixes segmentation fault in ```import google_benchmark as benchmark @benchmark.register def av_test(state): state.range(9) benchmark.main() ```
1 parent eddb024 commit 0d52283

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

bindings/python/google_benchmark/benchmark.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,15 @@ NB_MODULE(_benchmark, m) {
167167
.def_prop_rw("items_processed", &State::items_processed,
168168
&State::SetItemsProcessed)
169169
.def("set_label", &State::SetLabel)
170-
.def("range", &State::range, nb::arg("pos") = 0)
170+
.def(
171+
"range",
172+
[](const State& state, std::size_t pos = 0) -> int64_t {
173+
if (pos < state.range_size()) {
174+
return state.range(pos);
175+
}
176+
throw nb::index_error("pos is out of range");
177+
},
178+
nb::arg("pos") = 0)
171179
.def_prop_ro("iterations", &State::iterations)
172180
.def_prop_ro("name", &State::name)
173181
.def_rw("counters", &State::counters)

include/benchmark/benchmark.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,8 @@ class BENCHMARK_EXPORT BENCHMARK_INTERNAL_CACHELINE_ALIGNED State {
956956
BENCHMARK_ALWAYS_INLINE
957957
std::string name() const { return name_; }
958958

959+
size_t range_size() const { return range_.size(); }
960+
959961
private:
960962
// items we expect on the first cache line (ie 64 bytes of the struct)
961963
// When total_iterations_ is 0, KeepRunning() and friends will return false.

0 commit comments

Comments
 (0)