Avoid dereferencing an undefined pointer in DecodeInterval().
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 12 Feb 2023 17:50:55 +0000 (12:50 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 12 Feb 2023 17:50:55 +0000 (12:50 -0500)
Commit e39f99046 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

src/backend/utils/adt/datetime.c
src/test/regress/expected/interval.out
src/test/regress/sql/interval.sql

index d166613895868329373154a2f5d64ee81e93a028..b74889039d89754df8cacee262a16e6217ce98bc 100644 (file)
@@ -3376,7 +3376,7 @@ DecodeInterval(char **field, int *ftype, int nf, int range,
         * to dump in postgres style, not SQL style.)
         *----------
         */
-       if (IntervalStyle == INTSTYLE_SQL_STANDARD && *field[0] == '-')
+       if (IntervalStyle == INTSTYLE_SQL_STANDARD && nf > 0 && *field[0] == '-')
        {
                force_negative = true;
                /* Check for additional explicit signs */
index 579e92e7b392bf25239163c2a77d84138ffd9099..c7ac408bec803d60c4802b10e4f3a823d047f134 100644 (file)
@@ -888,6 +888,11 @@ SELECT  interval '-23 hours 45 min 12.34 sec',
  -23:45:12.34 | -1 23:45:12.34 | -1-2 -1 -23:45:12.34 | -0-10 +1 +23:45:12.34
 (1 row)
 
+-- edge case for sign-matching rules
+SELECT  interval '';  -- error
+ERROR:  invalid input syntax for type interval: ""
+LINE 1: SELECT  interval '';
+                         ^
 -- test outputting iso8601 intervals
 SET IntervalStyle to iso_8601;
 select  interval '0'                                AS "zero",
index 0517b5b82bd952430ef44eb390e15634c3d6c57f..54745c40d6d435efb4a28b72df14f0bf028b3614 100644 (file)
@@ -284,6 +284,9 @@ SELECT  interval '-23 hours 45 min 12.34 sec',
         interval '-1 year 2 months 1 day 23 hours 45 min 12.34 sec',
         interval '-1 year 2 months 1 day 23 hours 45 min +12.34 sec';
 
+-- edge case for sign-matching rules
+SELECT  interval '';  -- error
+
 -- test outputting iso8601 intervals
 SET IntervalStyle to iso_8601;
 select  interval '0'                                AS "zero",