Skip to content

Commit ae6d2b8

Browse files
committed
Write terminating 0 byte
1 parent ad28863 commit ae6d2b8

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.cext.common;
4242

43+
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbol.FUN_GET_BYTE_ARRAY_TYPE_ID;
44+
45+
import java.lang.reflect.Field;
46+
import java.nio.ByteBuffer;
47+
import java.nio.charset.StandardCharsets;
48+
4349
import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext.LLVMType;
4450
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.GetLLVMType;
4551
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.IsPointerNode;
@@ -63,13 +69,8 @@
6369
import com.oracle.truffle.api.library.ExportLibrary;
6470
import com.oracle.truffle.api.library.ExportMessage;
6571
import com.oracle.truffle.llvm.spi.NativeTypeLibrary;
66-
import sun.misc.Unsafe;
6772

68-
import java.lang.reflect.Field;
69-
import java.nio.ByteBuffer;
70-
import java.nio.charset.StandardCharsets;
71-
72-
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbol.FUN_GET_BYTE_ARRAY_TYPE_ID;
73+
import sun.misc.Unsafe;
7374

7475
/**
7576
* Native wrappers for managed objects such that they can be used as a C array by native code. The
@@ -98,10 +99,13 @@ private static Unsafe getUnsafe() {
9899
* the copies the contents to the native memory.
99100
*/
100101
@TruffleBoundary
101-
public static long byteArrayToNativeInt8(byte[] data) {
102-
int size = data.length * Byte.BYTES;
102+
public static long byteArrayToNativeInt8(byte[] data, boolean writeNullTerminator) {
103+
int size = (data.length + (writeNullTerminator ? 1 : 0)) * Byte.BYTES;
103104
long ptr = UNSAFE.allocateMemory(size);
104105
UNSAFE.copyMemory(data, Unsafe.ARRAY_BYTE_BASE_OFFSET, null, ptr, size);
106+
if (writeNullTerminator) {
107+
UNSAFE.putByte(ptr + data.length * Byte.BYTES, (byte) 0);
108+
}
105109
return ptr;
106110
}
107111

@@ -140,7 +144,7 @@ public static long stringToNativeUtf8Bytes(String string) {
140144
ByteBuffer encoded = StandardCharsets.UTF_8.encode(string);
141145
byte[] data = new byte[encoded.remaining() + 1];
142146
encoded.get(data, 0, data.length - 1);
143-
return byteArrayToNativeInt8(data);
147+
return byteArrayToNativeInt8(data, true);
144148
}
145149

146150
@TruffleBoundary
@@ -326,7 +330,7 @@ void toNative(
326330
@Exclusive @Cached InvalidateNativeObjectsAllManagedNode invalidateNode) {
327331
invalidateNode.execute();
328332
if (!lib.isNative(this)) {
329-
setNativePointer(byteArrayToNativeInt8(getByteArray(lib)));
333+
setNativePointer(byteArrayToNativeInt8(getByteArray(lib), true));
330334
}
331335
}
332336
}

graalpython/lib-graalpython/modules/hpy/test/test_argparse.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ def test_b(self):
8484
"function unsigned byte integer is less than minimum"
8585
)
8686

87-
# TODO(fa): needs to be fixed
88-
@pytest.mark.skip
8987
def test_s(self):
9088
import pytest
9189
mod = self.make_parse_item("s", "const char*", "HPyUnicode_FromString")

0 commit comments

Comments
 (0)