Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5e80d35

Browse files
committed
Avoid dereferencing an undefined pointer in DecodeInterval().
Commit e39f990 moved some code up closer to the start of DecodeInterval(), without noticing that it had been implicitly relying on previous checks to reject the case of empty input. Given empty input, we'd now dereference a pointer that hadn't been set, possibly leading to a core dump. (But if we fail to provoke a SIGSEGV, nothing bad happens, and the expected syntax error is thrown a bit later.) Per bug #17788 from Alexander Lakhin. Back-patch to v15 where the fault was introduced. Discussion: https://postgr.es/m/17788-dabac9f98f7eafd5@postgresql.org
1 parent 156c049 commit 5e80d35

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

src/backend/utils/adt/datetime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3376,7 +3376,7 @@ DecodeInterval(char **field, int *ftype, int nf, int range,
33763376
* to dump in postgres style, not SQL style.)
33773377
*----------
33783378
*/
3379-
if (IntervalStyle == INTSTYLE_SQL_STANDARD && *field[0] == '-')
3379+
if (IntervalStyle == INTSTYLE_SQL_STANDARD && nf > 0 && *field[0] == '-')
33803380
{
33813381
force_negative = true;
33823382
/* Check for additional explicit signs */

src/test/regress/expected/interval.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,11 @@ SELECT interval '-23 hours 45 min 12.34 sec',
888888
-23:45:12.34 | -1 23:45:12.34 | -1-2 -1 -23:45:12.34 | -0-10 +1 +23:45:12.34
889889
(1 row)
890890

891+
-- edge case for sign-matching rules
892+
SELECT interval ''; -- error
893+
ERROR: invalid input syntax for type interval: ""
894+
LINE 1: SELECT interval '';
895+
^
891896
-- test outputting iso8601 intervals
892897
SET IntervalStyle to iso_8601;
893898
select interval '0' AS "zero",

src/test/regress/sql/interval.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ SELECT interval '-23 hours 45 min 12.34 sec',
284284
interval '-1 year 2 months 1 day 23 hours 45 min 12.34 sec',
285285
interval '-1 year 2 months 1 day 23 hours 45 min +12.34 sec';
286286

287+
-- edge case for sign-matching rules
288+
SELECT interval ''; -- error
289+
287290
-- test outputting iso8601 intervals
288291
SET IntervalStyle to iso_8601;
289292
select interval '0' AS "zero",

0 commit comments

Comments
 (0)