Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lstat returns incorrect zero size for symlink on Windows 11 #20476

Closed
hakonhagland opened this issue Nov 2, 2022 · 1 comment · Fixed by #20500
Closed

lstat returns incorrect zero size for symlink on Windows 11 #20476

hakonhagland opened this issue Nov 2, 2022 · 1 comment · Fixed by #20500

Comments

@hakonhagland
Copy link
Contributor

I have built a debug version of perl 5.37.6 on Windows 11 using MinGW-w64 and gcc 11.3.0 from https://winlibs.com/ (using the MSVCRT runtime library). Note that this includes #20271, which fixes the readlink behavior on Windows, see #20460 for more information.

When trying to install Path::Tiny using this perl I get a test failure due to lstat returning a zero size. Here is a minimal example:

use feature qw(say);
use strict;
use warnings;

my $fn = 'foo.txt';
open (my $fh, '>', $fn) or die "Could not open file '$fn': $!";
say $fh "Hello";
close $fh;
my $link = 'bar.txt';
if (-e $link) {
    unlink $link or die "Could not unlink '$link': $!";
}
symlink $fn, $link or die "Could not create symlink: $!";
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
    $atime,$mtime,$ctime,$blksize,$blocks) = lstat $link;
say "symlink size: $size";

The output is:

symlink size: 0

expected output:

symlink size: 7

The expected size is 7 since the size of a symlink should be equal to the length of its path name according to the lstat(2) man page.

> perl -V
Summary of my perl5 (revision 5 version 37 subversion 6) configuration:
  Commit id: a589f4b93cf3864d7f665d0974e2d36f028b00b6
  Platform:
    osname=MSWin32
    osvers=10.0.22000.1098
    archname=MSWin32-x64-multi-thread
    uname=''
    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
  Compiler:
    cc='gcc'
    ccflags =' -DWIN32 -DWIN64 -DDEBUGGING -DPERL_TEXTMODE_SCRIPTS -DMULTIPLICITY -DPERL_IMPLICIT_SYS -DUSE_PERLIO -D__USE_MINGW_ANSI_STDIO -fwrapv -fno-strict-aliasing -mms-bitfields'
    optimize='-g -O0'
    cppflags='-DWIN32'
    ccversion=''
    gccversion='11.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++'
    ldflags ='-g -L"c:\perl-debug\lib\CORE" -L"C:\Winlibs64-Gcc11.3-msvcrt\mingw64\lib" -L"C:\Winlibs64-Gcc11.3-msvcrt\mingw64\x86_64-w64-mingw32\lib" -L"C:\Winlibs64-Gcc11.3-msvcrt\mingw64\lib\gcc\x86_64-w64-mingw32\11.3.0"'
    libpth=C:\Winlibs64-Gcc11.3-msvcrt\mingw64\lib C:\Winlibs64-Gcc11.3-msvcrt\mingw64\x86_64-w64-mingw32\lib C:\Winlibs64-Gcc11.3-msvcrt\mingw64\lib\gcc\x86_64-w64-mingw32\11.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=libperl537.a
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_win32.xs
    dlext=dll
    d_dlsymun=undef
    ccdlflags=' '
    cccdlflags=' '
    lddlflags='-shared -g -L"c:\perl-debug\lib\CORE" -L"C:\Winlibs64-Gcc11.3-msvcrt\mingw64\lib" -L"C:\Winlibs64-Gcc11.3-msvcrt\mingw64\x86_64-w64-mingw32\lib" -L"C:\Winlibs64-Gcc11.3-msvcrt\mingw64\lib\gcc\x86_64-w64-mingw32\11.3.0"'


Characteristics of this binary (from libperl):
  Compile-time options:
    DEBUGGING
    HAS_TIMES
    HAVE_INTERP_INTERN
    MULTIPLICITY
    PERLIO_LAYERS
    PERL_COPY_ON_WRITE
    PERL_DONT_CREATE_GVSV
    PERL_HASH_FUNC_SIPHASH13
    PERL_HASH_USE_SBOX32
    PERL_IMPLICIT_SYS
    PERL_MALLOC_WRAP
    PERL_OP_PARENT
    PERL_PRESERVE_IVUV
    PERL_TRACK_MEMPOOL
    PERL_USE_SAFE_PUTENV
    USE_64_BIT_INT
    USE_ITHREADS
    USE_LARGE_FILES
    USE_LOCALE
    USE_LOCALE_COLLATE
    USE_LOCALE_CTYPE
    USE_LOCALE_NUMERIC
    USE_LOCALE_TIME
    USE_PERLIO
    USE_PERL_ATOF
  Built under MSWin32
  Compiled at Nov  2 2022 00:00:55
  @INC:
    C:/perl-debug/site/lib
    C:/perl-debug/lib
@hakonhagland
Copy link
Contributor Author

The output is: symlink size: 0

I tried to investigate the source to find more information. I find win32_stat_low() function in win32.c. It calls GetFileInformationByHandle() to get stat information, see line 1572. I think the problem is that this function always returns zero for bhi.nFileSizeHigh and bhi.nFileSizeLow see lines 1583 and 1585. Instead, if we want lstat to return the size of path name of the target inside the symlink as on linux, see lstat(2) man page, we could use a similar approach to what win32_readlink() does? See lines 1940, 1947, and 1964 in win32.c.

tonycoz added a commit to tonycoz/perl5 that referenced this issue Nov 10, 2022
This is reflected in the result of lstat() in perl.

This matches POSIX behaviour.

Fixed Perl#20476
tonycoz added a commit that referenced this issue Jan 9, 2023
This is reflected in the result of lstat() in perl.

This matches POSIX behaviour.

Fixed #20476
pjacklam pushed a commit to pjacklam/perl5 that referenced this issue May 20, 2023
This is reflected in the result of lstat() in perl.

This matches POSIX behaviour.

Fixed Perl#20476
pjacklam pushed a commit to pjacklam/perl5 that referenced this issue May 20, 2023
This is reflected in the result of lstat() in perl.

This matches POSIX behaviour.

Fixed Perl#20476
khwilliamson pushed a commit to khwilliamson/perl5 that referenced this issue Jul 10, 2023
This is reflected in the result of lstat() in perl.

This matches POSIX behaviour.

Fixed Perl#20476
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants