Skip to content

Commit ff7fe4a

Browse files
Chan AungChan Aung
authored andcommitted
Add validation for --benchmark-samples to prevent crash with zero samples
1 parent 31ee3be commit ff7fe4a

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/catch2/internal/catch_commandline.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,19 @@ namespace Catch {
189189
config.shardCount = *parsedCount;
190190
return ParserResult::ok( ParseResultType::Matched );
191191
};
192+
auto const setBenchmarkSamples = [&]( std::string const& samples ) {
193+
auto parsedSamples = parseUInt( samples );
194+
if ( !parsedSamples ) {
195+
return ParserResult::runtimeError(
196+
"Could not parse '" + samples + "' as benchmark samples" );
197+
}
198+
if ( *parsedSamples == 0 ) {
199+
return ParserResult::runtimeError(
200+
"Benchmark samples must be greater than 0" );
201+
}
202+
config.benchmarkSamples = *parsedSamples;
203+
return ParserResult::ok( ParseResultType::Matched );
204+
};
192205

193206
auto const setShardIndex = [&](std::string const& shardIndex) {
194207
auto parsedIndex = parseUInt( shardIndex );
@@ -281,7 +294,7 @@ namespace Catch {
281294
| Opt( config.skipBenchmarks)
282295
["--skip-benchmarks"]
283296
( "disable running benchmarks")
284-
| Opt( config.benchmarkSamples, "samples" )
297+
| Opt( setBenchmarkSamples, "samples" )
285298
["--benchmark-samples"]
286299
( "number of samples to collect (default: 100)" )
287300
| Opt( config.benchmarkResamples, "resamples" )

0 commit comments

Comments
 (0)