]> granicus.if.org Git - postgresql/commitdiff
Fix NUMERIC field access macros to treat NaNs consistently.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 27 Jan 2015 17:06:36 +0000 (12:06 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 27 Jan 2015 17:06:36 +0000 (12:06 -0500)
Commit 145343534c153d1e6c3cff1fa1855787684d9a38 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.

src/backend/utils/adt/numeric.c

index e67ee91ccda752bdf6cac9523c0693836b7dcd9f..eed0955793d7a1e0e62557076c0853d86e9ce7d0 100644 (file)
@@ -168,9 +168,10 @@ struct NumericData
  * otherwise, we want the long one.  Instead of testing against each value, we
  * can just look at the high bit, for a slight efficiency gain.
  */
+#define NUMERIC_HEADER_IS_SHORT(n)     (((n)->choice.n_header & 0x8000) != 0)
 #define NUMERIC_HEADER_SIZE(n) \
        (VARHDRSZ + sizeof(uint16) + \
-               (((NUMERIC_FLAGBITS(n) & 0x8000) == 0) ? sizeof(int16) : 0))
+        (NUMERIC_HEADER_IS_SHORT(n) ? 0 : sizeof(int16)))
 
 /*
  * Short format definitions.
@@ -196,11 +197,11 @@ struct NumericData
        (NUMERIC_IS_SHORT(n) ? \
                (((n)->choice.n_short.n_header & NUMERIC_SHORT_SIGN_MASK) ? \
                NUMERIC_NEG : NUMERIC_POS) : NUMERIC_FLAGBITS(n))
-#define NUMERIC_DSCALE(n)      (NUMERIC_IS_SHORT((n)) ? \
+#define NUMERIC_DSCALE(n)      (NUMERIC_HEADER_IS_SHORT((n)) ? \
        ((n)->choice.n_short.n_header & NUMERIC_SHORT_DSCALE_MASK) \
                >> NUMERIC_SHORT_DSCALE_SHIFT \
        : ((n)->choice.n_long.n_sign_dscale & NUMERIC_DSCALE_MASK))
-#define NUMERIC_WEIGHT(n)      (NUMERIC_IS_SHORT((n)) ? \
+#define NUMERIC_WEIGHT(n)      (NUMERIC_HEADER_IS_SHORT((n)) ? \
        (((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_SIGN_MASK ? \
                ~NUMERIC_SHORT_WEIGHT_MASK : 0) \
         | ((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_MASK)) \
@@ -361,7 +362,7 @@ static void dump_var(const char *str, NumericVar *var);
 
 #define init_var(v)            MemSetAligned(v, 0, sizeof(NumericVar))
 
-#define NUMERIC_DIGITS(num) (NUMERIC_IS_SHORT(num) ? \
+#define NUMERIC_DIGITS(num) (NUMERIC_HEADER_IS_SHORT(num) ? \
        (num)->choice.n_short.n_data : (num)->choice.n_long.n_data)
 #define NUMERIC_NDIGITS(num) \
        ((VARSIZE(num) - NUMERIC_HEADER_SIZE(num)) / sizeof(NumericDigit))