Skip to content

Commit 70851f4

Browse files
committed
NumericSupport: fix serialization of BigInteger
1 parent 4d80b34 commit 70851f4

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/util/NumericSupport.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -315,7 +315,7 @@ public void putBigInteger(byte[] buffer, int index, BigInteger value, int numByt
315315
final byte[] src = value.toByteArray();
316316
int srcIndex = 0;
317317
int srcBytes = src.length;
318-
if (src.length > numBytes) {
318+
if (srcBytes > numBytes) {
319319
for (int i = 0; i < src.length; i++) {
320320
srcIndex = i;
321321
if (src[i] != 0) {
@@ -326,6 +326,13 @@ public void putBigInteger(byte[] buffer, int index, BigInteger value, int numByt
326326
if (srcBytes > numBytes) {
327327
throw OverflowException.INSTANCE;
328328
}
329+
} else if (srcBytes < numBytes) {
330+
// perform sign extension
331+
if (value.signum() < 0) {
332+
for (int i = 0; i < numBytes - srcBytes; i++) {
333+
buffer[i] = -1;
334+
}
335+
}
329336
}
330337
int dstIndex = index + (numBytes - srcBytes);
331338
PythonUtils.arraycopy(src, srcIndex, buffer, dstIndex, srcBytes);

0 commit comments

Comments
 (0)