Skip to content

Commit 199d449

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 07bc6fe commit 199d449

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
@@ -1470,6 +1470,10 @@ generate_series_step_int4(PG_FUNCTION_ARGS)
14701470
/* increment current in preparation for next iteration */
14711471
fctx->current += fctx->step;
14721472

1473+
/* if next-value computation overflows, this is the final result */
1474+
if (SAMESIGN(result, fctx->step) && !SAMESIGN(result, fctx->current))
1475+
fctx->step = 0;
1476+
14731477
/* do when there is more left to send */
14741478
SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
14751479
}

src/backend/utils/adt/int8.c

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

1271+
/* if next-value computation overflows, this is the final result */
1272+
if (SAMESIGN(result, fctx->step) && !SAMESIGN(result, fctx->current))
1273+
fctx->step = 0;
1274+
12711275
/* do when there is more left to send */
12721276
SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
12731277
}

0 commit comments

Comments
 (0)