Skip to content

Commit

Permalink
8282252: Improve BigInteger/Decimal validation
Browse files Browse the repository at this point in the history
Reviewed-by: jboes, rhalade, skoivu, bpb, smarks
  • Loading branch information
jddarcy authored and slowhog committed Oct 18, 2022
1 parent ecfb6bc commit 25e88b2
Show file tree
Hide file tree
Showing 5 changed files with 377 additions and 56 deletions.
63 changes: 48 additions & 15 deletions src/java.base/share/classes/java/math/BigDecimal.java
Expand Up @@ -31,6 +31,10 @@

import static java.math.BigInteger.LONG_MASK;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import java.io.StreamCorruptedException;
import java.util.Arrays;
import java.util.Objects;

Expand Down Expand Up @@ -1047,6 +1051,15 @@ public BigDecimal(double val, MathContext mc) {
this.precision = prec;
}

/**
* Accept no subclasses.
*/
private static BigInteger toStrictBigInteger(BigInteger val) {
return (val.getClass() == BigInteger.class) ?
val :
new BigInteger(val.toByteArray().clone());
}

/**
* Translates a {@code BigInteger} into a {@code BigDecimal}.
* The scale of the {@code BigDecimal} is zero.
Expand All @@ -1056,8 +1069,8 @@ public BigDecimal(double val, MathContext mc) {
*/
public BigDecimal(BigInteger val) {
scale = 0;
intVal = val;
intCompact = compactValFor(val);
intVal = toStrictBigInteger(val);
intCompact = compactValFor(intVal);
}

/**
Expand All @@ -1071,7 +1084,7 @@ public BigDecimal(BigInteger val) {
* @since 1.5
*/
public BigDecimal(BigInteger val, MathContext mc) {
this(val,0,mc);
this(toStrictBigInteger(val), 0, mc);
}

/**
Expand All @@ -1085,8 +1098,8 @@ public BigDecimal(BigInteger val, MathContext mc) {
*/
public BigDecimal(BigInteger unscaledVal, int scale) {
// Negative scales are now allowed
this.intVal = unscaledVal;
this.intCompact = compactValFor(unscaledVal);
this.intVal = toStrictBigInteger(unscaledVal);
this.intCompact = compactValFor(this.intVal);
this.scale = scale;
}

Expand All @@ -1104,6 +1117,7 @@ public BigDecimal(BigInteger unscaledVal, int scale) {
* @since 1.5
*/
public BigDecimal(BigInteger unscaledVal, int scale, MathContext mc) {
unscaledVal = toStrictBigInteger(unscaledVal);
long compactVal = compactValFor(unscaledVal);
int mcp = mc.precision;
int prec = 0;
Expand Down Expand Up @@ -4253,9 +4267,13 @@ private static class UnsafeHolder {
= unsafe.objectFieldOffset(BigDecimal.class, "intCompact");
private static final long intValOffset
= unsafe.objectFieldOffset(BigDecimal.class, "intVal");
private static final long scaleOffset
= unsafe.objectFieldOffset(BigDecimal.class, "scale");

static void setIntCompact(BigDecimal bd, long val) {
unsafe.putLong(bd, intCompactOffset, val);
static void setIntValAndScale(BigDecimal bd, BigInteger intVal, int scale) {
unsafe.putReference(bd, intValOffset, intVal);
unsafe.putInt(bd, scaleOffset, scale);
unsafe.putLong(bd, intCompactOffset, compactValFor(intVal));
}

static void setIntValVolatile(BigDecimal bd, BigInteger val) {
Expand All @@ -4274,15 +4292,30 @@ static void setIntValVolatile(BigDecimal bd, BigInteger val) {
@java.io.Serial
private void readObject(java.io.ObjectInputStream s)
throws IOException, ClassNotFoundException {
// Read in all fields
s.defaultReadObject();
// validate possibly bad fields
if (intVal == null) {
String message = "BigDecimal: null intVal in stream";
throw new java.io.StreamCorruptedException(message);
// [all values of scale are now allowed]
// prepare to read the fields
ObjectInputStream.GetField fields = s.readFields();
BigInteger serialIntVal = (BigInteger) fields.get("intVal", null);

// Validate field data
if (serialIntVal == null) {
throw new StreamCorruptedException("Null or missing intVal in BigDecimal stream");
}
UnsafeHolder.setIntCompact(this, compactValFor(intVal));
// Validate provenance of serialIntVal object
serialIntVal = toStrictBigInteger(serialIntVal);

// Any integer value is valid for scale
int serialScale = fields.get("scale", 0);

UnsafeHolder.setIntValAndScale(this, serialIntVal, serialScale);
}

/**
* Serialization without data not supported for this class.
*/
@java.io.Serial
private void readObjectNoData()
throws ObjectStreamException {
throw new InvalidObjectException("Deserialized BigDecimal objects need data");
}

/**
Expand Down
45 changes: 27 additions & 18 deletions src/java.base/share/classes/java/math/BigInteger.java
Expand Up @@ -30,9 +30,11 @@
package java.math;

import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.io.ObjectStreamException;
import java.util.Arrays;
import java.util.Objects;
import java.util.Random;
Expand Down Expand Up @@ -4836,17 +4838,21 @@ private void readObject(java.io.ObjectInputStream s)
// prepare to read the alternate persistent fields
ObjectInputStream.GetField fields = s.readFields();

// Read the alternate persistent fields that we care about
int sign = fields.get("signum", -2);
byte[] magnitude = (byte[])fields.get("magnitude", null);
// Read and validate the alternate persistent fields that we
// care about, signum and magnitude

// Validate signum
// Read and validate signum
int sign = fields.get("signum", -2);
if (sign < -1 || sign > 1) {
String message = "BigInteger: Invalid signum value";
if (fields.defaulted("signum"))
message = "BigInteger: Signum not present in stream";
throw new java.io.StreamCorruptedException(message);
}

// Read and validate magnitude
byte[] magnitude = (byte[])fields.get("magnitude", null);
magnitude = magnitude.clone(); // defensive copy
int[] mag = stripLeadingZeroBytes(magnitude, 0, magnitude.length);
if ((mag.length == 0) != (sign == 0)) {
String message = "BigInteger: signum-magnitude mismatch";
Expand All @@ -4855,18 +4861,24 @@ private void readObject(java.io.ObjectInputStream s)
throw new java.io.StreamCorruptedException(message);
}

// Equivalent to checkRange() on mag local without assigning
// this.mag field
if (mag.length > MAX_MAG_LENGTH ||
(mag.length == MAX_MAG_LENGTH && mag[0] < 0)) {
throw new java.io.StreamCorruptedException("BigInteger: Out of the supported range");
}

// Commit final fields via Unsafe
UnsafeHolder.putSign(this, sign);
UnsafeHolder.putSignAndMag(this, sign, mag);
}

// Calculate mag field from magnitude and discard magnitude
UnsafeHolder.putMag(this, mag);
if (mag.length >= MAX_MAG_LENGTH) {
try {
checkRange();
} catch (ArithmeticException e) {
throw new java.io.StreamCorruptedException("BigInteger: Out of the supported range");
}
}
/**
* Serialization without data not supported for this class.
*/
@java.io.Serial
private void readObjectNoData()
throws ObjectStreamException {
throw new InvalidObjectException("Deserialized BigInteger objects need data");
}

// Support for resetting final fields while deserializing
Expand All @@ -4878,11 +4890,8 @@ private static class UnsafeHolder {
private static final long magOffset
= unsafe.objectFieldOffset(BigInteger.class, "mag");

static void putSign(BigInteger bi, int sign) {
static void putSignAndMag(BigInteger bi, int sign, int[] magnitude) {
unsafe.putInt(bi, signumOffset, sign);
}

static void putMag(BigInteger bi, int[] magnitude) {
unsafe.putReference(bi, magOffset, magnitude);
}
}
Expand Down
64 changes: 64 additions & 0 deletions test/jdk/java/math/BigDecimal/ConstructorUnscaledValue.java
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8282252
* @summary Test constructors of BigDecimal to replace BigInteger subclasses
*/

import java.math.*;

public class ConstructorUnscaledValue {
public static void main(String... args) {
TestBigInteger tbi = new TestBigInteger(BigInteger.ONE);
// Create BigDecimal's using each of the three constructors
// with guards on the class of unscaledValue
BigDecimal[] values = {
new BigDecimal(tbi),
new BigDecimal(tbi, 2),
new BigDecimal(tbi, 3, MathContext.DECIMAL32),
};

for (var bd : values) {
BigInteger unscaledValue = bd.unscaledValue();
if (unscaledValue.getClass() != BigInteger.class) {
throw new RuntimeException("Bad class for unscaledValue");
}
if (!unscaledValue.equals(BigInteger.ONE)) {
throw new RuntimeException("Bad value for unscaledValue");
}
}
}

private static class TestBigInteger extends BigInteger {
public TestBigInteger(BigInteger bi) {
super(bi.toByteArray());
}

@Override
public String toString() {
return java.util.Arrays.toString(toByteArray());
}
}
}

0 comments on commit 25e88b2

Please sign in to comment.