Skip to content

Commit da02101

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 1907dca commit da02101

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
@@ -1410,6 +1410,10 @@ generate_series_step_int4(PG_FUNCTION_ARGS)
14101410
/* increment current in preparation for next iteration */
14111411
fctx->current += fctx->step;
14121412

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

src/backend/utils/adt/int8.c

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

1233+
/* if next-value computation overflows, this is the final result */
1234+
if (SAMESIGN(result, fctx->step) && !SAMESIGN(result, fctx->current))
1235+
fctx->step = 0;
1236+
12331237
/* do when there is more left to send */
12341238
SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
12351239
}

0 commit comments

Comments
 (0)