build: Check for invalid parameter overriding on Windows

Allow one to override the invalid parameter handler if we have the
following items:

* _set_invalid_parameter_hander() or
  _set_thread_local_parameter_handler()
* _CrtSetReportMode() as a function or macro

Currently, we are doing this on Visual Studio to allow GSpawn to work on
Windows as well as having the log writer support color output, as we
might be passing in file descriptors that are invalid, which will cause
the CRT to abort unless the default invalid parameter handler is
overridden.
This commit is contained in:
Chun-wei Fan 2022-11-02 23:55:48 +08:00
parent 92730c6226
commit b92b17f021
4 changed files with 26 additions and 6 deletions

View File

@ -121,6 +121,12 @@ GMainContext * g_get_worker_context (void);
gboolean g_check_setuid (void);
GMainContext * g_main_context_new_with_next_id (guint next_id);
#if (defined (HAVE__SET_THREAD_LOCAL_INVALID_PARAMETER_HANDLER) || \
defined (HAVE__SET_INVALID_PARAMETER_HANDLER)) && \
defined (HAVE__CRT_SET_REPORT_MODE)
# define USE_INVALID_PARAMETER_HANDLER
#endif
#ifdef G_OS_WIN32
GLIB_AVAILABLE_IN_ALL
gchar *_glib_get_locale_dir (void);

View File

@ -2112,7 +2112,7 @@ g_log_writer_supports_color (gint output_fd)
#ifdef G_OS_WIN32
gboolean result = FALSE;
#if (defined (_MSC_VER) && _MSC_VER >= 1400)
#ifdef USE_INVALID_PARAMETER_HANDLER
_invalid_parameter_handler oldHandler, newHandler;
int prev_report_mode = 0;
#endif
@ -2143,7 +2143,7 @@ g_log_writer_supports_color (gint output_fd)
*/
#ifdef G_OS_WIN32
#if (defined (_MSC_VER) && _MSC_VER >= 1400)
#ifdef USE_INVALID_PARAMETER_HANDLER
/* Set up our empty invalid parameter handler, for isatty(),
* in case of bad fd's passed in for isatty(), so that
* msvcrt80.dll+ won't abort the program
@ -2185,7 +2185,7 @@ g_log_writer_supports_color (gint output_fd)
result = win32_is_pipe_tty (output_fd);
reset_invalid_param_handler:
#if defined (_MSC_VER) && (_MSC_VER >= 1400)
#ifdef USE_INVALID_PARAMETER_HANDLER
_CrtSetReportMode(_CRT_ASSERT, prev_report_mode);
_set_invalid_parameter_handler (oldHandler);
#endif

View File

@ -33,7 +33,7 @@
* Please see http://msdn.microsoft.com/zh-tw/library/ks2530z6%28v=vs.80%29.aspx
* for an explanation on this.
*/
#if (defined (_MSC_VER) && _MSC_VER >= 1400)
#ifdef USE_INVALID_PARAMETER_HANDLER
#include <crtdbg.h>
#endif
@ -168,7 +168,7 @@ checked_dup2 (int oldfd, int newfd, int report_fd)
return newfd;
}
#if (defined (_MSC_VER) && _MSC_VER >= 1400)
#ifdef USE_INVALID_PARAMETER_HANDLER
/*
* This is the (empty) invalid parameter handler
* that is used for Visual C++ 2005 (and later) builds
@ -221,7 +221,7 @@ main (int ignored_argc, char **ignored_argv)
wchar_t **wargv;
char c;
#if (defined (_MSC_VER) && _MSC_VER >= 1400)
#ifdef USE_INVALID_PARAMETER_HANDLER
/* set up our empty invalid parameter handler */
_invalid_parameter_handler oldHandler, newHandler;
newHandler = myInvalidParameterHandler;

View File

@ -2373,6 +2373,20 @@ install_data('m4macros/glib-2.0.m4', 'm4macros/glib-gettext.m4', 'm4macros/gsett
install_tag : 'devel',
)
# Check whether we support overriding the invalid parameter handler on Windows for _get_osfhandle(),
# g_fsync() (i.e. _commit()), etc
if host_system == 'windows'
if cc.has_function('_set_thread_local_invalid_parameter_handler', prefix: '#include <stdlib.h>')
glib_conf.set('HAVE__SET_THREAD_LOCAL_INVALID_PARAMETER_HANDLER', 1)
endif
if cc.has_function('_set_invalid_parameter_handler', prefix: '#include <stdlib.h>')
glib_conf.set('HAVE__SET_INVALID_PARAMETER_HANDLER', 1)
endif
if cc.has_header_symbol('crtdbg.h', '_CrtSetReportMode')
glib_conf.set('HAVE__CRT_SET_REPORT_MODE', 1)
endif
endif
configure_file(output : 'config.h', configuration : glib_conf)
if host_system == 'windows'