Skip to content

Commit 5656f2c

Browse files
committed
Fix out-of-bound memory access for interval -> char conversion
Using Roman numbers (via "RM" or "rm") for a conversion to calculate a number of months has never considered the case of negative numbers, where a conversion could easily cause out-of-bound memory accesses. The conversions in themselves were not completely consistent either, as specifying 12 would result in NULL, but it should mean XII. This commit reworks the conversion calculation to have a more consistent behavior: - If the number of months and years is 0, return NULL. - If the number of months is positive, return the exact month number. - If the number of months is negative, do a backward calculation, with -1 meaning December, -2 November, etc. Reported-by: Theodor Arsenij Larionov-Trichkin Author: Julien Rouhaud Discussion: https://postgr.es/m/16953-f255a18f8c51f1d5@postgresql.org backpatch-through: 9.6
1 parent 721b3a3 commit 5656f2c

File tree

3 files changed

+95
-10
lines changed

3 files changed

+95
-10
lines changed

src/backend/utils/adt/formatting.c

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,18 +2936,61 @@ DCH_to_char(FormatNode *node, bool is_interval, TmToChar *in, char *out, Oid col
29362936
s += strlen(s);
29372937
break;
29382938
case DCH_RM:
2939-
if (!tm->tm_mon)
2940-
break;
2941-
sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -4,
2942-
rm_months_upper[MONTHS_PER_YEAR - tm->tm_mon]);
2943-
s += strlen(s);
2944-
break;
2939+
/* FALLTHROUGH */
29452940
case DCH_rm:
2946-
if (!tm->tm_mon)
2941+
2942+
/*
2943+
* For intervals, values like '12 month' will be reduced to 0
2944+
* month and some years. These should be processed.
2945+
*/
2946+
if (!tm->tm_mon && !tm->tm_year)
29472947
break;
2948-
sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -4,
2949-
rm_months_lower[MONTHS_PER_YEAR - tm->tm_mon]);
2950-
s += strlen(s);
2948+
else
2949+
{
2950+
int mon = 0;
2951+
const char *const *months;
2952+
2953+
if (n->key->id == DCH_RM)
2954+
months = rm_months_upper;
2955+
else
2956+
months = rm_months_lower;
2957+
2958+
/*
2959+
* Compute the position in the roman-numeral array. Note
2960+
* that the contents of the array are reversed, December
2961+
* being first and January last.
2962+
*/
2963+
if (tm->tm_mon == 0)
2964+
{
2965+
/*
2966+
* This case is special, and tracks the case of full
2967+
* interval years.
2968+
*/
2969+
mon = tm->tm_year >= 0 ? 0 : MONTHS_PER_YEAR - 1;
2970+
}
2971+
else if (tm->tm_mon < 0)
2972+
{
2973+
/*
2974+
* Negative case. In this case, the calculation is
2975+
* reversed, where -1 means December, -2 November,
2976+
* etc.
2977+
*/
2978+
mon = -1 * (tm->tm_mon + 1);
2979+
}
2980+
else
2981+
{
2982+
/*
2983+
* Common case, with a strictly positive value. The
2984+
* position in the array matches with the value of
2985+
* tm_mon.
2986+
*/
2987+
mon = MONTHS_PER_YEAR - tm->tm_mon;
2988+
}
2989+
2990+
sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -4,
2991+
months[mon]);
2992+
s += strlen(s);
2993+
}
29512994
break;
29522995
case DCH_W:
29532996
sprintf(s, "%d", (tm->tm_mday - 1) / 7 + 1);

src/test/regress/expected/timestamp.out

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,42 @@ SELECT '' AS to_char_11, to_char(d1, 'FMIYYY FMIYY FMIY FMI FMIW FMIDDD FMID')
17011701
| 2001 1 1 1 1 1 1
17021702
(65 rows)
17031703

1704+
-- Roman months, with upper and lower case.
1705+
SELECT i,
1706+
to_char(i * interval '1mon', 'rm'),
1707+
to_char(i * interval '1mon', 'RM')
1708+
FROM generate_series(-13, 13) i;
1709+
i | to_char | to_char
1710+
-----+---------+---------
1711+
-13 | xii | XII
1712+
-12 | i | I
1713+
-11 | ii | II
1714+
-10 | iii | III
1715+
-9 | iv | IV
1716+
-8 | v | V
1717+
-7 | vi | VI
1718+
-6 | vii | VII
1719+
-5 | viii | VIII
1720+
-4 | ix | IX
1721+
-3 | x | X
1722+
-2 | xi | XI
1723+
-1 | xii | XII
1724+
0 | |
1725+
1 | i | I
1726+
2 | ii | II
1727+
3 | iii | III
1728+
4 | iv | IV
1729+
5 | v | V
1730+
6 | vi | VI
1731+
7 | vii | VII
1732+
8 | viii | VIII
1733+
9 | ix | IX
1734+
10 | x | X
1735+
11 | xi | XI
1736+
12 | xii | XII
1737+
13 | i | I
1738+
(27 rows)
1739+
17041740
-- timestamp numeric fields constructor
17051741
SELECT make_timestamp(2014,12,28,6,30,45.887);
17061742
make_timestamp

src/test/regress/sql/timestamp.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,5 +235,11 @@ SELECT '' AS to_char_10, to_char(d1, 'IYYY IYY IY I IW IDDD ID')
235235
SELECT '' AS to_char_11, to_char(d1, 'FMIYYY FMIYY FMIY FMI FMIW FMIDDD FMID')
236236
FROM TIMESTAMP_TBL;
237237

238+
-- Roman months, with upper and lower case.
239+
SELECT i,
240+
to_char(i * interval '1mon', 'rm'),
241+
to_char(i * interval '1mon', 'RM')
242+
FROM generate_series(-13, 13) i;
243+
238244
-- timestamp numeric fields constructor
239245
SELECT make_timestamp(2014,12,28,6,30,45.887);

0 commit comments

Comments
 (0)