[foreign-memaccess+abi] RFR: 8268230: Foreign Linker API & Windows user32/kernel32: String conversion seems broken

Duncan Gittins duncan.gittins at gmail.com
Fri Jun 11 12:54:11 UTC 2021


I've had problems with Windows String conversions to/from wide string 
using Clinker toCString / toJavaString so switched to using kernel32.dll 
MultiByteToWideChar / WideCharToMultiByte. Hopefully your fix will 
address the issue with toCString(s, UTF_16LE).

I don't think reverse conversion works using Clinker.toJavaString. It 
may help to verify by changing 
"test/jdk/java/foreign/TestToCStringWide.java.testStrings()" to handled 
input array of strings:

     for (String testString : new String[] {"", "x", "testing"} ) { ...

.. and also checked the reverse operation returns the original:

        String outString = CLinker.toJavaString(text, charset);
        assertEquals(testString, outString);

Kind regards

Duncan


On 10/06/2021 13:12, Jorn Vernee wrote:
> The problem is that we only add a single 0 byte as a null terminator, regardless of the charset used. For wider char sets, more 0 bytes need to be added. For instance, for UTF_16LE two 0 bytes need to be added.
>
> This patch fixes the issue by adding the null terminator to the Java string, and only then encoding it as a `byte[]`.
>
> -------------
>
> Commit messages:
>   - Fix toCString for wider character sets
>
> Changes: https://git.openjdk.java.net/panama-foreign/pull/554/files
>   Webrev: https://webrevs.openjdk.java.net/?repo=panama-foreign&pr=554&range=00
>    Issue: https://bugs.openjdk.java.net/browse/JDK-8268230
>    Stats: 85 lines in 2 files changed: 75 ins; 6 del; 4 mod
>    Patch: https://git.openjdk.java.net/panama-foreign/pull/554.diff
>    Fetch: git fetch https://git.openjdk.java.net/panama-foreign pull/554/head:pull/554
>
> PR: https://git.openjdk.java.net/panama-foreign/pull/554
> .




More information about the panama-dev mailing list