summaryrefslogtreecommitdiff
path: root/glib/glib-private.c
blob: 6b2205f867d7656d716d732203f08f9c7f8ceb7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* GLIB - Library of useful routines for C programming
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Colin Walters <walters@verbum.org>
 */

#include "config.h"

#include "glib-private.h"
#include "glib-init.h"

#ifdef USE_INVALID_PARAMETER_HANDLER
#include <crtdbg.h>
#endif

/**
 * glib__private__:
 * @arg: Do not use this argument
 *
 * Do not call this function; it is used to share private
 * API between glib, gobject, and gio.
 */
GLibPrivateVTable *
glib__private__ (void)
{
  static GLibPrivateVTable table = {
    g_wakeup_new,
    g_wakeup_free,
    g_wakeup_get_pollfd,
    g_wakeup_signal,
    g_wakeup_acknowledge,

    g_get_worker_context,

    g_check_setuid,
    g_main_context_new_with_next_id,

    g_dir_open_with_errno,
    g_dir_new_from_dirp,

    glib_init,

#ifdef G_OS_WIN32
    g_win32_stat_utf8,
    g_win32_lstat_utf8,
    g_win32_readlink_utf8,
    g_win32_fstat,
    g_win32_find_helper_executable_path,
    g_win32_reopen_noninherited,
    g_win32_handle_is_socket,
#endif

    g_win32_push_empty_invalid_parameter_handler,
    g_win32_pop_invalid_parameter_handler,

    g_find_program_for_path,
  };

  return &table;
}

#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, when calling _get_osfhandle(), isatty()
 * and _commit() (via g_fsync()) and so on with an invalid file
 * descriptor.
 *
 * This is necessary so that the gspawn helper and the test programs
 * will continue to run as expected, since we are purposely or
 * forced to use invalid FDs.
 *
 * Please see https://learn.microsoft.com/en-us/cpp/c-runtime-library/parameter-validation?view=msvc-170
 * for an explanation on this.
 */
static void
empty_invalid_parameter_handler (const wchar_t *expression,
                                 const wchar_t *function,
                                 const wchar_t *file,
                                 unsigned int   line,
                                 uintptr_t      pReserved)
{
}

/* fallback to _set_invalid_parameter_handler() if we don't have _set_thread_local_invalid_parameter_handler() */
#ifndef HAVE__SET_THREAD_LOCAL_INVALID_PARAMETER_HANDLER
# define _set_thread_local_invalid_parameter_handler _set_invalid_parameter_handler
#endif

#endif
/*
 * g_win32_push_empty_invalid_parameter_handler:
 * @handler: a possibly uninitialized GWin32InvalidParameterHandler
 */
void
g_win32_push_empty_invalid_parameter_handler (GWin32InvalidParameterHandler *handler)
{
#ifdef USE_INVALID_PARAMETER_HANDLER
  /* use the empty invalid parameter handler to override the default invalid parameter_handler */
  handler->pushed_handler = empty_invalid_parameter_handler;
  handler->old_handler = _set_thread_local_invalid_parameter_handler (handler->pushed_handler);

  /* Disable the message box for assertions. */
  handler->pushed_report_mode = 0;
  handler->prev_report_mode = _CrtSetReportMode(_CRT_ASSERT, handler->pushed_report_mode);
#endif
}

/*
 * g_win32_pop_invalid_parameter_handler:
 * @handler: a GWin32InvalidParameterHandler processed with
 * g_win32_push_empty_invalid_parameter_handler()
 */
void
g_win32_pop_invalid_parameter_handler (GWin32InvalidParameterHandler *handler)
{
#ifdef USE_INVALID_PARAMETER_HANDLER
  G_GNUC_UNUSED _invalid_parameter_handler popped_handler;
  G_GNUC_UNUSED int popped_report_mode;

  /* Restore previous/default invalid parameter handler, check the value returned matches the one we previously pushed */
  popped_handler = _set_thread_local_invalid_parameter_handler (handler->old_handler);
  g_return_if_fail (handler->pushed_handler == popped_handler);

  /* Restore the message box for assertions, check the value returned matches the one we previously pushed */
  popped_report_mode = _CrtSetReportMode(_CRT_ASSERT, handler->prev_report_mode);
  g_return_if_fail (handler->pushed_report_mode == popped_report_mode);
#endif
}