Skip to content

Commit 1eb147a

Browse files
committed
GH-4950 LMDB: Correctly convert byte values to unsigned integers.
1 parent 7f640bc commit 1eb147a

3 files changed

Lines changed: 10 additions & 1 deletion

File tree

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/inlined/Integers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static long packShort(Literal literal) {
7777
}
7878

7979
static long packByte(Literal literal) {
80-
return ValueIds.createId(ValueIds.T_BYTE, literal.byteValue());
80+
return ValueIds.createId(ValueIds.T_BYTE, 0xFF & literal.byteValue());
8181
}
8282

8383
static long packUnsignedLong(Literal literal) {

core/sail/lmdb/src/test/java/org/eclipse/rdf4j/sail/lmdb/inlined/StringsTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package org.eclipse.rdf4j.sail.lmdb.inlined;
1212

1313
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertFalse;
1415
import static org.junit.jupiter.api.Assertions.assertNotEquals;
1516

1617
import org.eclipse.rdf4j.model.Literal;
@@ -26,6 +27,7 @@ void testPackStringWithinMaxLength() {
2627
ValueFactory valueFactory = SimpleValueFactory.getInstance();
2728
Literal literal = valueFactory.createLiteral("test", XSD.STRING);
2829
long packed = Strings.packString(literal);
30+
assertFalse(packed < 0, "Packed value should be non-negative for value: " + literal);
2931

3032
// Assert that the packed value is not 0
3133
assertNotEquals(0L, packed, "Packed value should not be 0 for valid input.");
@@ -38,6 +40,7 @@ void testPackStringExceedsMaxLength() {
3840
String longString = "a".repeat(Values.MAX_LENGTH);
3941
Literal literal = valueFactory.createLiteral(longString, XSD.STRING);
4042
long packed = Strings.packString(literal);
43+
assertFalse(packed < 0, "Packed value should be non-negative for value: " + literal);
4144

4245
// Assert that the packed value is 0
4346
assertEquals(0L, packed, "Packed value should be 0 for input exceeding max length.");
@@ -48,6 +51,7 @@ void testUnpackString() {
4851
ValueFactory valueFactory = SimpleValueFactory.getInstance();
4952
Literal literal = valueFactory.createLiteral("test", XSD.STRING);
5053
long packed = Strings.packString(literal);
54+
assertFalse(packed < 0, "Packed value should be non-negative for value: " + literal);
5155

5256
Literal unpackedLiteral = Strings.unpackString(packed, valueFactory);
5357

core/sail/lmdb/src/test/java/org/eclipse/rdf4j/sail/lmdb/inlined/ValuesTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package org.eclipse.rdf4j.sail.lmdb.inlined;
1212

1313
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.junit.jupiter.api.Assertions.assertFalse;
1415

1516
import java.math.BigDecimal;
1617
import java.math.BigInteger;
@@ -146,6 +147,8 @@ void testPackAndUnpack_AllLiteralTypesWithEdgeAndStandardCases() {
146147
for (Literal literal : literals) {
147148
try {
148149
long packed = Values.packLiteral(literal);
150+
assertFalse(packed < 0, "Packed value should be non-negative for value: " + literal);
151+
149152
// If the literal is not inlined, packed==0. Only test roundtrip if it is inlined.
150153
if (packed != 0L) {
151154
Literal unpacked = Values.unpackLiteral(packed, vf);
@@ -165,6 +168,8 @@ void testPackAndUnpack_AllLiteralTypesWithVarintConversion() {
165168
ByteBuffer bb = ByteBuffer.allocate(Long.BYTES + 1);
166169
for (Literal literal : literals) {
167170
long packed = Values.packLiteral(literal);
171+
assertFalse(packed < 0, "Packed value should be non-negative for value: " + literal);
172+
168173
// If the literal is not inlined, packed==0. Only test roundtrip if it is inlined.
169174
if (packed != 0L) {
170175
bb.clear();

0 commit comments

Comments
 (0)