Skip to content

Commit

Permalink
crt: Redirect access() to __mingw_access() on UCRT
Browse files Browse the repository at this point in the history
__mingw_access() was introduced as the msvcrt.dll access() function
reportedly returned errors when passed the X_OK constant on
Vista.

Users who expect to be calling access() with the X_OK constant could
set the __USE_MINGW_ACCESS define to get a mingw specific reimplementation
of the function. GCC has been setting this define to work around this
issue (but there have been cases where the define hasn't applied on all
the source where it's needed).

Current versions of msvcrt.dll no longer seem to have this issue
with X_OK, so the issue has somewhat been forgotten since. But UCRT's
access() function shows the same behaviour of returning errors when
given that constant.

Always defining __USE_MINGW_ACCESS when building targeting UCRT
doesn't work, as the define of access() breaks other valid cases
(e.g. calls to methods named access() in C++ classes).

Instead remove the access() symbol from the import libraries, and
expose an UCRT specific access() that just redirects to __mingw_access().

Signed-off-by: Martin Storsjö <martin@martin.st>
  • Loading branch information
mstorsjo committed May 23, 2022
1 parent 82fe049 commit bceadc5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions mingw-w64-crt/Makefile.am
Expand Up @@ -240,6 +240,7 @@ src_ucrtbase=\
crt/ucrtbase_compat.c \
math/_huge.c \
misc/__initenv.c \
misc/ucrt-access.c \
stdio/ucrt_fprintf.c \
stdio/ucrt_fscanf.c \
stdio/ucrt_fwprintf.c \
Expand Down
4 changes: 4 additions & 0 deletions mingw-w64-crt/def-include/msvcrt-common.def.in
Expand Up @@ -12,7 +12,11 @@ wcscmpi == _wcsicmp
strcasecmp == _stricmp
strncasecmp == _strnicmp

#ifdef UCRTBASE
; access is provided as an alias for __mingw_access
#else
ADD_UNDERSCORE(access)
#endif
ADD_UNDERSCORE(chdir)
ADD_UNDERSCORE(chmod)
ADD_UNDERSCORE(chsize)
Expand Down
Expand Up @@ -3,7 +3,8 @@ LIBRARY api-ms-win-crt-filesystem-l1-1-0
EXPORTS

_access
access == _access
; access is provided as an alias for __mingw_access
; access == _access
_access_s
_chdir
chdir == _chdir
Expand Down
19 changes: 19 additions & 0 deletions mingw-w64-crt/misc/ucrt-access.c
@@ -0,0 +1,19 @@
/**
* This file has no copyright assigned and is placed in the Public Domain.
* This file is part of the mingw-w64 runtime package.
* No warranty is given; refer to the file DISCLAIMER.PD within this package.
*/

#include <io.h>

int __cdecl __mingw_access(const char *fname, int mode);

int __cdecl access(const char *fname, int mode)
{
/* On UCRT, unconditionally forward access to __mingw_access. UCRT's
* access() function return an error if passed the X_OK constant,
* while msvcrt.dll's access() doesn't. (It's reported that msvcrt.dll's
* access() also returned errors on X_OK in the version shipped in Vista,
* but in recent tests it's no longer the case.) */
return __mingw_access(fname, mode);
}

0 comments on commit bceadc5

Please sign in to comment.