Skip to content

Commit 67a3438

Browse files
committed
Account for null character in NativeCharSequence
1 parent aa2537d commit 67a3438

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

graalpython/com.oracle.graal.python.cext/src/unicodeobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,9 @@ PyObject* PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) {
512512
kind = PyUnicode_4BYTE_KIND;
513513
}
514514

515-
size_t n = size * kind;
516-
int8_t* ptr = (int8_t*) malloc(n);
517-
return _jls_PyUnicode_New(polyglot_from_i8_array((int8_t*)ptr, n), kind, is_ascii);
515+
/* add one to size for the null character */
516+
int8_t* ptr = (int8_t*) calloc(size + 1, kind);
517+
return _jls_PyUnicode_New(polyglot_from_i8_array((int8_t*)ptr, (size + 1) * kind), kind, is_ascii);
518518
}
519519

520520
UPCALL_ID(PyUnicode_Compare);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/NativeCharSequence.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ int length(InteropLibrary lib, CastToJavaIntExactNode castToJavaIntNode) {
9191
try {
9292
int arraySize = castToJavaIntNode.execute(lib.getArraySize(ptr));
9393
assert arraySize % elementSize == 0;
94+
// we need to subtract the terminating null character
9495
return arraySize / elementSize;
9596
} catch (UnsupportedMessageException e) {
9697
throw CompilerDirectives.shouldNotReachHere("pointer of NativeCharSequence is not an array");

0 commit comments

Comments
 (0)