Skip to content

SilkMarshal.PtrToStringArray ignores the encoding parameter #2375

@Exanite

Description

@Exanite

Summary

As I was debugging a different problem, I noticed that SilkMarshal.PtrToStringArray ignores the encoding parameter.
This leads to incorrect behavior when not using the default encoding.

This incorrect behavior can be seen when converting to a pointer and back.

public static unsafe void CopyPtrToStringArray
(
nint ptr,
string[] arr,
NativeStringEncoding encoding = NativeStringEncoding.Ansi
)
{
for (var i = 0; i < arr.Length; i++)
{
arr[i] = PtrToString(((nint*) ptr)![i]);
}
}

Steps to reproduce

Run https://dotnetfiddle.net/zQNrbf with Silk.NET.Core v2.22.0 added.

This will be the output:
Note how some strings are truncated.

----- String array -----
BStr
H
F
B
1

LPStr
Hello world
Foo
Bar
123

LPTStr
Hello world
Foo
Bar
123

LPUTF8Str
Hello world
Foo
Bar
123

LPWStr
H
F
B
1

Comments

This issue seems to be easy to fix so I can submit a pull request if wanted.

The single string variants (StringToPtr and PtrToString) work fine. The issue is only found in PtrToStringArray.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions