cygwin1.dll (v1.1.2): dup2() with invalid newfd lead to exception: STATUS_ACCESS_VIOLATION
Alexander Trush
trush@kbotd.ru
Mon Jun 26 06:35:00 GMT 2000
Hi!
I have loaded the last version of the environment CygWin and attempted
to compile SQUID-2.3.STABLE3. In the configurator SQUID there is a piece
of the code: " checking Maximum number of filedescriptors we can open ",
this code utillize the function dup2(), which one with invalid value of
argument reduces to in crash of the program instead of returning an error
code.
Piece of this code:
#include "confdefs.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>ÃÂ ÃÂ /* needed on FreeBSD */
#include <sys/param.h>
#include <sys/resource.h>
main() {
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ FILE *fp;
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ int i,j;
#if __CYGWIN32__
ÃÂ ÃÂ ÃÂ /* getrlimit and sysconf returns bogous values
on cygwin32.
ÃÂ ÃÂ ÃÂ ÃÂ * Number of fds is virtually unlimited
in cygwin (sys/param.h)
ÃÂ ÃÂ ÃÂ ÃÂ */
ÃÂ ÃÂ ÃÂ i = NOFILE;
#elif HAVE_SETRLIMIT
ÃÂ ÃÂ ÃÂ struct rlimit rl;
#if defined(RLIMIT_NOFILE)
ÃÂ ÃÂ ÃÂ if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
{
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ perror("getrlimit: RLIMIT_NOFILE");
ÃÂ ÃÂ ÃÂ } else {
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ rl.rlim_cur = rl.rlim_max;ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ
/* set it to the max */
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ if (setrlimit(RLIMIT_NOFILE,
&rl) < 0) {
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ
perror("setrlimit: RLIMIT_NOFILE");
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ }
ÃÂ ÃÂ ÃÂ }
#elif defined(RLIMIT_OFILE)
ÃÂ ÃÂ ÃÂ if (getrlimit(RLIMIT_OFILE, &rl) < 0)
{
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ perror("getrlimit: RLIMIT_OFILE");
ÃÂ ÃÂ ÃÂ } else {
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ rl.rlim_cur = rl.rlim_max;ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ
/* set it to the max */
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ if (setrlimit(RLIMIT_OFILE,
&rl) < 0) {
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ
perror("setrlimit: RLIMIT_OFILE");
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ }
ÃÂ ÃÂ ÃÂ }
#endif /* RLIMIT_NOFILE */
#endif /* HAVE_SETRLIMIT */
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ /* by starting at 2^14,
we will never get higher
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ than 2^15 for SQUID_MAXFD
*/
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ i = j = 1<<14;
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ while (j) {
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ
j >>= 1;
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ
if (dup2(0, i) < 0) {
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ
i -= j;
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ
} else {
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ
close(i);
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ
i += j;
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ
}
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ }
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ i++;
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ fp = fopen("conftestval",
"w");
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ fprintf (fp, "%d\n",
i);
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ exit(0);
}
ÃÂ
ÃÂ
ÃÂ
The elementary test routine resulting in to the same result (under Borland
5.02 all is correct - " Ret val =-1, errno=0 "):
ÃÂ
test.c:
-------
#include <stdio.h>
#include <io.h>
#include <errno.h>
void main(void)
{
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ printf("Ret val=%d,
errno=%d", dup2(0, 1<<14), errno);
}
ÃÂ
ÃÂ
bash-2.04$ cd /usr/src/squid-2.3.STABLE3
bash-2.04$ gcc test.c -o test.exe
test.c: In function `main':
test.c:6: warning: return type of `main' is not `int'
bash-2.04$ ./test.exe
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ 0 [main] test 1008 handle_exceptions:
Exception: STATUS_ACCESS_VIOLATION
ÃÂ ÃÂ 1392 [main] test 1008 stackdump: Dumping stack trace
to test.exe.stackdump
bash-2.04$
ÃÂ
test.exe.stackdump:
-------------------
Exception: STATUS_ACCESS_VIOLATION at eip=6101F19A
eax=0A040E08 ebx=FFFFFFFF ecx=7FFDE000 edx=61084BE8 esi=00004000
edi=0A0416B8
ebp=0246FE5C esp=0246FE40 program=c:\CygWIN\usr\src\squid-2.3.STABLE3\test.exe
cs=001B ds=0023 es=0023 fs=003B gs=0000 ss=0023
Stack trace:
FrameÃÂ ÃÂ ÃÂ ÃÂ FunctionÃÂ Args
0246FE5CÃÂ 6102F23BÃÂ (61084BE8, 00000000, 00004000, 0247273B)
0246FE7CÃÂ 6102F23BÃÂ (00000000, 00004000, 0246FEC8, 0246FEA4)
0246FEA4ÃÂ 0040107FÃÂ (00000001, 0A041688, 0A040008, 00000000)
0246FF00ÃÂ 61002385ÃÂ (7FFDF000, 00000000, 0247DF10, 77F9D0E8)
0246FF60ÃÂ 610027D5ÃÂ (00403010, C0059000, 0246FF90, 004011BA)
0246FF90ÃÂ 004011C7ÃÂ (00401058, 815C04C0, 00000246, 801174E1)
0246FFC0ÃÂ 0040103BÃÂ (0247DF10, 00000000, 7FFDF000, 7FFDF000)
0246FFF0ÃÂ 77F1B9EAÃÂ (00401000, 00000000, 000000B0, 00000100)
End of stack trace
ÃÂ
More information about the Cygwin
mailing list