Skip to content

Commit 51328d5

Browse files
committed
Add overflow checks to int4 and int8 versions of generate_series().
The previous code went into an infinite loop after overflow. In fact, an overflow is not really an error; it just means that the current value is the last one we need to return. So, just arrange to stop immediately when overflow is detected. Back-patch all the way.
1 parent 1c7ddbf commit 51328d5

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/backend/utils/adt/int.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,10 @@ generate_series_step_int4(PG_FUNCTION_ARGS)
13811381
/* increment current in preparation for next iteration */
13821382
fctx->current += fctx->step;
13831383

1384+
/* if next-value computation overflows, this is the final result */
1385+
if (SAMESIGN(result, fctx->step) && !SAMESIGN(result, fctx->current))
1386+
fctx->step = 0;
1387+
13841388
/* do when there is more left to send */
13851389
SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
13861390
}

src/backend/utils/adt/int8.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,10 @@ generate_series_step_int8(PG_FUNCTION_ARGS)
14121412
/* increment current in preparation for next iteration */
14131413
fctx->current += fctx->step;
14141414

1415+
/* if next-value computation overflows, this is the final result */
1416+
if (SAMESIGN(result, fctx->step) && !SAMESIGN(result, fctx->current))
1417+
fctx->step = 0;
1418+
14151419
/* do when there is more left to send */
14161420
SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
14171421
}

0 commit comments

Comments
 (0)