Skip to content

Commit 350f1e7

Browse files
committed
Fix NUMERIC field access macros to treat NaNs consistently.
Commit 1453435 arranged to store numeric NaN values as short-header numerics, but the field access macros did not get the memo: they thought only "SHORT" numerics have short headers. Most of the time this makes no difference because we don't access the weight or dscale of a NaN; but numeric_send does that. As pointed out by Andrew Gierth, this led to fetching uninitialized bytes. AFAICS this could not have any worse consequences than that; in particular, an unaligned stored numeric would have been detoasted by PG_GETARG_NUMERIC, so that there's no risk of a fetch off the end of memory. Still, the code is wrong on its own terms, and it's not hard to foresee future changes that might expose us to real risks. So back-patch to all affected branches.
1 parent 8abd0e2 commit 350f1e7

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/backend/utils/adt/numeric.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,10 @@ struct NumericData
168168
* otherwise, we want the long one. Instead of testing against each value, we
169169
* can just look at the high bit, for a slight efficiency gain.
170170
*/
171+
#define NUMERIC_HEADER_IS_SHORT(n) (((n)->choice.n_header & 0x8000) != 0)
171172
#define NUMERIC_HEADER_SIZE(n) \
172173
(VARHDRSZ + sizeof(uint16) + \
173-
(((NUMERIC_FLAGBITS(n) & 0x8000) == 0) ? sizeof(int16) : 0))
174+
(NUMERIC_HEADER_IS_SHORT(n) ? 0 : sizeof(int16)))
174175

175176
/*
176177
* Short format definitions.
@@ -196,11 +197,11 @@ struct NumericData
196197
(NUMERIC_IS_SHORT(n) ? \
197198
(((n)->choice.n_short.n_header & NUMERIC_SHORT_SIGN_MASK) ? \
198199
NUMERIC_NEG : NUMERIC_POS) : NUMERIC_FLAGBITS(n))
199-
#define NUMERIC_DSCALE(n) (NUMERIC_IS_SHORT((n)) ? \
200+
#define NUMERIC_DSCALE(n) (NUMERIC_HEADER_IS_SHORT((n)) ? \
200201
((n)->choice.n_short.n_header & NUMERIC_SHORT_DSCALE_MASK) \
201202
>> NUMERIC_SHORT_DSCALE_SHIFT \
202203
: ((n)->choice.n_long.n_sign_dscale & NUMERIC_DSCALE_MASK))
203-
#define NUMERIC_WEIGHT(n) (NUMERIC_IS_SHORT((n)) ? \
204+
#define NUMERIC_WEIGHT(n) (NUMERIC_HEADER_IS_SHORT((n)) ? \
204205
(((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_SIGN_MASK ? \
205206
~NUMERIC_SHORT_WEIGHT_MASK : 0) \
206207
| ((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_MASK)) \
@@ -351,7 +352,7 @@ static void dump_var(const char *str, NumericVar *var);
351352

352353
#define init_var(v) MemSetAligned(v, 0, sizeof(NumericVar))
353354

354-
#define NUMERIC_DIGITS(num) (NUMERIC_IS_SHORT(num) ? \
355+
#define NUMERIC_DIGITS(num) (NUMERIC_HEADER_IS_SHORT(num) ? \
355356
(num)->choice.n_short.n_data : (num)->choice.n_long.n_data)
356357
#define NUMERIC_NDIGITS(num) \
357358
((VARSIZE(num) - NUMERIC_HEADER_SIZE(num)) / sizeof(NumericDigit))

0 commit comments

Comments
 (0)