From 68cbb9f4e70b7b7ed515b5c63bafbe47d9617bf0 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Fri, 5 Aug 2011 12:48:45 -0400 Subject: [PATCH] Modestly improve pgbench's checking for invalid ranges. The old check against MAX_RANDOM_VALUE is clearly irrelevant since getrand() no longer calls random(). Instead, check whether min and max are close enough together to avoid an overflow inside getrand(), as suggested by Tom Lane. This is still somewhat silly, because we're using atoi(), which doesn't check for overflow anyway and (at least on my system) will cheerfully return 0 when given "4294967296". But that's a problem for another commit. --- contrib/pgbench/pgbench.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/contrib/pgbench/pgbench.c b/contrib/pgbench/pgbench.c index 56dab6192d..0d809c915b 100644 --- a/contrib/pgbench/pgbench.c +++ b/contrib/pgbench/pgbench.c @@ -1066,9 +1066,23 @@ top: else max = atoi(argv[3]); - if (max < min || max > MAX_RANDOM_VALUE) + if (max < min) { - fprintf(stderr, "%s: invalid maximum number %d\n", argv[0], max); + fprintf(stderr, "%s: maximum is less than minimum\n", argv[0]); + st->ecnt++; + return true; + } + + /* + * getrand() neeeds to be able to subtract max from min and add + * one the result without overflowing. Since we know max > min, + * we can detect overflow just by checking for a negative result. + * But we must check both that the subtraction doesn't overflow, + * and that adding one to the result doesn't overflow either. + */ + if (max - min < 0 || (max - min) + 1 < 0) + { + fprintf(stderr, "%s: range too large\n", argv[0]); st->ecnt++; return true; } -- 2.40.0