Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl6 NativeCall with Str is encoded('utf16') got randomly corrupted result

I am mapping the GetFullPathName windows API in a perl6 script using NativeCall, for so I wrote the following:

#!perl6
use NativeCall;
constant \WIN32_MAX_PATH = 260;    

#I may use directly $path.IO.absolute()
sub Win32-GetFullPathName(
        Str $lpFileName is encoded('utf16'),
        int32 $nBufferLength, 
        #Str $lpBuffer is encoded('utf16') is rw,
        Blob $lpBuffer is rw,
        Str $lpFilenameIndex is rw)
    returns int32
    is native('kernel32.dll') 
    is symbol('GetFullPathNameW') 
    { * }


my $path = '.';
my $fp-size = Win32-GetFullPathName(
        $path, # $path ~ "\0", # <!-- this hack make it working fine
        WIN32_MAX_PATH, 
        #my Str $fpath = ' ' x WIN32_MAX_PATH;
        my $buffer = buf16.allocate(WIN32_MAX_PATH), 
        Str );
put "[$fp-size] => ", $buffer.decode('utf16').substr(0, $fp-size);

The code is working randomly, until I append a "\0" after $path.

[EDIT] The results when called multiple times:

[12] => D:\dev\pa.t

[12] => D:\dev\pa.

[12] => D:\dev\pa.槟

[9] => D:\dev\pa

[9] => D:\dev\pa

Is there another proper way to do it?

like image 210
xlat Avatar asked Dec 09 '18 10:12

xlat


1 Answers

I suspect a MoarVM bug in src/strings/utf16.c, in particular line 403:

result = MVM_realloc(result, *output_size);

which should probably read

result = MVM_realloc(result, *output_size + 2);

If you could verify that this fixes your problem, feel free to file a bug report or even create a pull request.

like image 92
Christoph Avatar answered Nov 14 '22 06:11

Christoph