From 7da18cef6b9d31819ce81342b1a4ecae450ee4fa Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 11 Feb 2023 12:17:43 +0200 Subject: [PATCH 12/12] Use fopen_s() instead of fopen() when possible See osdn #46420 Signed-off-by: Marko Lindqvist --- configure.ac | 2 +- utility/support.c | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/configure.ac b/configure.ac index 0d2e951c91..044741cde2 100644 --- a/configure.ac +++ b/configure.ac @@ -1512,7 +1512,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/utility/support.c b/utility/support.c index fb4f74d253..ee16cc48a6 100644 --- a/utility/support.c +++ b/utility/support.c @@ -495,17 +495,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