develooper Front page | perl.perl5.porters | Postings from September 2011

[perl #98912] wrong return value for dup2 on Win32

Thread Next
Nicholas Clark
September 12, 2011 02:06
[perl #98912] wrong return value for dup2 on Win32
Message ID:
# New Ticket Created by  Nicholas Clark 
# Please include the string:  [perl #98912]
# in the subject line of all future correspondence about this issue. 
# <URL: >

On Win32, I infer that dup2() is returning -1 or 0 [like dup() and most
other system calls]. It *should* be returning the file descriptor (ie the
second argument) or -1, as POSIX has documented for the past 2 decades.

This code:

undef $buffer;
read($fd2, $buffer, 4) if $fd2 > 2;
is($buffer, "# Ex", 'read');
# The descriptor $testfd was using is now free, and is lower than that which
# $fd1 was using. Hence if dup2() behaves as dup(), we'll know :-)
$testfd = dup2($fd2, $fd1);
is($testfd, $fd1, 'dup2');
undef $buffer;
read($testfd, $buffer, 4) if $testfd > 2;
is($buffer, 'pect', 'read');
is(lseek($testfd, 0, 0), 0, 'lseek back');

generated this diagnostic:

#   Failed test 'dup2'
#   at t/posix.t line 349.
#          got: '0 but true'
#     expected: '4'

#   Failed test 'read'
#   at t/posix.t line 352.
#          got: undef
#     expected: 'pect'

#   Failed test 'lseek back'
#   at t/posix.t line 353.
#          got: '-1'
#     expected: '0'


The test passes elsewhere.

I can't work out where the actual bugging implementation of dup2() comes from.

I'd marked the code as TODO to get it to pass, but I realise it probably
should be TODO & SKIP, as a read from fd 0 is likely to hang if STDIN is a

Nicholas Clark

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About