dankogai / p5-encode

Encode - character encodings (for Perl 5.8 or better)
https://metacpan.org/release/Encode
37 stars 51 forks source link

decode() omits trailing NUL #159

Closed FGasper closed 3 years ago

FGasper commented 3 years ago
> perl -MDevel::Peek -MEncode -e'my $foo = Encode::decode("UTF-16LE", "/\0v\0a\0r\0/\0c\0p\0a\0n\0e\0l\0/\0u\0s\0e\0r\0s\0/\0s\0u\0p\0e\0r\0m\0a\0n\0"); Dump $foo'
SV = PV(0x7fb5a5814170) at 0x7fb5a4816378
  REFCNT = 1
  FLAGS = (POK,IsCOW,pPOK,UTF8)
  PV = 0x7fb5a4501a20 "/var/cpanel/users/superman" [UTF8 "/var/cpanel/users/superman"]
  CUR = 26
  LEN = 48
  COW_REFCNT = 0

But note:

> perl -MDevel::Peek -e'my $foo = "/var/cpanel/users/superman"; utf8::upgrade $foo; Dump $foo'
SV = PV(0x7fb9dd804c70) at 0x7fb9dd8162d0
  REFCNT = 1
  FLAGS = (POK,pPOK,UTF8)
  PV = 0x7fb9dd409060 "/var/cpanel/users/superman"\0 [UTF8 "/var/cpanel/users/superman"]
  CUR = 26
  LEN = 28

This causes weird breakage when giving decode()d strings to stat() et al because Perl just gives the raw PV, which means the system call receives a different string than Perl shows to the application.