Skip to content

validate scale before converting to BigInt #3864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/com/fasterxml/jackson/databind/JsonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.*;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.MissingNode;
Expand Down Expand Up @@ -608,8 +609,9 @@ public byte[] binaryValue() throws IOException {
* types returns <code>BigInteger.ZERO</code>.
*
* @return {@link BigInteger} value this node contains, if numeric node; <code>BigInteger.ZERO</code> for non-number nodes.
* @throws StreamConstraintsException if the scale of the underlying {@link BigDecimal} is too large
*/
public BigInteger bigIntegerValue() { return BigInteger.ZERO; }
public BigInteger bigIntegerValue() throws StreamConstraintsException { return BigInteger.ZERO; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually.... this is backwards-incompatible change, will need to wrap exception. Since I just merged I'll change this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, decided to use "sneaky throw" instead. Not ideal but will (have to) do for now.


/*
/**********************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,9 @@ public BigInteger deserialize(JsonParser p, DeserializationContext ctxt) throws
if (act == CoercionAction.AsEmpty) {
return (BigInteger) getEmptyValue(ctxt);
}
return p.getDecimalValue().toBigInteger();
final BigDecimal bd = p.getDecimalValue();
StreamReadConstraints.defaults().validateBigIntegerScale(bd.scale());
return bd.toBigInteger();
// 29-Jun-2020, tatu: New! "Scalar from Object" (mostly for XML)
case JsonTokenId.ID_START_OBJECT:
text = ctxt.extractScalarFromObject(p, this, _valueClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.math.BigInteger;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
import com.fasterxml.jackson.databind.*;

/**
Expand Down Expand Up @@ -85,7 +86,10 @@ public boolean canConvertToExactIntegral() {


@Override
public BigInteger bigIntegerValue() { return _value.toBigInteger(); }
public BigInteger bigIntegerValue() throws StreamConstraintsException {
StreamReadConstraints.defaults().validateBigIntegerScale(_value.scale());
return _value.toBigInteger();
}

@Override
public float floatValue() { return _value.floatValue(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.math.BigInteger;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.exc.StreamConstraintsException;

/**
* Intermediate value node used for numeric nodes.
Expand Down Expand Up @@ -31,7 +32,7 @@ public final JsonNodeType getNodeType()
@Override public abstract long longValue();
@Override public abstract double doubleValue();
@Override public abstract BigDecimal decimalValue();
@Override public abstract BigInteger bigIntegerValue();
@Override public abstract BigInteger bigIntegerValue() throws StreamConstraintsException;

@Override public abstract boolean canConvertToInt();
@Override public abstract boolean canConvertToLong();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1812,7 +1812,9 @@ public BigInteger getBigIntegerValue() throws IOException
if (n instanceof BigInteger) {
return (BigInteger) n;
} else if (n instanceof BigDecimal) {
return ((BigDecimal) n).toBigInteger();
final BigDecimal bd = (BigDecimal) n;
streamReadConstraints().validateBigIntegerScale(bd.scale());
return bd.toBigInteger();
}
// int/long is simple, but let's also just truncate float/double:
return BigInteger.valueOf(n.longValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.databind.node;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
Expand Down Expand Up @@ -202,7 +203,7 @@ public void testBasicsPutSet()
assertEquals("foobar", old.textValue());
}

public void testBigNumbers()
public void testBigNumbers() throws IOException
{
ObjectNode n = new ObjectNode(JsonNodeFactory.instance);
assertStandardEquals(n);
Expand Down