bench: fail early if user inputs invalid value for SECP256K1_BENCH_ITERS
What changed, and why it matters
This commit improves error handling in the benchmark programs only. It makes the benchmark tools reject invalid or non-positive values for the SECP256K1_BENCH_ITERS environment variable instead of silently using a bad value. This is a hardening/robustness fix for developer tooling, not a security fix for the cryptographic library itself.
No security action required. Treat as a normal code-quality/robustness improvement. If backporting, include only for benchmark tooling consistency.
Security signals we found
Input validation added to environment variable parsing in benchmark tooling
Use of strtol with endptr check to detect malformed integer input
No changes to cryptographic, consensus, or library code paths
Evidence from the diff
The get_iters() helper in src/bench.h now uses strtol with endptr validation and rejects values that are not positive integers. The three benchmark main() functions (bench.c, bench_ecmult.c, bench_internal.c) now check for a 0 return from get_iters(), print help, and exit with EXIT_FAILURE. Previously, an invalid SECP256K1_BENCH_ITERS value could be passed through strtol(NULL,…) and used as an iteration count, potentially causing zero or negative iterations in benchmarks. This change is confined to benchmark executables and does not affect libsecp256k1 library code, public API, consensus code, or wallet operations.
Changed components
src/bench.hsrc/bench.csrc/bench_ecmult.csrc/bench_internal.cInspect captured patch +23 / −4
diff --git a/src/bench.c b/src/bench.c
index 04685d9..a5231b7 100644
--- a/src/bench.c
+++ b/src/bench.c
@@ -177,8 +177,6 @@ int main(int argc, char** argv) {
bench_data data;
int d = argc == 1;
- int default_iters = 20000;
- int iters = get_iters(default_iters);
/* Check for invalid user arguments */
char* valid_args[] = {"ecdsa", "verify", "ecdsa_verify", "sign", "ecdsa_sign", "ecdh", "recover",
@@ -188,6 +186,13 @@ int main(int argc, char** argv) {
size_t valid_args_size = sizeof(valid_args)/sizeof(valid_args[0]);
int invalid_args = have_invalid_args(argc, argv, valid_args, valid_args_size);
+ int default_iters = 20000;
+ int iters = get_iters(default_iters);
+ if (iters == 0) {
+ help(default_iters);
+ return EXIT_FAILURE;
+ }
+
if (argc > 1) {
if (have_flag(argc, argv, "-h")
|| have_flag(argc, argv, "--help")
diff --git a/src/bench.h b/src/bench.h
index 4e8e961..72a3f21 100644
--- a/src/bench.h
+++ b/src/bench.h
@@ -150,7 +150,13 @@ static int have_invalid_args(int argc, char** argv, char** valid_args, size_t n)
static int get_iters(int default_iters) {
char* env = getenv("SECP256K1_BENCH_ITERS");
if (env) {
- return strtol(env, NULL, 0);
+ char* endptr;
+ long int iters = strtol(env, &endptr, 0);
+ if (*endptr != '\0' || iters <= 0) {
+ printf("Error: Value of SECP256K1_BENCH_ITERS is not a positive integer: %s\n\n", env);
+ return 0;
+ }
+ return iters;
} else {
return default_iters;
}
diff --git a/src/bench_ecmult.c b/src/bench_ecmult.c
index e8fab14..bcf8b43 100644
--- a/src/bench_ecmult.c
+++ b/src/bench_ecmult.c
@@ -313,6 +313,10 @@ int main(int argc, char **argv) {
int default_iters = 10000;
int iters = get_iters(default_iters);
+ if (iters == 0) {
+ help(argv, default_iters);
+ return EXIT_FAILURE;
+ }
data.ecmult_multi = secp256k1_ecmult_multi_var;
diff --git a/src/bench_internal.c b/src/bench_internal.c
index 8688a4d..001bd25 100644
--- a/src/bench_internal.c
+++ b/src/bench_internal.c
@@ -385,9 +385,13 @@ static void bench_context(void* arg, int iters) {
int main(int argc, char **argv) {
bench_inv data;
+ int d = argc == 1; /* default */
int default_iters = 20000;
int iters = get_iters(default_iters);
- int d = argc == 1; /* default */
+ if (iters == 0) {
+ help(default_iters);
+ return EXIT_FAILURE;
+ }
if (argc > 1) {
if (have_flag(argc, argv, "-h")
Why this scored 16/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.