The close() function also crashes when the fd argument is invalid. Here's the workaround.
2011-09-23 Bruno Haible <[email protected]> close: Support for MSVC 9. * lib/close.c: Include <errno.h>, msvc-inval.h. (close_nothrow): New function. (rpl_close): Use it. * m4/close.m4 (gl_FUNC_CLOSE): Replace close if the platform has an invalid parameter handler. * modules/close (Depends-on): Add msvc-inval. * modules/dup2-tests (Depends-on): Add close. * modules/dup3-tests (Depends-on): Likewise. * modules/fcntl-tests (Depends-on): Likewise. * modules/spawn-pipe-tests (Depends-on): Likewise. * modules/unistd-safer-tests (Depends-on): Likewise. * doc/posix-functions/close.texi: Mention the problem on MSVC. *** doc/posix-functions/close.texi.orig Sat Sep 24 02:43:05 2011 --- doc/posix-functions/close.texi Sat Sep 24 02:24:04 2011 *************** *** 9,14 **** --- 9,17 ---- Portability problems fixed by Gnulib: @itemize @item + This function crashes when invoked with invalid arguments on some platforms: + MSVC 9. + @item On Windows platforms (excluding Cygwin), @code{socket} and @code{accept} do not return file descriptors that can be closed by @code{close}. Instead, @code{closesocket} must be used. *** lib/close.c.orig Sat Sep 24 02:43:06 2011 --- lib/close.c Fri Sep 23 22:03:29 2011 *************** *** 19,36 **** /* Specification. */ #include <unistd.h> #include "fd-hook.h" /* Override close() to call into other gnulib modules. */ int rpl_close (int fd) - #undef close { #if WINDOWS_SOCKETS ! int retval = execute_all_close_hooks (close, fd); #else ! int retval = close (fd); #endif #if REPLACE_FCHDIR --- 19,63 ---- /* Specification. */ #include <unistd.h> + #include <errno.h> + #include "fd-hook.h" + #include "msvc-inval.h" + + #undef close + + #if HAVE_MSVC_INVALID_PARAMETER_HANDLER + static int + close_nothrow (int fd) + { + int result; + + TRY_MSVC_INVAL + { + result = close (fd); + } + CATCH_MSVC_INVAL + { + result = -1; + errno = EBADF; + } + DONE_MSVC_INVAL; + + return result; + } + #else + # define close_nothrow close + #endif /* Override close() to call into other gnulib modules. */ int rpl_close (int fd) { #if WINDOWS_SOCKETS ! int retval = execute_all_close_hooks (close_nothrow, fd); #else ! int retval = close_nothrow (fd); #endif #if REPLACE_FCHDIR *** m4/close.m4.orig Sat Sep 24 02:43:06 2011 --- m4/close.m4 Sat Sep 24 02:36:29 2011 *************** *** 1,4 **** ! # close.m4 serial 7 dnl Copyright (C) 2008-2011 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, --- 1,4 ---- ! # close.m4 serial 8 dnl Copyright (C) 2008-2011 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, *************** *** 7,12 **** --- 7,16 ---- AC_DEFUN([gl_FUNC_CLOSE], [ AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) + AC_REQUIRE([gl_MSVC_INVAL]) + if test $HAVE_MSVC_INVALID_PARAMETER_HANDLER = 1; then + REPLACE_CLOSE=1 + fi m4_ifdef([gl_PREREQ_SYS_H_WINSOCK2], [ gl_PREREQ_SYS_H_WINSOCK2 if test $UNISTD_H_HAVE_WINSOCK2_H = 1; then *** modules/close.orig Sat Sep 24 02:43:06 2011 --- modules/close Sat Sep 24 02:34:05 2011 *************** *** 8,13 **** --- 8,14 ---- Depends-on: unistd fd-hook [test $REPLACE_CLOSE = 1] + msvc-inval [test $REPLACE_CLOSE = 1] configure.ac: gl_FUNC_CLOSE *** modules/dup2-tests.orig Sat Sep 24 02:43:06 2011 --- modules/dup2-tests Fri Sep 23 23:15:56 2011 *************** *** 5,10 **** --- 5,11 ---- Depends-on: binary-io + close msvc-nothrow open *** modules/dup3-tests.orig Sat Sep 24 02:43:06 2011 --- modules/dup3-tests Sat Sep 24 02:26:39 2011 *************** *** 6,11 **** --- 6,12 ---- Depends-on: msvc-nothrow open + close configure.ac: *** modules/fcntl-tests.orig Sat Sep 24 02:43:06 2011 --- modules/fcntl-tests Fri Sep 23 23:14:19 2011 *************** *** 5,10 **** --- 5,11 ---- Depends-on: binary-io + close getdtablesize msvc-nothrow stdbool *** modules/spawn-pipe-tests.orig Sat Sep 24 02:43:06 2011 --- modules/spawn-pipe-tests Sat Sep 24 02:30:59 2011 *************** *** 5,10 **** --- 5,11 ---- tests/macros.h Depends-on: + close configure.ac: *** modules/unistd-safer-tests.orig Sat Sep 24 02:43:06 2011 --- modules/unistd-safer-tests Sat Sep 24 02:27:11 2011 *************** *** 5,10 **** --- 5,11 ---- Depends-on: binary-io cloexec + close dup fd-safer-flag msvc-nothrow -- In memoriam Ghazala Khan <http://en.wikipedia.org/wiki/Ghazala_Khan>
