From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS22989 209.51.188.0/24 X-Spam-Status: No, score=-3.9 required=3.0 tests=AWL,BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 8EABB1F461 for ; Tue, 2 Jul 2019 19:51:02 +0000 (UTC) Received: from localhost ([::1]:56758 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hiOnq-0003JN-Uh for normalperson@yhbt.net; Tue, 02 Jul 2019 15:50:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37604) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hiNog-0005rW-37 for bug-gnulib@gnu.org; Tue, 02 Jul 2019 14:47:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hiNoa-0003e9-Sd for bug-gnulib@gnu.org; Tue, 02 Jul 2019 14:47:42 -0400 Received: from mo6-p00-ob.smtp.rzone.de ([2a01:238:20a:202:5300::5]:19172) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hiNoY-0003BG-V6 for bug-gnulib@gnu.org; Tue, 02 Jul 2019 14:47:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1562093210; s=strato-dkim-0002; d=clisp.org; h=Message-ID:Date:Subject:To:From:X-RZG-CLASS-ID:X-RZG-AUTH:From: Subject:Sender; bh=1BBYPvRL8g05e9xslviSacjar3Qp5ffAcRmjZnSjnS0=; b=c/hiPeu7ecCKfPtUXWcCTssvfUhHtD/GhKXleyr7mREUjOiPtzTyO+C9USBY32+c3z 2Hu4GdTQayBUFITSn3GgeUUM8+ly0uWD462ToL4dJXehyDYa2UQfD5I5pwtUTJsrdu/U t5H3xuCpCJOqbU6JOAdf5rjvlxRMGvhqNOx2RaaDSUyd9f/Yr+e5fT1znuCljD6ui11O uHyF9+IbiGFQrXTwk4y/BJbISRrGPx7U9VB/aup3gcqtTYGN5/K6rS7R1De6OGgT4pRL xD4mt0fRA1xqnqHq2js+gW9p9Lb94/8ggKMDkXjgUQvuF47MirR6CU8+h8Y29+nwF1/e eZUw== X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH+AHjwLuWOGaf0zJZW" X-RZG-CLASS-ID: mo00 Received: from bruno.haible.de by smtp.strato.de (RZmta 44.24 DYNA|AUTH) with ESMTPSA id v018bcv62Ikn7gT (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (curve secp521r1 with 521 ECDH bits, eq. 15360 bits RSA)) (Client did not present a certificate); Tue, 2 Jul 2019 20:46:49 +0200 (CEST) From: Bruno Haible To: bug-gnulib@gnu.org Subject: getcwd: fix crash when invoked with size = 0 on MSVC Date: Tue, 02 Jul 2019 20:46:49 +0200 Message-ID: <3240480.A9dRRlpuj9@omega> User-Agent: KMail/5.1.3 (Linux/4.4.0-151-generic; KDE/5.18.0; x86_64; ; ) MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a01:238:20a:202:5300::5 X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" On MSVC, I'm seeing this test failure: FAIL: test-getcwd-lgpl ====================== FAIL test-getcwd-lgpl.exe (exit status: 127) It's the getcwd-lgpl test, but it exercises the function rpl_getcwd defined in lib/getcwd.c, not the one in lib/getcwd-lgpl.c. The crash happens in the getcwd (buf, 0) call. The MSVC runtime signals an invalid argument through an exception which, by default, crashes the program. This patch fixes it (in the same manner as in the functions close(), dup2(), etc.). 2019-07-02 Bruno Haible getcwd: Fix crash when invoked with size = 0 on MSVC. * lib/getcwd.c: Include msvc-inval.h. (getcwd_nothrow): New function/macro. (getcwd_system): New macro. (__getcwd): Use it instead of getcwd. * modules/getcwd (Depends-on): Add msvc-inval. * doc/posix-functions/getcwd.texi: Mention the MSVC issue. diff --git a/lib/getcwd.c b/lib/getcwd.c index 41eedb7..8f15f56 100644 --- a/lib/getcwd.c +++ b/lib/getcwd.c @@ -79,6 +79,10 @@ # define MATCHING_INO(dp, ino) true #endif +#if HAVE_MSVC_INVALID_PARAMETER_HANDLER +# include "msvc-inval.h" +#endif + #if !_LIBC # define __getcwd rpl_getcwd # define __lstat lstat @@ -100,6 +104,34 @@ # undef closedir #endif +#ifdef _MSC_VER +# if HAVE_MSVC_INVALID_PARAMETER_HANDLER +static char * +getcwd_nothrow (char *buf, size_t size) +{ + char *result; + + TRY_MSVC_INVAL + { + result = _getcwd (buf, size); + } + CATCH_MSVC_INVAL + { + result = NULL; + errno = ERANGE; + } + DONE_MSVC_INVAL; + + return result; +} +# else +# define getcwd_nothrow _getcwd +# endif +# define getcwd_system getcwd_nothrow +#else +# define getcwd_system getcwd +#endif + /* Get the name of the current working directory, and put it in SIZE bytes of BUF. Returns NULL if the directory couldn't be determined or SIZE was too small. If successful, returns BUF. In GNU, if BUF is @@ -155,7 +187,7 @@ __getcwd (char *buf, size_t size) this wrong result with errno = 0. */ # undef getcwd - dir = getcwd (buf, size); + dir = getcwd_system (buf, size); if (dir || (size && errno == ERANGE)) return dir; @@ -166,7 +198,7 @@ __getcwd (char *buf, size_t size) if (errno == EINVAL && buf == NULL && size == 0) { char big_buffer[BIG_FILE_NAME_LENGTH + 1]; - dir = getcwd (big_buffer, sizeof big_buffer); + dir = getcwd_system (big_buffer, sizeof big_buffer); if (dir) return strdup (dir); } diff --git a/modules/getcwd b/modules/getcwd index 628c4d1..90299a1 100644 --- a/modules/getcwd +++ b/modules/getcwd @@ -13,6 +13,7 @@ Depends-on: unistd extensions pathmax [test $REPLACE_GETCWD = 1] +msvc-inval [test $REPLACE_GETCWD = 1] mempcpy [test $REPLACE_GETCWD = 1] d-ino [test $REPLACE_GETCWD = 1] memmove [test $REPLACE_GETCWD = 1] diff --git a/doc/posix-functions/getcwd.texi b/doc/posix-functions/getcwd.texi index 0702569..41e19c5 100644 --- a/doc/posix-functions/getcwd.texi +++ b/doc/posix-functions/getcwd.texi @@ -23,9 +23,12 @@ instead of @code{size_t} for the size argument when using non-standard headers, and the declaration is missing from @code{}: mingw, MSVC 14. @item +On some platforms, @code{getcwd (buf, 0)} crashes: +MSVC 14. +@item On some platforms, @code{getcwd (buf, 0)} fails with @code{ERANGE} instead of the required @code{EINVAL}: -mingw, MSVC 14. +mingw. @end itemize Portability problems fixed by Gnulib module @code{getcwd}: