Skip to content

Commit 8eb1e9d

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 61fb800 commit 8eb1e9d

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
@@ -167,9 +167,10 @@ struct NumericData
167167
* otherwise, we want the long one. Instead of testing against each value, we
168168
* can just look at the high bit, for a slight efficiency gain.
169169
*/
170+
#define NUMERIC_HEADER_IS_SHORT(n) (((n)->choice.n_header & 0x8000) != 0)
170171
#define NUMERIC_HEADER_SIZE(n) \
171172
(VARHDRSZ + sizeof(uint16) + \
172-
(((NUMERIC_FLAGBITS(n) & 0x8000) == 0) ? sizeof(int16) : 0))
173+
(NUMERIC_HEADER_IS_SHORT(n) ? 0 : sizeof(int16)))
173174

174175
/*
175176
* Short format definitions.
@@ -195,11 +196,11 @@ struct NumericData
195196
(NUMERIC_IS_SHORT(n) ? \
196197
(((n)->choice.n_short.n_header & NUMERIC_SHORT_SIGN_MASK) ? \
197198
NUMERIC_NEG : NUMERIC_POS) : NUMERIC_FLAGBITS(n))
198-
#define NUMERIC_DSCALE(n) (NUMERIC_IS_SHORT((n)) ? \
199+
#define NUMERIC_DSCALE(n) (NUMERIC_HEADER_IS_SHORT((n)) ? \
199200
((n)->choice.n_short.n_header & NUMERIC_SHORT_DSCALE_MASK) \
200201
>> NUMERIC_SHORT_DSCALE_SHIFT \
201202
: ((n)->choice.n_long.n_sign_dscale & NUMERIC_DSCALE_MASK))
202-
#define NUMERIC_WEIGHT(n) (NUMERIC_IS_SHORT((n)) ? \
203+
#define NUMERIC_WEIGHT(n) (NUMERIC_HEADER_IS_SHORT((n)) ? \
203204
(((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_SIGN_MASK ? \
204205
~NUMERIC_SHORT_WEIGHT_MASK : 0) \
205206
| ((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_MASK)) \
@@ -350,7 +351,7 @@ static void dump_var(const char *str, NumericVar *var);
350351

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

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy