glib: Port to the private invalid parameter handler APIs

Use the newly-added private APIs added in the previous commit so that we
can clean up the code a bit.
This commit is contained in:
Chun-wei Fan 2022-11-01 13:07:44 +08:00
parent 08e0fef632
commit 9bcc9405d7
2 changed files with 7 additions and 85 deletions

View File

@ -192,6 +192,7 @@
#include "gcharset.h"
#include "gconvert.h"
#include "genviron.h"
#include "glib-private.h"
#include "gmain.h"
#include "gmem.h"
#include "gprintfint.h"
@ -219,22 +220,6 @@
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
#if defined (_MSC_VER) && (_MSC_VER >=1400)
/* This is ugly, but we need it for isatty() in case we have bad fd's,
* otherwise Windows will abort() the program on msvcrt80.dll and later
*/
#include <crtdbg.h>
_GLIB_EXTERN void
myInvalidParameterHandler(const wchar_t *expression,
const wchar_t *function,
const wchar_t *file,
unsigned int line,
uintptr_t pReserved)
{
}
#endif
#include "gwin32.h"
#endif
@ -2111,12 +2096,7 @@ g_log_writer_supports_color (gint output_fd)
{
#ifdef G_OS_WIN32
gboolean result = FALSE;
#ifdef USE_INVALID_PARAMETER_HANDLER
_invalid_parameter_handler oldHandler, newHandler;
int prev_report_mode = 0;
#endif
GWin32InvalidParameterHandler handler;
#endif
g_return_val_if_fail (output_fd >= 0, FALSE);
@ -2143,17 +2123,7 @@ g_log_writer_supports_color (gint output_fd)
*/
#ifdef G_OS_WIN32
#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
*/
newHandler = myInvalidParameterHandler;
oldHandler = _set_invalid_parameter_handler (newHandler);
/* Disable the message box for assertions. */
prev_report_mode = _CrtSetReportMode(_CRT_ASSERT, 0);
#endif
g_win32_push_empty_invalid_parameter_handler (&handler);
if (g_win32_check_windows_version (10, 0, 0, G_WIN32_OS_ANY))
{
@ -2185,10 +2155,7 @@ g_log_writer_supports_color (gint output_fd)
result = win32_is_pipe_tty (output_fd);
reset_invalid_param_handler:
#ifdef USE_INVALID_PARAMETER_HANDLER
_CrtSetReportMode(_CRT_ASSERT, prev_report_mode);
_set_invalid_parameter_handler (oldHandler);
#endif
g_win32_pop_invalid_parameter_handler (&handler);
return result;
#else

View File

@ -23,20 +23,6 @@
#include <fcntl.h>
/* For _CrtSetReportMode, we don't want Windows CRT (2005 and later)
* to terminate the process if a bad file descriptor is passed into
* _get_osfhandle(). This is necessary because we use _get_osfhandle()
* to check the validity of the fd before we try to call close() on
* it as attempting to close an invalid fd will cause the Windows CRT
* to abort() this program internally.
*
* Please see http://msdn.microsoft.com/zh-tw/library/ks2530z6%28v=vs.80%29.aspx
* for an explanation on this.
*/
#ifdef USE_INVALID_PARAMETER_HANDLER
#include <crtdbg.h>
#endif
#undef G_LOG_DOMAIN
#include "glib.h"
#define GSPAWN_HELPER
@ -168,30 +154,6 @@ checked_dup2 (int oldfd, int newfd, int report_fd)
return newfd;
}
#ifdef USE_INVALID_PARAMETER_HANDLER
/*
* This is the (empty) invalid parameter handler
* that is used for Visual C++ 2005 (and later) builds
* so that we can use this instead of the system automatically
* aborting the process.
*
* This is necessary as we use _get_oshandle() to check the validity
* of the file descriptors as we close them, so when an invalid file
* descriptor is passed into that function as we check on it, we get
* -1 as the result, instead of the gspawn helper program aborting.
*
* Please see http://msdn.microsoft.com/zh-tw/library/ks2530z6%28v=vs.80%29.aspx
* for an explanation on this.
*/
extern void
myInvalidParameterHandler(const wchar_t *expression,
const wchar_t *function,
const wchar_t *file,
unsigned int line,
uintptr_t pReserved);
#endif
#ifndef HELPER_CONSOLE
int _stdcall
WinMain (struct HINSTANCE__ *hInstance,
@ -220,16 +182,7 @@ main (int ignored_argc, char **ignored_argv)
char **argv;
wchar_t **wargv;
char c;
#ifdef USE_INVALID_PARAMETER_HANDLER
/* set up our empty invalid parameter handler */
_invalid_parameter_handler oldHandler, newHandler;
newHandler = myInvalidParameterHandler;
oldHandler = _set_invalid_parameter_handler(newHandler);
/* Disable the message box for assertions. */
_CrtSetReportMode(_CRT_ASSERT, 0);
#endif
GWin32InvalidParameterHandler handler;
/* Fetch the wide-char argument vector */
wargv = CommandLineToArgvW (GetCommandLineW(), &argc);
@ -398,11 +351,13 @@ main (int ignored_argc, char **ignored_argv)
/* argv[ARG_CLOSE_DESCRIPTORS] is "y" if file descriptors from 3
* upwards should be closed
*/
GLIB_PRIVATE_CALL (g_win32_push_empty_invalid_parameter_handler) (&handler);
if (argv[ARG_CLOSE_DESCRIPTORS][0] == 'y')
for (i = 3; i < 1000; i++) /* FIXME real limit? */
if (!g_hash_table_contains (fds, GINT_TO_POINTER (i)))
if (_get_osfhandle (i) != -1)
close (i);
GLIB_PRIVATE_CALL (g_win32_pop_invalid_parameter_handler) (&handler);
/* We don't want our child to inherit the error report and
* helper sync fds.