Skip to content

Commit 2fa7c4b

Browse files
committed
[GR-18163] Fix rb_str_new_nul to use a 4-bytes NULL terminator
PullRequest: truffleruby/3906
2 parents b6c229a + fad5384 commit 2fa7c4b

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ public static Pointer newNativeStringPointer(RubyLanguage language, RubyContext
161161
return pointer;
162162
}
163163

164+
public static Pointer newZeroedNativeStringPointer(RubyLanguage language, RubyContext context, int capacity) {
165+
// We need up to 4 \0 bytes for UTF-32. Always use 4 for speed rather than checking the encoding min length.
166+
return Pointer.callocAutoRelease(language, context, capacity + 4);
167+
}
168+
164169
private static long getNativeStringCapacity(Pointer pointer) {
165170
final long nativeBufferSize = pointer.getSize();
166171
assert nativeBufferSize > 0;
@@ -783,7 +788,7 @@ public abstract static class RbStrNewNulNode extends CoreMethodArrayArgumentsNod
783788
@Specialization
784789
protected RubyString rbStrNewNul(int byteLength,
785790
@Cached MutableTruffleString.FromNativePointerNode fromNativePointerNode) {
786-
final Pointer pointer = Pointer.callocAutoRelease(getLanguage(), getContext(), byteLength + 1);
791+
final Pointer pointer = newZeroedNativeStringPointer(getLanguage(), getContext(), byteLength);
787792
var nativeTString = fromNativePointerNode.execute(pointer, 0, byteLength, Encodings.BINARY.tencoding,
788793
false);
789794
return createMutableString(nativeTString, Encodings.BINARY);

0 commit comments

Comments
 (0)