This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix POSIX::mblen mbstate_t initialization on threaded perls with glibc
authorNiko Tyni <ntyni@debian.org>
Sun, 10 Mar 2019 17:40:42 +0000 (19:40 +0200)
committerKarl Williamson <khw@cpan.org>
Mon, 25 Mar 2019 03:23:51 +0000 (21:23 -0600)
commit25d7b7aa379d33ce2e8fe3e2bef4206b35739bc5
tree59382b98b5039006c3fe5c2aa48b0e76b1a26396
parente7a474c07a323436b1df5dd0738f84316f86ca48
Fix POSIX::mblen mbstate_t initialization on threaded perls with glibc

As reported in https://bugs.launchpad.net/bugs/1818953 POSIX::mblen()
is broken on threaded perls with glibc.

  % perl -MPOSIX=mblen -e 'mblen("a", 1)'
  perl: mbrtowc.c:105: __mbrtowc: Assertion `__mbsinit (data.__statep)' failed.
  zsh: abort (core dumped)  perl -MPOSIX=mblen -e 'mblen("a", 1)'

This broke in v5.27.8-134-g6c9ff7e96e which made the function
use mbrlen(3) under the hood on threaded perls.

The problem is initialization of the shift state with

  mbrlen(NULL, 0, &ps));

The glibc documentation for mbrlen(3) at

  https://www.gnu.org/software/libc/manual/html_node/Converting-a-Character.html#Converting-a-Character

does not mention initialization by passing in a null pointer for the
string, only a pointer to a NUL wide character.

   If the next multibyte character corresponds to the NUL wide character,
   the return value is 0. If the next n bytes form a valid multibyte
   character, the number of bytes belonging to this multibyte character
   byte sequence is returned.

Use memset(3) instead for mbstate_t initialization, as suggested in

  https://www.gnu.org/software/libc/manual/html_node/Keeping-the-state.html

with the hope that this is more portable.

While at it, add a few basic test cases. These are in a new file because
they need fresh_perl_is() from test.pl while the existing ones use
Test::More (and conversion of at least posix.t looks way too involved.)

Bug-Ubuntu: https://bugs.launchpad.net/bugs/1818953
MANIFEST
ext/POSIX/POSIX.xs
ext/POSIX/lib/POSIX.pm
ext/POSIX/t/mb.t [new file with mode: 0644]