Skip to content

ValueWriter.writeFieldValue allows BigDecimals that are too big to be written #617

Closed
@changlinli

Description

@changlinli

writeFieldValue has the following:

        else if(value instanceof BigDecimal) {
            writeOctet('D');
            BigDecimal decimal = (BigDecimal)value;
            writeOctet(decimal.scale());
            BigInteger unscaled = decimal.unscaledValue();
            if(unscaled.bitLength() > 32) /*Integer.SIZE in Java 1.5*/
                throw new IllegalArgumentException
                    ("BigDecimal too large to be encoded");
            writeLong(decimal.unscaledValue().intValue());
        }

but 32 is actually too permissive. It should be 31 because bitLength ignores the sign bit, meaning that writing new BigDecimal(Integer.Max_Value).add(new BigDecimal(1)) "works" (that is no exception is thrown) and yields a different number back when you read it back using ValueReader.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions