Skip to content

Commit 08c983f

Browse files
committed
Use native encoded bytes when making a downcall to standard printf
When making a downcall to standard printf function, use native encoded bytes as argument instead of UTF-8 bytes. Signed-off-by: Rahil Shah <[email protected]>
1 parent 50b63b2 commit 08c983f

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

test/jdk/java/foreign/StdLibTest.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
* questions.
2222
*/
2323

24+
/*
25+
* ===========================================================================
26+
* (c) Copyright IBM Corp. 2024, 2024 All Rights Reserved
27+
* ===========================================================================
28+
*/
29+
2430
/*
2531
* @test
2632
* @run testng/othervm --enable-native-access=ALL-UNNAMED StdLibTest
@@ -305,8 +311,10 @@ int rand() throws Throwable {
305311
}
306312

307313
int printf(String format, List<PrintfArg> args) throws Throwable {
314+
byte[] nativeEncodingBytes = format.getBytes(System.getProperty("native.encoding"));
315+
nativeEncodingBytes = Arrays.copyOf(nativeEncodingBytes, nativeEncodingBytes.length + 1);
308316
try (var arena = Arena.ofConfined()) {
309-
MemorySegment formatStr = arena.allocateFrom(format);
317+
MemorySegment formatStr = arena.allocateArray(ValueLayout.JAVA_BYTE, nativeEncodingBytes);
310318
return (int)specializedPrintf(args).invokeExact(formatStr,
311319
args.stream().map(a -> a.nativeValue(arena)).toArray());
312320
}

0 commit comments

Comments
 (0)