Skip to content

Change # of bits ValueWriter checks for BigDecimal #618

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 4 commits into from
Jun 28, 2019
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
10 changes: 9 additions & 1 deletion src/main/java/com/rabbitmq/client/impl/ValueWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,17 @@ else if(value instanceof Integer) {
else if(value instanceof BigDecimal) {
writeOctet('D');
BigDecimal decimal = (BigDecimal)value;
// The scale must be an unsigned octet, therefore its values must
// be between 0 and 255
if(decimal.scale() > 255 || decimal.scale() < 0)
throw new IllegalArgumentException
("BigDecimal has too large of a scale to be encoded. " +
"The scale was: " + decimal.scale());
writeOctet(decimal.scale());
BigInteger unscaled = decimal.unscaledValue();
if(unscaled.bitLength() > 32) /*Integer.SIZE in Java 1.5*/
// We use 31 instead of 32 because bitLength ignores the sign bit,
// so e.g. new BigDecimal(Integer.MAX_VALUE) comes out to 31 bits.
if(unscaled.bitLength() > 31) /*Integer.SIZE in Java 1.5*/
throw new IllegalArgumentException
("BigDecimal too large to be encoded");
writeLong(decimal.unscaledValue().intValue());
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/com/rabbitmq/client/impl/ValueWriterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.rabbitmq.client.impl;

import org.junit.Test;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.BigInteger;

public class ValueWriterTest {
@Test(expected = IllegalArgumentException.class) public void writingOverlyLargeBigDecimalShouldFail()
throws IOException {

OutputStream outputStream = new OutputStream() {
@Override
public void write(int b) {
}
};

DataOutputStream dataOutputStream = new DataOutputStream(outputStream);

ValueWriter valueWriter = new ValueWriter(dataOutputStream);

valueWriter.writeFieldValue(new BigDecimal(Integer.MAX_VALUE).add(new BigDecimal(1)));

}

@Test(expected = IllegalArgumentException.class) public void writingOverlyLargeScaleInBigDecimalShouldFail()
throws IOException {

OutputStream outputStream = new OutputStream() {
@Override
public void write(int b) {
}
};

DataOutputStream dataOutputStream = new DataOutputStream(outputStream);

ValueWriter valueWriter = new ValueWriter(dataOutputStream);

valueWriter.writeFieldValue(new BigDecimal(BigInteger.ONE, 500));
}
}