Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 68cbb9f

Browse files
committed
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.
1 parent b43bf61 commit 68cbb9f

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

contrib/pgbench/pgbench.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,9 +1066,23 @@ doCustom(TState *thread, CState *st, instr_time *conn_time, FILE *logfile)
10661066
else
10671067
max = atoi(argv[3]);
10681068

1069-
if (max < min || max > MAX_RANDOM_VALUE)
1069+
if (max < min)
10701070
{
1071-
fprintf(stderr, "%s: invalid maximum number %d\n", argv[0], max);
1071+
fprintf(stderr, "%s: maximum is less than minimum\n", argv[0]);
1072+
st->ecnt++;
1073+
return true;
1074+
}
1075+
1076+
/*
1077+
* getrand() neeeds to be able to subtract max from min and add
1078+
* one the result without overflowing. Since we know max > min,
1079+
* we can detect overflow just by checking for a negative result.
1080+
* But we must check both that the subtraction doesn't overflow,
1081+
* and that adding one to the result doesn't overflow either.
1082+
*/
1083+
if (max - min < 0 || (max - min) + 1 < 0)
1084+
{
1085+
fprintf(stderr, "%s: range too large\n", argv[0]);
10721086
st->ecnt++;
10731087
return true;
10741088
}

0 commit comments

Comments
 (0)