Report
to_number() builds a numeric typmod from the pattern's digit counts without range checks, so a pattern with 1024 or more fractional digit positions rounds the result (1500 digits gives 0) and 65536+ integer positions overflow the int and wrap the precision
- Severity
- 14
- Blamed commit
- b866d2e2d79416f8497e4dffa7e800298d018f6c
On 27 branches
REL7_1_STABLE, REL7_2_STABLE, REL7_3_STABLE, REL7_4_STABLE, REL8_0_STABLE, REL8_1_STABLE, REL8_2_STABLE, REL8_3_STABLE, REL8_4_STABLE, REL9_0_STABLE, REL9_1_STABLE, REL9_2_STABLE, REL9_3_STABLE, REL9_4_STABLE, REL9_5_STABLE, REL9_6_STABLE, REL_10_STABLE, REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
numeric_to_number() converts the text with numeric_in() and passes a typmod computed from the pattern:
scale = Num.post;
precision = Num.pre + Num.multi + scale;
... Int32GetDatum(((precision << 16) | scale) + VARHDRSZ)
Nothing checks that the values fit the numeric typmod encoding, which holds the scale in 11 signed bits (-1024..1023) and the precision in 16 bits. SQL-level numeric(p,s) typmods are validated to p <= 1000 and s <= 1000, but this call site bypasses that validation.
- With 1024 or more fractional digits read, the scale bits wrap (to -1024 at exactly 1024, then upward), and the plain OR also spills scale bits into the precision field, so the value is rounded to a wrong scale. A scale that wraps negative rounds away the whole number.
- With 65536 or more digit positions in the pattern, precision << 16 overflows a signed int (undefined behavior) and the precision field wraps modulo 65536, so valid numbers are rejected with "numeric field overflow".
numeric itself supports 131072 integer and 16383 fractional digits, and the same text read without a typmod (numeric_in with -1) is exact.
Reproduction
select scale(to_number('1.' || repeat('1', 1023), '9.' || repeat('9', 1023))); -- 1023
select scale(to_number('1.' || repeat('1', 1024), '9.' || repeat('9', 1024))); -- 0
select to_number('1.' || repeat('1', 1500), '9.' || repeat('9', 1500)); -- 0
select scale(to_number('1.' || repeat('1', 2500), '9.' || repeat('9', 2500))); -- 452
select length(to_number(repeat('1', 5000), repeat('9', 5000))::text); -- 5000
select length(to_number(repeat('1', 5000), repeat('9', 70000))::text);
-- ERROR: numeric field overflow
-- DETAIL: A field with precision 4464, scale 0 must round to an
-- absolute value less than 10^4464.
Expected: the exact value 1.1111... with all digits, and 5000 ones.
Analysis
numeric_in() applies the typmod with apply_typmod(), which decodes precision as ((typmod - VARHDRSZ) >> 16) & 0xffff and scale as (((typmod - VARHDRSZ) & 0x7ff) ^ 1024) - 1024. The scale is written into the low 16 bits with a plain OR, so scale values above 2047 also corrupt the precision bits. The code dates from the original to_char family patch.
Notes
Tested on master b7822f9992216f9aa85422337c15f1e7a8d6db72.
Branches
Reproduced on master. The code dates from the original to_char() patch, so all supported branches likely behave the same; they were not tested.