Skip to content

Commit a0db429

Browse files
committed
Fix division by zero error in date_bin
Bauyrzhan Sakhariyev, via Github Backpatch to v14
1 parent 86a1aae commit a0db429

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

src/backend/utils/adt/timestamp.c

+10
Original file line numberDiff line numberDiff line change
@@ -3843,6 +3843,11 @@ timestamp_bin(PG_FUNCTION_ARGS)
38433843

38443844
stride_usecs = stride->day * USECS_PER_DAY + stride->time;
38453845

3846+
if (stride_usecs == 0)
3847+
ereport(ERROR,
3848+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3849+
errmsg("stride cannot equal zero")));
3850+
38463851
tm_diff = timestamp - origin;
38473852
tm_delta = tm_diff - tm_diff % stride_usecs;
38483853

@@ -4021,6 +4026,11 @@ timestamptz_bin(PG_FUNCTION_ARGS)
40214026

40224027
stride_usecs = stride->day * USECS_PER_DAY + stride->time;
40234028

4029+
if (stride_usecs == 0)
4030+
ereport(ERROR,
4031+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4032+
errmsg("stride cannot equal zero")));
4033+
40244034
tm_diff = timestamp - origin;
40254035
tm_delta = tm_diff - tm_diff % stride_usecs;
40264036

src/test/regress/expected/timestamp.out

+3
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,9 @@ SELECT date_bin('5 months'::interval, timestamp '2020-02-01 01:01:01', timestamp
704704
ERROR: timestamps cannot be binned into intervals containing months or years
705705
SELECT date_bin('5 years'::interval, timestamp '2020-02-01 01:01:01', timestamp '2001-01-01');
706706
ERROR: timestamps cannot be binned into intervals containing months or years
707+
-- disallow zero intervals
708+
SELECT date_bin('0 days'::interval, timestamp '1970-01-01 01:00:00' , timestamp '1970-01-01 00:00:00');
709+
ERROR: stride cannot equal zero
707710
-- Test casting within a BETWEEN qualifier
708711
SELECT d1 - timestamp without time zone '1997-01-02' AS diff
709712
FROM TIMESTAMP_TBL

src/test/regress/expected/timestamptz.out

+3
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,9 @@ SELECT date_bin('5 months'::interval, timestamp with time zone '2020-02-01 01:01
748748
ERROR: timestamps cannot be binned into intervals containing months or years
749749
SELECT date_bin('5 years'::interval, timestamp with time zone '2020-02-01 01:01:01+00', timestamp with time zone '2001-01-01+00');
750750
ERROR: timestamps cannot be binned into intervals containing months or years
751+
-- disallow zero intervals
752+
SELECT date_bin('0 days'::interval, timestamp with time zone '1970-01-01 01:00:00+00' , timestamp with time zone '1970-01-01 00:00:00+00');
753+
ERROR: stride cannot equal zero
751754
-- Test casting within a BETWEEN qualifier
752755
SELECT d1 - timestamp with time zone '1997-01-02' AS diff
753756
FROM TIMESTAMPTZ_TBL

src/test/regress/sql/timestamp.sql

+3
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ SELECT date_bin('5 min'::interval, timestamp '2020-02-01 01:01:01', timestamp '2
263263
SELECT date_bin('5 months'::interval, timestamp '2020-02-01 01:01:01', timestamp '2001-01-01');
264264
SELECT date_bin('5 years'::interval, timestamp '2020-02-01 01:01:01', timestamp '2001-01-01');
265265

266+
-- disallow zero intervals
267+
SELECT date_bin('0 days'::interval, timestamp '1970-01-01 01:00:00' , timestamp '1970-01-01 00:00:00');
268+
266269
-- Test casting within a BETWEEN qualifier
267270
SELECT d1 - timestamp without time zone '1997-01-02' AS diff
268271
FROM TIMESTAMP_TBL

src/test/regress/sql/timestamptz.sql

+3
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ SELECT date_bin('5 min'::interval, timestamptz '2020-02-01 01:01:01+00', timesta
238238
SELECT date_bin('5 months'::interval, timestamp with time zone '2020-02-01 01:01:01+00', timestamp with time zone '2001-01-01+00');
239239
SELECT date_bin('5 years'::interval, timestamp with time zone '2020-02-01 01:01:01+00', timestamp with time zone '2001-01-01+00');
240240

241+
-- disallow zero intervals
242+
SELECT date_bin('0 days'::interval, timestamp with time zone '1970-01-01 01:00:00+00' , timestamp with time zone '1970-01-01 00:00:00+00');
243+
241244
-- Test casting within a BETWEEN qualifier
242245
SELECT d1 - timestamp with time zone '1997-01-02' AS diff
243246
FROM TIMESTAMPTZ_TBL

0 commit comments

Comments
 (0)