Skip to content

Commit 399a570

Browse files
committed
int8in failed to detect overflow; it really should.
1 parent f40c506 commit 399a570

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/backend/utils/adt/int8.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ int8in(char *str)
5959

6060
/*
6161
* Do our own scan, rather than relying on sscanf which might be
62-
* broken for long long. NOTE: this will not detect int64 overflow...
63-
* but sscanf doesn't either...
62+
* broken for long long.
6463
*/
6564
while (*ptr && isspace(*ptr)) /* skip leading spaces */
6665
ptr++;
@@ -69,11 +68,17 @@ int8in(char *str)
6968
else if (*ptr == '+')
7069
ptr++;
7170
if (!isdigit(*ptr)) /* require at least one digit */
72-
elog(ERROR, "Bad int8 external representation '%s'", str);
71+
elog(ERROR, "Bad int8 external representation \"%s\"", str);
7372
while (*ptr && isdigit(*ptr)) /* process digits */
74-
tmp = tmp * 10 + (*ptr++ - '0');
73+
{
74+
int64 newtmp = tmp * 10 + (*ptr++ - '0');
75+
76+
if ((newtmp / 10) != tmp) /* overflow? */
77+
elog(ERROR,"int8 value out of range: \"%s\"", str);
78+
tmp = newtmp;
79+
}
7580
if (*ptr) /* trailing junk? */
76-
elog(ERROR, "Bad int8 external representation '%s'", str);
81+
elog(ERROR, "Bad int8 external representation \"%s\"", str);
7782

7883
*result = (sign < 0) ? -tmp : tmp;
7984

0 commit comments

Comments
 (0)