Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.96k stars 555 forks source link

binmode with ':encoding(GBK)' makes 'die()/warn()' blocking #19544

Open d-xn opened 2 years ago

d-xn commented 2 years ago

Module:

Description

I deal with files with GBK/GB2312 encoding a lot. Sometimes it occurs so weird that my Perl scripts got stuck while the CPU usage running up to 100%.

Steps to Reproduce

use warnings;
use strict;

my $fname = "\x{00be}\x{02c9}\x{00b8}\x{00e6}\x{00d7}a\x{00bb}\x{00bb}\x{00cf}\x{00df}.pl";
open F, '>', $fname or die "$!";
print F while (<DATA>);
close F;
system("perl $fname");

__END__
use warnings;
use strict;
use Data::Dumper;

binmode STDERR, ":encoding(GBK)";
# binmode STDERR, ":crlf";

print Dumper(\$0);
my $fname = "\x{00be}\x{02c9}\x{00b8}\x{00e6}\x{00d7}a\x{00bb}\x{00bb}\x{00cf}\x{00df}.pl";
die "$fname";

Expected behavior

Perl configuration

Site configuration information for perl 5.32.1:

Configured by strawberry-perl at Sun Jan 24 15:01:28 2021.

Summary of my perl5 (revision 5 version 32 subversion 1) configuration:

  Platform:
    osname=MSWin32
    osvers=10.0.19042.746
    archname=MSWin32-x64-multi-thread
    uname='Win32 strawberry-perl 5.32.1.1 #1 Sun Jan 24 15:00:15 2021 x64'
    config_args='undef'
    hint=recommended
    useposix=true
    d_sigaction=undef
    useithreads=define
    usemultiplicity=define
    use64bitint=define
    use64bitall=undef
    uselongdouble=undef
    usemymalloc=n
    default_inc_excludes_dot=define
    bincompat5005=undef
  Compiler:
    cc='gcc'
    ccflags =' -DWIN32 -DWIN64 -D__USE_MINGW_ANSI_STDIO -DPERL_TEXTMODE_SCRIPTS -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO -fwrapv -fno-strict-aliasing -mms-bitfields'
    optimize='-s -O2'
    cppflags='-DWIN32'
    ccversion=''
    gccversion='8.3.0'
    gccosandvers=''
    intsize=4
    longsize=4
    ptrsize=8
    doublesize=8
    byteorder=12345678
    doublekind=3
    d_longlong=define
    longlongsize=8
    d_longdbl=define
    longdblsize=16
    longdblkind=3
    ivtype='long long'
    ivsize=8
    nvtype='double'
    nvsize=8
    Off_t='long long'
    lseeksize=8
    alignbytes=8
    prototype=define
  Linker and Libraries:
    ld='g++.exe'
    ldflags ='-s -L"D:\perl\perl\lib\CORE" -L"D:\perl\c\lib"'
    libpth=D:\perl\c\lib D:\perl\c\x86_64-w64-mingw32\lib D:\perl\c\lib\gcc\x86_64-w64-mingw32\8.3.0
    libs= -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32
    perllibs= -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32
    libc=
    so=dll
    useshrplib=true
    libperl=libperl532.a
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_win32.xs
    dlext=xs.dll
    d_dlsymun=undef
    ccdlflags=' '
    cccdlflags=' '
    lddlflags='-mdll -s -L"D:\perl\perl\lib\CORE" -L"D:\perl\c\lib"'

---
@INC for perl 5.32.1:
    D:/perl/perl/site/lib
    D:/perl/perl/vendor/lib
    D:/perl/perl/lib

---
Environment for perl 5.32.1:
    HOME=D:\Dasn
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=D:\cmd;D:\ImageMagick;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\ffmpeg\bin;D:\curl\bin;D:\EMACS\bin;D:\perl\perl\site\bin;D:\perl\perl\bin;D:\perl\c\bin
    PERL_BADLANG (unset)
    SHELL (unset)
Leont commented 2 years ago

It seems to not output the error instead of dying if I do use PerlIO::encoding; $PerlIO::encoding::fallback = Encode::FB_CROAK|Encode::STOP_AT_PARTIAL; before the binmode. I'm pretty sure this is a problem in Encode @dankogai