Skip to content

Commit 03e4fa8

Browse files
committed
don't copy beyond bounds of byte[]
1 parent 86b16de commit 03e4fa8

File tree

1 file changed

+4
-4
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/common

1 file changed

+4
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/common/CArrayWrappers.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2023, 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
@@ -99,11 +99,11 @@ private static Unsafe getUnsafe() {
9999
*/
100100
@TruffleBoundary
101101
public static long byteArrayToNativeInt8(byte[] data, boolean writeNullTerminator) {
102-
int size = (data.length + (writeNullTerminator ? 1 : 0)) * Byte.BYTES;
103-
long ptr = UNSAFE.allocateMemory(size);
102+
int size = data.length * Byte.BYTES;
103+
long ptr = UNSAFE.allocateMemory(size + (writeNullTerminator ? Byte.BYTES : 0));
104104
UNSAFE.copyMemory(data, Unsafe.ARRAY_BYTE_BASE_OFFSET, null, ptr, size);
105105
if (writeNullTerminator) {
106-
UNSAFE.putByte(ptr + data.length * Byte.BYTES, (byte) 0);
106+
UNSAFE.putByte(ptr + size, (byte) 0);
107107
}
108108
return ptr;
109109
}

0 commit comments

Comments
 (0)