Skip to content

Commit ecb31ba

Browse files
committed
Add additional fix for not checking scale
The scale of a BigDecimal must also be checked because it must be a signed octet
1 parent a4c5a1a commit ecb31ba

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/main/java/com/rabbitmq/client/impl/ValueWriter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ else if(value instanceof Integer) {
143143
else if(value instanceof BigDecimal) {
144144
writeOctet('D');
145145
BigDecimal decimal = (BigDecimal)value;
146+
// The scale must be an unsigned octet, therefore its values must
147+
// be between 0 and 255
148+
if(decimal.scale() > 255 || decimal.scale() < 0)
149+
throw new IllegalArgumentException
150+
("BigDecimal has too large of a scale to be encoded. " +
151+
"The scale was: " + decimal.scale());
146152
writeOctet(decimal.scale());
147153
BigInteger unscaled = decimal.unscaledValue();
148154
// We use 31 instead of 32 because bitLength ignores the sign bit,

src/test/java/com/rabbitmq/client/impl/ValueWriterTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
import java.io.IOException;
77
import java.io.OutputStream;
88
import java.math.BigDecimal;
9+
import java.math.BigInteger;
910
import java.util.ArrayDeque;
1011
import java.util.Queue;
1112

1213
public class ValueWriterTest {
13-
@Test(expected = IllegalArgumentException.class) public void writingOverlyLargeBigDecimalShouldFail() throws IOException {
14+
@Test(expected = IllegalArgumentException.class) public void writingOverlyLargeBigDecimalShouldFail()
15+
throws IOException {
1416
Queue<Byte> queue = new ArrayDeque<>();
1517

1618
OutputStream outputStream = new OutputStream() {
@@ -27,4 +29,22 @@ public void write(int b) {
2729
valueWriter.writeFieldValue(new BigDecimal(Integer.MAX_VALUE).add(new BigDecimal(1)));
2830

2931
}
32+
33+
@Test(expected = IllegalArgumentException.class) public void writingOverlyLargeScaleInBigDecimalShouldFail()
34+
throws IOException {
35+
Queue<Byte> queue = new ArrayDeque<>();
36+
37+
OutputStream outputStream = new OutputStream() {
38+
@Override
39+
public void write(int b) {
40+
queue.add((byte) b);
41+
}
42+
};
43+
44+
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
45+
46+
ValueWriter valueWriter = new ValueWriter(dataOutputStream);
47+
48+
valueWriter.writeFieldValue(new BigDecimal(BigInteger.ONE, 500));
49+
}
3050
}

0 commit comments

Comments
 (0)