From ac1efb50d1e4d5ac04968cffefa5464b1511743c Mon Sep 17 00:00:00 2001 From: benbuckman Date: Mon, 11 Nov 2019 14:20:44 -0800 Subject: [PATCH] ArgumentError on interval <1 or >1m Addresses https://github.com/tmm1/stackprof/issues/125 --- ext/stackprof/stackprof.c | 5 +++++ test/test_stackprof.rb | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/ext/stackprof/stackprof.c b/ext/stackprof/stackprof.c index ae3467d4..c6c18214 100644 --- a/ext/stackprof/stackprof.c +++ b/ext/stackprof/stackprof.c @@ -16,6 +16,7 @@ #include #define BUF_SIZE 2048 +#define MICROSECONDS_IN_SECOND 1000000 typedef struct { size_t total_samples; @@ -92,6 +93,10 @@ stackprof_start(int argc, VALUE *argv, VALUE self) } if (!RTEST(mode)) mode = sym_wall; + if (!NIL_P(interval) && (NUM2INT(interval) < 1 || NUM2INT(interval) >= MICROSECONDS_IN_SECOND)) { + rb_raise(rb_eArgError, "interval is a number of microseconds between 1 and 1 million"); + } + if (!_stackprof.frames) { _stackprof.frames = st_init_numtable(); _stackprof.overall_signals = 0; diff --git a/test/test_stackprof.rb b/test/test_stackprof.rb index 27da5c37..f8b4f65f 100644 --- a/test/test_stackprof.rb +++ b/test/test_stackprof.rb @@ -194,6 +194,15 @@ def test_pathname_out refute_empty profile[:frames] end + def test_min_max_interval + [-1, 0, 1_000_000, 1_000_001].each do |invalid_interval| + err = assert_raises(ArgumentError, "invalid interval #{invalid_interval}") do + StackProf.run(interval: invalid_interval, debug: true) {} + end + assert_match(/microseconds/, err.message) + end + end + def math 250_000.times do 2 ** 10