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

Commit 926987b

Browse files
macdiceCommitfest Bot
authored and
Commitfest Bot
committed
pgbench: Modernize integer parsing routine.
strtoint64() is from pre-C99 times and had comments about scanf() not being sure to handle long long. Even though C99 scanf() can do SCNi64, it seems like we'd lose a bit of error reporting. A more obvious modern drop-in is strtoi64(). Then strtoint64() can log the same errors as before.
1 parent cce5387 commit 926987b

File tree

1 file changed

+11
-61
lines changed

1 file changed

+11
-61
lines changed

src/bin/pgbench/pgbench.c

Lines changed: 11 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,9 +1003,6 @@ is_an_int(const char *str)
10031003
/*
10041004
* strtoint64 -- convert a string to 64-bit integer
10051005
*
1006-
* This function is a slightly modified version of pg_strtoint64() from
1007-
* src/backend/utils/adt/numutils.c.
1008-
*
10091006
* The function returns whether the conversion worked, and if so
10101007
* "*result" is set to the result.
10111008
*
@@ -1014,71 +1011,24 @@ is_an_int(const char *str)
10141011
bool
10151012
strtoint64(const char *str, bool errorOK, int64 *result)
10161013
{
1017-
const char *ptr = str;
1018-
int64 tmp = 0;
1019-
bool neg = false;
1020-
1021-
/*
1022-
* Do our own scan, rather than relying on sscanf which might be broken
1023-
* for long long.
1024-
*
1025-
* As INT64_MIN can't be stored as a positive 64 bit integer, accumulate
1026-
* value as a negative number.
1027-
*/
1014+
char *end;
10281015

1029-
/* skip leading spaces */
1030-
while (*ptr && isspace((unsigned char) *ptr))
1031-
ptr++;
1016+
errno = 0;
1017+
*result = strtoi64(str, &end, 10);
10321018

1033-
/* handle sign */
1034-
if (*ptr == '-')
1019+
if (errno == ERANGE)
10351020
{
1036-
ptr++;
1037-
neg = true;
1038-
}
1039-
else if (*ptr == '+')
1040-
ptr++;
1041-
1042-
/* require at least one digit */
1043-
if (unlikely(!isdigit((unsigned char) *ptr)))
1044-
goto invalid_syntax;
1045-
1046-
/* process digits */
1047-
while (*ptr && isdigit((unsigned char) *ptr))
1048-
{
1049-
int8 digit = (*ptr++ - '0');
1050-
1051-
if (unlikely(pg_mul_s64_overflow(tmp, 10, &tmp)) ||
1052-
unlikely(pg_sub_s64_overflow(tmp, digit, &tmp)))
1053-
goto out_of_range;
1021+
if (!errorOK)
1022+
pg_log_error("value \"%s\" is out of range for type bigint", str);
1023+
return false;
10541024
}
1055-
1056-
/* allow trailing whitespace, but not other trailing chars */
1057-
while (*ptr != '\0' && isspace((unsigned char) *ptr))
1058-
ptr++;
1059-
1060-
if (unlikely(*ptr != '\0'))
1061-
goto invalid_syntax;
1062-
1063-
if (!neg)
1025+
if (str == end || *end != '\0')
10641026
{
1065-
if (unlikely(tmp == PG_INT64_MIN))
1066-
goto out_of_range;
1067-
tmp = -tmp;
1027+
if (!errorOK)
1028+
pg_log_error("invalid input syntax for type bigint: \"%s\"", str);
1029+
return false;
10681030
}
1069-
1070-
*result = tmp;
10711031
return true;
1072-
1073-
out_of_range:
1074-
if (!errorOK)
1075-
pg_log_error("value \"%s\" is out of range for type bigint", str);
1076-
return false;
1077-
1078-
invalid_syntax:
1079-
if (!errorOK)
1080-
pg_log_error("invalid input syntax for type bigint: \"%s\"", str);
1081-
return false;
10821032
}
10831033

10841034
/* convert string to double, detecting overflows/underflows */

0 commit comments

Comments
 (0)