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

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

Thread Next
From:
Nicholas Clark
Date:
September 12, 2011 02:06
Subject:
[perl #98912] wrong return value for dup2 on Win32
Message ID:
rt-3.6.HEAD-31297-1315818358-1066.98912-75-0@perl.org
# New Ticket Created by  Nicholas Clark 
# Please include the string:  [perl #98912]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org:443/rt3/Ticket/Display.html?id=98912 >


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'

[from
http://m-l.org/~perl/smoke/perl/win32/smoke-me/Nicholas%20Clark/logb5413daf791f474dd7dc1274bc210e17cf3c59e0.log.gz
]

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
terminal.

Nicholas Clark


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About