Mail Archives: djgpp/2016/04/09/11:02:24
X-Authentication-Warning: | delorie.com: mail set sender to djgpp-bounces using -f
|
X-Recipient: | djgpp AT delorie DOT com
|
Message-ID: | <57091A44.1090309@gmx.de>
|
Date: | Sat, 09 Apr 2016 17:05:40 +0200
|
From: | "Juan Manuel Guerrero (juan DOT guerrero AT gmx DOT de) [via djgpp AT delorie DOT com]" <djgpp AT delorie DOT com>
|
User-Agent: | Mozilla/5.0 (X11; U; Linux x86_64; de; rv:1.9.2.13) Gecko/20101206 SUSE/3.1.7 Thunderbird/3.1.7
|
MIME-Version: | 1.0
|
To: | djgpp AT delorie DOT com
|
Subject: | Re: Different dup2 behaviour between DOS/DJGPP and posix.
|
References: | <5706CAFA DOT 1060201 AT gmx DOT de> <5706E3D9 DOT 9070401 AT gmx DOT de>
|
In-Reply-To: | <5706E3D9.9070401@gmx.de>
|
X-Provags-ID: | V03:K0:G7QFyGdVAM7nPSImLZK7xryXGPtAsYAdszGqKrBeMd82oSsRAil
|
| 8af4fdSWBPo+qDF8RK2K7c8naZey82EavvyOnKt7lMbg5hhOC6WuAfW9uHkY1fb5VXcY+eN
|
| Wseb+z4UrLIA+9IYx+0bXX2pF7yazQpt0FidaECuJSz2e8je7Ih3s1pYtMfQZ0dfhPbAUVT
|
| m2BMVradKMiV8rZqqoSyg==
|
X-UI-Out-Filterresults: | notjunk:1;V01:K0:PlAO07BoPwc=:eTp11JtCH7NShg5j7C3s0l
|
| TKu9gCS/4ZplxESXsWM6ESYSPKOlL9qzTaRPvS86p4qyrlYj6MvYwvmnY6hdSIFUXxEEOyQST
|
| RY1pmFDrwWsQ/Glqob3G+NsVD0b7iTxt2NHuIwfpzfKAN3jh4+Af5olxgqOKSLPV4deay4aIx
|
| qJ0rRouMw48L+R4VPiI0gddgN7ILl5HvbeNdkKb/YSQLlMW/qtkfj+Y83uitjSmcIBP1Gw3j8
|
| zXFMxoNBdFWDxbs1s3251IZndxqq8o91Qo+++NKv1DlSuSw4qmaZayeBkTbO2auGUEAnnMJRj
|
| IpoQsGWT57GjLrJsn3h/WgdMnaDZALee1TAgdHjT44Q1JP5ZkX+DC8dgqCp/mJvzlCTAe8ucw
|
| 4yGhLGyX93sxAKsFvR/A4msyc1rXp0R+wTsnHOmbTe+vNDu1JaSV8coi7gTqpRd21jEPOGQPS
|
| b96C7KEcppjnqHh2XDoKPauX0nKWbMDq823vP4xdK9WqpKNAQkoEsqGM5FegP28UpZQuoqXt+
|
| aXqeFKLz3BkOIoKMkSCoAzWko2Ic6SyEuyrJk/M93Nk5O3Tq52BWQ+nVGUf44ZMWN7Xm2SWOM
|
| e/hNnN/Z1ZILyuRcNGsktyeLmMScJPw8+ebvqdlOQ0HWK0zKDUcvrr1pYTACxNI6DBWxHnygi
|
| Z9Zt8z60rBFwKkeHblV5jMm++6ch+4q4KUJ16FlRhJj1on43F1OyDhci/VR7T4W2wxg/mBd3c
|
| cq/PnhpXZi4JhGTXOsbnw2dpuiDBhFpyzHHftkg4Spkjndmq6KqiizJYbReffCts2k1hbBtLX
|
| svQdvea
|
Reply-To: | djgpp AT delorie DOT com
|
Am 08.04.2016 00:48, schrieb Juan Manuel Guerrero (juan DOT guerrero AT gmx DOT de) [via djgpp AT delorie DOT com]:
> Am 07.04.2016 23:02, schrieb Juan Manuel Guerrero (juan DOT guerrero AT gmx DOT de) [via djgpp AT delorie DOT com]:
> [snip]
>> The question is why DOS/DJGPP allows to close STDIN and reopen it with FD 0
>> meanwhile posix seems not to allow this. Is this behaviour expected?
>> I have inspected the code of dup2 and the implementation seems OK.
>>
>> The consequence of this check is that Autoconf assumes that DJGPP's dup2
>> implementaion does not work.
>
>
> OK I think I have got it. It is a bug in the DJGPP implementation of dup2.
> According to http://pubs.opengroup.org/onlinepubs/009695399/functions/dup.html
> The dup2() function shall fail if:
>
> [EBADF]
> The fildes argument is not a valid open file descriptor or the argument fildes2 is negative or greater than or equal to {OPEN_MAX}.
>
>
> The current implementation checks first if fd == newfd and if this is the case
> it returns inmediatly with newfd. Here some fix will be required because no one
> of the 3 conditions are checked. May be these error conditions were not in effect
> in those days when dup2 was implemented.
>
>
> Regards,
> Juan M. Guerrero
I have implemented a small fix that shall check for the validity of the
passed file descriptors to dup2. This change should increase the posix
compliance of the dup2 implementation a little bit. I have also added
a small check program that performs the same checks than the autoconf
check that called my attention to this issue.
As usual suggestions, objections and comments are welcome. If I do not
get a response in a rasonable period of time I will commit the changes
to the repository.
Regards,
Juan M. Guerrero
2016-04-09 Juan Manuel Guerrero <juan DOT guerrero AT gmx DOT de>
* djgpp/src/docs/kb/wc.txi: Entry for wc206.txi added.
* djgpp/src/docs/kb/wc205.txi: Entry for wc206.txi added.
* djgpp/src/docs/kb/wc206.txi: Changes in DJGPP 2.06.
* djgpp/src/libc/posix/unistd/dup2.c: Check validity of passed file descriptors.
* djgpp/src/libc/posix/unistd/dup2.txh: Check validity of passed file descriptors.
* djgpp/tests/libc/posix/unistd/dup2.c: Check for dup2.
* djgpp/tests/libc/posix/unistd/makefile: Entry for dup2 added.
diff -aprNU7 djgpp.orig/src/docs/kb/wc.txi djgpp/src/docs/kb/wc.txi
--- djgpp.orig/src/docs/kb/wc.txi 2015-05-03 10:14:56 -0509
+++ djgpp/src/docs/kb/wc.txi 2016-04-09 14:04:10 -0509
@@ -1,18 +1,20 @@
@node What Changed, Known Bugs, Introduction, Top
@chapter What Changed
@menu
-* Changes in 2.01:: DJGPP 2.01
-* Changes in 2.02:: DJGPP 2.02
-* Changes in 2.03:: DJGPP 2.03
-* Changes in 2.04:: DJGPP 2.04
-* Changes in 2.05:: DJGPP 2.05
+* Changes in 2.01:: DJGPP 2.01
+* Changes in 2.02:: DJGPP 2.02
+* Changes in 2.03:: DJGPP 2.03
+* Changes in 2.04:: DJGPP 2.04
+* Changes in 2.05:: DJGPP 2.05
+* Changes in 2.06:: DJGPP 2.06
@end menu
@include wc201.txi
@include wc202.txi
@include wc203.txi
@include wc204.txi
@include wc205.txi
+@include wc206.txi
diff -aprNU7 djgpp.orig/src/docs/kb/wc205.txi djgpp/src/docs/kb/wc205.txi
--- djgpp.orig/src/docs/kb/wc205.txi 2015-08-23 14:27:40 -0509
+++ djgpp/src/docs/kb/wc205.txi 2016-04-09 14:04:10 -0509
@@ -1,8 +1,8 @@
-@node Changes in 2.05, , Changes in 2.04, What Changed
+@node Changes in 2.05, Changes in 2.06, Changes in 2.04, What Changed
@section Changes in 2.05
Here is a list of changes from DJGPP V2.04 to DJGPP V2.05
DJGPP v2.04 was actually never released and stayed in beta stage much too
long.
diff -aprNU7 djgpp.orig/src/docs/kb/wc206.txi djgpp/src/docs/kb/wc206.txi
--- djgpp.orig/src/docs/kb/wc206.txi 1969-12-31 18:50:31 -0509
+++ djgpp/src/docs/kb/wc206.txi 2016-04-09 14:08:14 -0509
@@ -0,0 +1,12 @@
+@node Changes in 2.06, , Changes in 2.05, What Changed
+@section Changes in 2.06
+
+Here is a list of changes from DJGPP V2.05 to DJGPP V2.06
+
+@findex dup2 AT r{, and POSIX.1-2001 compliance}
+The validity of the file descriptors passed to @code{dup2} is now checked.
+If @var{existing_handle} is not a valid open file descriptor or
+if @var{new_handle} is out of the allowed range for file descriptors
+the call of @code{dup2} will fail and @code{errno} will be set to @code{EBADF}.
+With this adjustment, the @acronym{Posix} compliance of the @code{dup2} implementation
+is increasd.
diff -aprNU7 djgpp.orig/src/libc/posix/unistd/dup2.c djgpp/src/libc/posix/unistd/dup2.c
--- djgpp.orig/src/libc/posix/unistd/dup2.c 2001-08-19 16:50:26 -0509
+++ djgpp/src/libc/posix/unistd/dup2.c 2016-04-09 15:18:28 -0509
@@ -1,7 +1,8 @@
+/* Copyright (C) 2016 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
#include <libc/stubs.h>
#include <unistd.h>
#include <fcntl.h>
#include <dpmi.h>
@@ -11,16 +12,24 @@
#include <libc/dosio.h>
#include <libc/fd_props.h>
int
dup2(int fd, int newfd)
{
__dpmi_regs r;
+
+ if (newfd < 0 || newfd >= getdtablesize() || _get_dev_info(fd) == -1)
+ {
+ errno = EBADF;
+ return -1;
+ }
+
if (fd == newfd)
return newfd;
+
/* Reset the text/binary modes, the fsext, and the properties, since
DOS function 46h closes newfd. */
__file_handle_set(newfd, __file_handle_modes[fd] ^ (O_BINARY|O_TEXT));
__FSEXT_set_function(newfd, 0);
r.h.ah = 0x46;
r.x.bx = fd;
r.x.cx = newfd;
diff -aprNU7 djgpp.orig/src/libc/posix/unistd/dup2.txh djgpp/src/libc/posix/unistd/dup2.txh
--- djgpp.orig/src/libc/posix/unistd/dup2.txh 2003-01-29 12:51:40 -0509
+++ djgpp/src/libc/posix/unistd/dup2.txh 2016-04-09 14:04:10 -0509
@@ -8,23 +8,25 @@
int dup2(int existing_handle, int new_handle);
@end example
@subheading Description
This call causes @var{new_handle} to refer to the same file and file
pointer as @var{existing_handle}. If @var{new_handle} is an open file,
-it is closed.
+it is closed. If @var{existing_handle} is not a valid open file descriptor
+or if @var{new_handle} out of the allowed range for file descriptors the
+call will fail and @code{errno} will be set to @code{EBADF}.
@subheading Return Value
The new handle, or -1 on error.
@subheading Portability
-@portability !ansi, posix
+@portability !ansi, posix-1003.1-2001
@subheading Example
@example
/* copy new file to stdin stream */
close(0);
dup2(new_stdin, 0);
diff -aprNU7 djgpp.orig/tests/libc/posix/unistd/dup2.c djgpp/tests/libc/posix/unistd/dup2.c
--- djgpp.orig/tests/libc/posix/unistd/dup2.c 1969-12-31 18:50:31 -0509
+++ djgpp/tests/libc/posix/unistd/dup2.c 2016-04-09 16:10:50 -0509
@@ -0,0 +1,56 @@
+#include <stdio.h>
+#include <errno.h>
+#include <limits.h>
+#include <sys/resource.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+
+
+int main (void)
+{
+ int result = 0;
+ int bad_fd = INT_MAX;
+ struct rlimit rlim;
+
+
+ if (getrlimit(RLIMIT_NOFILE, &rlim) == 0
+ && 0 <= rlim.rlim_cur && rlim.rlim_cur <= INT_MAX)
+ bad_fd = rlim.rlim_cur;
+
+ if (dup2(1, 1) != 1)
+ result |= 1;
+
+ close (0);
+ if (dup2(0, 0) != -1)
+ result |= 2;
+
+ if (dup2(2, bad_fd) == -1 && errno != EBADF)
+ result |= 4;
+
+ if (dup2 (2, -1) != -1 && errno != EBADF)
+ result |= 8;
+
+ if (dup2(2, 254) != 254)
+ result |= 16;
+ if (dup2(2, 255) != -1)
+ result |= 32;
+
+ {
+ int fd = open(".", O_RDONLY);
+ if (fd == -1)
+ result |= 64;
+ else if (dup2(fd, fd + 1) == -1)
+ result |= 128;
+
+ close(fd);
+ }
+
+ if (result)
+ printf("dup2 check failed with result = %d\n", result);
+ else
+ printf("dup2 check passed.\n");
+
+ return result;
+}
+
diff -aprNU7 djgpp.orig/tests/libc/posix/unistd/makefile djgpp/tests/libc/posix/unistd/makefile
--- djgpp.orig/tests/libc/posix/unistd/makefile 2003-03-26 19:50:54 -0509
+++ djgpp/tests/libc/posix/unistd/makefile 2016-04-09 14:04:12 -0509
@@ -1,12 +1,13 @@
TOP=../..
SRC += access.c
SRC += append.c
SRC += chdr.c
+SRC += dup2.c
SRC += gcwd.c
SRC += getpid.c
SRC += sleep.c
SRC += tread.c
SRC += write.c
SRC += t-fchdir.c
SRC += t-isatty.c
- Raw text -