@@ -157,23 +157,25 @@ public abstract class CExtNodes {
157
157
public static final int RUBY_TAG_THROW = 0x7 ;
158
158
public static final int RUBY_TAG_FATAL = 0x8 ;
159
159
160
+ /** We need up to 4 \0 bytes for UTF-32. Always use 4 for speed rather than checking the encoding min length.
161
+ * Corresponds to TERM_LEN() in MRI. */
162
+ public static final int NATIVE_STRING_TERMINATOR_LENGTH = 4 ;
163
+
160
164
public static Pointer newNativeStringPointer (RubyLanguage language , RubyContext context , int capacity ) {
161
- // We need up to 4 \0 bytes for UTF-32. Always use 4 for speed rather than checking the encoding min length.
162
- Pointer pointer = Pointer .mallocAutoRelease (language , context , capacity + 4 );
165
+ Pointer pointer = Pointer .mallocAutoRelease (language , context , capacity + NATIVE_STRING_TERMINATOR_LENGTH );
163
166
pointer .writeInt (capacity , 0 );
164
167
return pointer ;
165
168
}
166
169
167
170
public static Pointer newZeroedNativeStringPointer (RubyLanguage language , RubyContext context , int capacity ) {
168
- // We need up to 4 \0 bytes for UTF-32. Always use 4 for speed rather than checking the encoding min length.
169
- return Pointer .callocAutoRelease (language , context , capacity + 4 );
171
+ return Pointer .callocAutoRelease (language , context , capacity + NATIVE_STRING_TERMINATOR_LENGTH );
170
172
}
171
173
172
174
private static long getNativeStringCapacity (Pointer pointer ) {
173
175
final long nativeBufferSize = pointer .getSize ();
174
176
assert nativeBufferSize > 0 ;
175
- // Do not count the extra byte for \0 , like MRI.
176
- return nativeBufferSize - 1 ;
177
+ // Do not count the extra terminator bytes , like MRI.
178
+ return nativeBufferSize - NATIVE_STRING_TERMINATOR_LENGTH ;
177
179
}
178
180
179
181
@ Primitive (name = "call_with_c_mutex_and_frame" )
0 commit comments