Skip to content

Commit db03350

Browse files
lukasstadlerfangerer
authored andcommitted
raise proper error for negative values in _PyLong_AsByteArray
1 parent 980d063 commit db03350

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextLongBuiltins.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,9 @@ static Object get(int value, Object bytes, long n, int littleEndian, int isSigne
420420
@Shared @Cached InlinedConditionProfile profile,
421421
@Shared @Cached PRaiseNode raise,
422422
@Shared @Cached CStructAccess.WriteByteNode write) {
423+
if (isSigned == 0 && value < 0) {
424+
throw raise.raise(OverflowError, ErrorMessages.MESSAGE_CONVERT_NEGATIVE);
425+
}
423426

424427
byte[] array = IntBuiltins.ToBytesNode.fromLong(value, PythonUtils.toIntError(n), littleEndian == 0, isSigned != 0, inliningTarget, profile, raise);
425428
write.writeByteArray(bytes, array);
@@ -432,6 +435,9 @@ static Object get(long value, Object bytes, long n, int littleEndian, int isSign
432435
@Shared @Cached InlinedConditionProfile profile,
433436
@Shared @Cached PRaiseNode raise,
434437
@Shared @Cached CStructAccess.WriteByteNode write) {
438+
if (isSigned == 0 && value < 0) {
439+
throw raise.raise(OverflowError, ErrorMessages.MESSAGE_CONVERT_NEGATIVE);
440+
}
435441

436442
byte[] array = IntBuiltins.ToBytesNode.fromLong(value, PythonUtils.toIntError(n), littleEndian == 0, isSigned != 0, inliningTarget, profile, raise);
437443
write.writeByteArray(bytes, array);
@@ -444,6 +450,9 @@ static Object get(PInt value, Object bytes, long n, int littleEndian, int isSign
444450
@Shared @Cached InlinedConditionProfile profile,
445451
@Shared @Cached PRaiseNode raise,
446452
@Shared @Cached CStructAccess.WriteByteNode write) {
453+
if (isSigned == 0 && value.isNegative()) {
454+
throw raise.raise(OverflowError, ErrorMessages.MESSAGE_CONVERT_NEGATIVE);
455+
}
447456

448457
byte[] array = IntBuiltins.ToBytesNode.fromBigInteger(value, PythonUtils.toIntError(n), littleEndian == 0, isSigned != 0, inliningTarget, profile, raise);
449458
write.writeByteArray(bytes, array);

0 commit comments

Comments
 (0)