diff options
author | Tom Lane | 2022-04-02 20:12:26 +0000 |
---|---|---|
committer | Tom Lane | 2022-04-02 20:12:29 +0000 |
commit | e39f9904671082c5ad3a2c5acbdbd028fa93bf35 (patch) | |
tree | 6ab9ab1ed3b78bab977b0949e3cfcdb6af9f9cde /src/include | |
parent | f7e4d5c64fb3977e3a773e7213472be1b59dab2f (diff) |
Fix overflow hazards in interval input and output conversions.
DecodeInterval (interval input) was careless about integer-overflow
hazards, allowing bogus results to be obtained for sufficiently
large input values. Also, since it initially converted the input
to a "struct tm", it was impossible to produce the full range of
representable interval values.
Meanwhile, EncodeInterval (interval output) and a few other
functions could suffer failures if asked to process sufficiently
large interval values, because they also relied on being able to
represent an interval in "struct tm" which is not designed to
handle that.
Fix all this stuff by introducing new struct types that are more
fit for purpose.
While this is clearly a bug fix, it's also an API break for any
code that's calling these functions directly. So back-patching
doesn't seem wise, especially in view of the lack of field
complaints.
Joe Koshakow, editorialized a bit by me
Discussion: https://postgr.es/m/CAAvxfHff0JLYHwyBrtMx_=6wr=k2Xp+D+-X3vEhHjJYMj+mQcg@mail.gmail.com
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/datatype/timestamp.h | 39 | ||||
-rw-r--r-- | src/include/pgtime.h | 3 | ||||
-rw-r--r-- | src/include/utils/datetime.h | 6 | ||||
-rw-r--r-- | src/include/utils/timestamp.h | 5 |
4 files changed, 48 insertions, 5 deletions
diff --git a/src/include/datatype/timestamp.h b/src/include/datatype/timestamp.h index 5fa38d20d84..d155f1b03b0 100644 --- a/src/include/datatype/timestamp.h +++ b/src/include/datatype/timestamp.h @@ -40,6 +40,10 @@ typedef int64 TimestampTz; typedef int64 TimeOffset; typedef int32 fsec_t; /* fractional seconds (in microseconds) */ + +/* + * Storage format for type interval. + */ typedef struct { TimeOffset time; /* all time units other than days, months and @@ -48,6 +52,41 @@ typedef struct int32 month; /* months and years, after time for alignment */ } Interval; +/* + * Data structure representing a broken-down interval. + * + * For historical reasons, this is modeled on struct pg_tm for timestamps. + * Unlike the situation for timestamps, there's no magic interpretation + * needed for months or years: they're just zero or not. Note that fields + * can be negative; however, because of the divisions done while converting + * from struct Interval, only tm_mday could be INT_MIN. This is important + * because we may need to negate the values in some code paths. + */ +struct pg_itm +{ + int tm_usec; + int tm_sec; + int tm_min; + int64 tm_hour; /* needs to be wide */ + int tm_mday; + int tm_mon; + int tm_year; +}; + +/* + * Data structure for decoding intervals. We could just use struct pg_itm, + * but then the requirement for tm_usec to be 64 bits would propagate to + * places where it's not really needed. Also, omitting the fields that + * aren't used during decoding seems like a good error-prevention measure. + */ +struct pg_itm_in +{ + int64 tm_usec; /* needs to be wide */ + int tm_mday; + int tm_mon; + int tm_year; +}; + /* Limits on the "precision" option (typmod) for these data types */ #define MAX_TIMESTAMP_PRECISION 6 diff --git a/src/include/pgtime.h b/src/include/pgtime.h index 2977b13aabb..441d7847c12 100644 --- a/src/include/pgtime.h +++ b/src/include/pgtime.h @@ -23,6 +23,8 @@ typedef int64 pg_time_t; /* + * Data structure representing a broken-down timestamp. + * * CAUTION: the IANA timezone library (src/timezone/) follows the POSIX * convention that tm_mon counts from 0 and tm_year is relative to 1900. * However, Postgres' datetime functions generally treat tm_mon as counting @@ -44,6 +46,7 @@ struct pg_tm const char *tm_zone; }; +/* These structs are opaque outside the timezone library */ typedef struct pg_tz pg_tz; typedef struct pg_tzenum pg_tzenum; diff --git a/src/include/utils/datetime.h b/src/include/utils/datetime.h index 0d158f3e4bb..0801858d601 100644 --- a/src/include/utils/datetime.h +++ b/src/include/utils/datetime.h @@ -300,9 +300,9 @@ extern int DecodeTimeOnly(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp); extern int DecodeInterval(char **field, int *ftype, int nf, int range, - int *dtype, struct pg_tm *tm, fsec_t *fsec); + int *dtype, struct pg_itm_in *itm_in); extern int DecodeISO8601Interval(char *str, - int *dtype, struct pg_tm *tm, fsec_t *fsec); + int *dtype, struct pg_itm_in *itm_in); extern void DateTimeParseError(int dterr, const char *str, const char *datatype) pg_attribute_noreturn(); @@ -315,7 +315,7 @@ extern int DetermineTimeZoneAbbrevOffsetTS(TimestampTz ts, const char *abbr, extern void EncodeDateOnly(struct pg_tm *tm, int style, char *str); extern void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str); extern void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str); -extern void EncodeInterval(struct pg_tm *tm, fsec_t fsec, int style, char *str); +extern void EncodeInterval(struct pg_itm *itm, int style, char *str); extern void EncodeSpecialTimestamp(Timestamp dt, char *str); extern int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, diff --git a/src/include/utils/timestamp.h b/src/include/utils/timestamp.h index c1a74f8e2b0..d33421d3809 100644 --- a/src/include/utils/timestamp.h +++ b/src/include/utils/timestamp.h @@ -88,8 +88,9 @@ extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone); extern void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec); -extern int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec); -extern int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span); +extern void interval2itm(Interval span, struct pg_itm *itm); +extern int itm2interval(struct pg_itm *itm, Interval *span); +extern int itmin2interval(struct pg_itm_in *itm_in, Interval *span); extern Timestamp SetEpochTimestamp(void); extern void GetEpochTime(struct pg_tm *tm); |