Add overflow checks to int4 and int8 versions of generate_series().
authorRobert Haas <[email protected]>
Fri, 17 Jun 2011 18:28:45 +0000 (14:28 -0400)
committerRobert Haas <[email protected]>
Fri, 17 Jun 2011 18:32:44 +0000 (14:32 -0400)
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.

src/backend/utils/adt/int.c
src/backend/utils/adt/int8.c

index abe3c7eae034ce1604dc6453f1f3bc32cdf9312b..a6e853b47d363337d525d98bf74c1c7f5a9abd66 100644 (file)
@@ -1372,6 +1372,10 @@ generate_series_step_int4(PG_FUNCTION_ARGS)
        /* increment current in preparation for next iteration */
        fctx->current += fctx->step;
 
+       /* if next-value computation overflows, this is the final result */
+       if (SAMESIGN(result, fctx->step) && !SAMESIGN(result, fctx->current))
+           fctx->step = 0;
+
        /* do when there is more left to send */
        SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
    }
index 5e8cc03013759aaec8531cc689c5d4e959223f38..9ffc5f11d697e9d2f3307220ae7e22b0645341c8 100644 (file)
@@ -1421,6 +1421,10 @@ generate_series_step_int8(PG_FUNCTION_ARGS)
        /* increment current in preparation for next iteration */
        fctx->current += fctx->step;
 
+       /* if next-value computation overflows, this is the final result */
+       if (SAMESIGN(result, fctx->step) && !SAMESIGN(result, fctx->current))
+           fctx->step = 0;
+
        /* do when there is more left to send */
        SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
    }