Skip to content

msgpack-lite: fix BigInteger pack / unpack #121

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 1 commit into from
Jan 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
25 changes: 25 additions & 0 deletions src/main/java/org/tarantool/MsgPackLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public class MsgPackLite {
protected final int MAX_31BIT = 0x7fffffff;
protected final long MAX_32BIT = 0xffffffffL;

protected final BigInteger BI_MIN_LONG = BigInteger.valueOf(Long.MIN_VALUE);
protected final BigInteger BI_MAX_LONG = BigInteger.valueOf(Long.MAX_VALUE);
protected final BigInteger BI_MAX_64BIT = BigInteger.valueOf(2).pow(64).subtract(BigInteger.ONE);

//these values are from http://wiki.msgpack.org/display/MSGPACK/Format+specification
protected final byte MP_NULL = (byte) 0xc0;
protected final byte MP_FALSE = (byte) 0xc2;
Expand Down Expand Up @@ -91,6 +95,23 @@ public void pack(Object item, OutputStream os) throws IOException {
out.write(MP_DOUBLE);
out.writeDouble((Double) item);
} else {
if (item instanceof BigInteger) {
BigInteger value = (BigInteger) item;
boolean isPositive = value.signum() >= 0;
if (isPositive && value.compareTo(BI_MAX_64BIT) > 0 ||
value.compareTo(BI_MIN_LONG) < 0)
throw new IllegalArgumentException(
"Cannot encode BigInteger as MsgPack: out of -2^63..2^64-1 range");
if (isPositive && value.compareTo(BI_MAX_LONG) > 0) {
byte[] data = value.toByteArray();
// data can contain leading zero bytes
for (int i = 0; i < data.length - 8; ++i)
assert data[i] == 0;
out.write(MP_UINT64);
out.write(data, data.length - 8, 8);
return;
}
}
long value = item instanceof Code ? ((Code) item).getId() : ((Number) item).longValue();
if (value >= 0) {
if (value <= MAX_7BIT) {
Expand Down Expand Up @@ -238,6 +259,10 @@ public Object unpack(InputStream is) throws IOException {
} else {
//this is a little bit more tricky, since we don't have unsigned longs
byte[] bytes = new byte[]{
(byte) ((v >> 56) & 0xff),
(byte) ((v >> 48) & 0xff),
(byte) ((v >> 40) & 0xff),
(byte) ((v >> 32) & 0xff),
(byte) ((v >> 24) & 0xff),
(byte) ((v >> 16) & 0xff),
(byte) ((v >> 8) & 0xff),
Expand Down
35 changes: 29 additions & 6 deletions src/test/java/org/tarantool/AbstractTarantoolConnectorIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.opentest4j.AssertionFailedError;

import java.math.BigInteger;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
Expand Down Expand Up @@ -180,27 +181,49 @@ protected TarantoolConnection openConnection() {
}
}

protected List<?> consoleSelect(String spaceName, Object key) {
StringBuilder sb = new StringBuilder("box.space.");
sb.append(spaceName);
sb.append(":select{");
private void appendBigInteger(StringBuilder sb, BigInteger value) {
sb.append(value);
sb.append(value.signum() >= 0 ? "ULL" : "LL");
}

private void appendKey(StringBuilder sb, Object key) {
if (List.class.isAssignableFrom(key.getClass())) {
List parts = (List)key;
List parts = (List) key;
for (int i = 0; i < parts.size(); i++) {
if (i != 0)
sb.append(", ");
Object k = parts.get(i);
if (k.getClass().isAssignableFrom(String.class)) {
if (k instanceof BigInteger) {
appendBigInteger(sb, (BigInteger) k);
} else if (k instanceof String) {
sb.append('\'');
sb.append(k);
sb.append('\'');
} else {
sb.append(k);
}
}
} else if (key instanceof BigInteger) {
appendBigInteger(sb, (BigInteger) key);
} else {
sb.append(key);
}
}

protected List<?> consoleSelect(String spaceName, Object key) {
StringBuilder sb = new StringBuilder("box.space.");
sb.append(spaceName);
sb.append(":select{");
appendKey(sb, key);
sb.append("}");
return console.eval(sb.toString());
}

protected List<?> consoleDelete(String spaceName, Object key) {
StringBuilder sb = new StringBuilder("box.space.");
sb.append(spaceName);
sb.append(":delete{");
appendKey(sb, key);
sb.append("}");
return console.eval(sb.toString());
}
Expand Down
27 changes: 26 additions & 1 deletion src/test/java/org/tarantool/AbstractTarantoolOpsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -61,6 +62,25 @@ public void testInsertSimple() {

// Check it actually was inserted.
checkTupleResult(consoleSelect(SPACE_NAME, 100), tup);

// Leave the database in a clean state.
consoleDelete(SPACE_NAME, 100);
}

@Test
public void testInsertBigInteger() {
BigInteger id = BigInteger.valueOf(2).pow(64).subtract(BigInteger.ONE);

List tup = Arrays.asList(id, "big");
List<?> res = getOps().insert(SPACE_ID, tup);

checkTupleResult(res, tup);

// Check it actually was inserted.
checkTupleResult(consoleSelect(SPACE_NAME, id), tup);

// Leave the database in a clean state.
consoleDelete(SPACE_NAME, id);
}

@Test
Expand All @@ -70,8 +90,13 @@ public void testInsertMultiPart() {

checkTupleResult(res, tup);

List<?> id = Arrays.asList(100, "hundred");

// Check it actually was inserted.
checkTupleResult(consoleSelect(MULTIPART_SPACE_NAME, Arrays.asList(100, "hundred")), tup);
checkTupleResult(consoleSelect(MULTIPART_SPACE_NAME, id), tup);

// Leave the database in a clean state.
consoleDelete(MULTIPART_SPACE_NAME, id);
}

@Test
Expand Down