From a0044c29421abaac9a7db27697406716ba158048 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 11 Feb 2023 12:15:12 +0200 Subject: [PATCH 19/19] Use fopen_s() instead of fopen() when possible See osdn #46420 Signed-off-by: Marko Lindqvist --- configure.ac | 2 +- gen_headers/meson_fc_config.h.in | 3 +++ utility/support.c | 24 +++++++++++++++++------- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/configure.ac b/configure.ac index 24b66b4c11..b0371d9974 100644 --- a/configure.ac +++ b/configure.ac @@ -1585,7 +1585,7 @@ AC_CHECK_FUNCS([bind connect fileno flock ftime gethostbyname gethostname]) AC_CHECK_FUNCS([getpwuid inet_aton select snooze strcasestr]) AC_CHECK_FUNCS([strerror strstr uname nanosleep usleep]) AC_CHECK_FUNCS([getline _strcoll stricoll _stricoll strcasecoll]) -AC_CHECK_FUNCS([backtrace setenv putenv getcwd]) +AC_CHECK_FUNCS([backtrace setenv putenv getcwd fopen_s]) dnl Possible random sources AC_CHECK_HEADERS([sys/random.h]) diff --git a/gen_headers/meson_fc_config.h.in b/gen_headers/meson_fc_config.h.in index f20bb4ac67..f1e8544b43 100644 --- a/gen_headers/meson_fc_config.h.in +++ b/gen_headers/meson_fc_config.h.in @@ -257,6 +257,9 @@ /* fdopen() available */ #mesondefine HAVE_FDOPEN +/* fopen_s() available */ +#mesondefine HAVE_FOPEN_S + /* fileno() available */ #mesondefine HAVE_FILENO diff --git a/utility/support.c b/utility/support.c index a8e237d08c..a2a86e9e37 100644 --- a/utility/support.c +++ b/utility/support.c @@ -505,17 +505,27 @@ int fc_stricoll(const char *str0, const char *str1) ****************************************************************************/ FILE *fc_fopen(const char *filename, const char *opentype) { -#ifdef FREECIV_MSWINDOWS FILE *result; - char *filename_in_local_encoding = - internal_to_local_string_malloc(filename); - result = fopen(filename_in_local_encoding, opentype); - free(filename_in_local_encoding); - return result; +#ifdef FREECIV_MSWINDOWS + char *real_filename = internal_to_local_string_malloc(filename); #else /* FREECIV_MSWINDOWS */ - return fopen(filename, opentype); + const char *real_filename = filename; #endif /* FREECIV_MSWINDOWS */ + +#ifdef HAVE_FOPEN_S + if (fopen_s(&result, real_filename, opentype) != 0) { + result = NULL; + } +#else /* HAVE_FOPEN_S */ + result = fopen(real_filename, opentype); +#endif /* HAVE_FOPEN_S */ + +#ifdef FREECIV_MSWINDOWS + free(real_filename); +#endif /* FREECIV_MSWINDOWS */ + + return result; } /************************************************************************//** -- 2.39.1