1 /* Invalid parameter handler for MSVC runtime libraries.
2 Copyright (C) 2011 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 /* With MSVC runtime libraries with the "invalid parameter handler" concept,
22 functions like fprintf(), dup2(), or close() crash when the caller passes
23 an invalid argument. But POSIX wants error codes (such as EINVAL or EBADF)
25 This file defines macros that turn such an invalid parameter notification
26 into a non-local exit. An error code can then be produced at the target
27 of this exit. You can thus write code like
31 <Code that can trigger an invalid parameter notification
32 but does not do 'return', 'break', 'continue', nor 'goto'.>
36 <Code that handles an invalid parameter notification
37 but does not do 'return', 'break', 'continue', nor 'goto'.>
41 This entire block expands to a single statement.
44 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
45 /* A native Windows platform with the "invalid parameter handler" concept. */
47 /* Get _invalid_parameter_handler type and _set_invalid_parameter_handler
52 /* Gnulib can define its own status codes, as described in the page
53 "Raising Software Exceptions" on microsoft.com
54 <http://msdn.microsoft.com/en-us/library/het71c37.aspx>.
55 Our status codes are composed of
56 - 0xE0000000, mandatory for all user-defined status codes,
57 - 0x474E550, a API identifier ("GNU"),
58 - 0, 1, 2, ..., used to distinguish different status codes from the
60 # define STATUS_GNULIB_INVALID_PARAMETER (0xE0000000 + 0x474E550 + 0)
63 /* A compiler that supports __try/__except, as described in the page
64 "try-except statement" on microsoft.com
65 <http://msdn.microsoft.com/en-us/library/s58ftw19.aspx>.
66 With __try/__except, we can use the multithread-safe exception handling. */
72 /* Ensure that the invalid parameter handler in installed that raises a
73 software exception with code STATUS_GNULIB_INVALID_PARAMETER.
74 Because we assume no other part of the program installs a different
75 invalid parameter handler, this solution is multithread-safe. */
76 extern void gl_msvc_inval_ensure_handler (void);
82 # define TRY_MSVC_INVAL \
85 gl_msvc_inval_ensure_handler (); \
87 # define CATCH_MSVC_INVAL \
88 __except (GetExceptionCode () == STATUS_GNULIB_INVALID_PARAMETER \
89 ? EXCEPTION_EXECUTE_HANDLER \
90 : EXCEPTION_CONTINUE_SEARCH)
91 # define DONE_MSVC_INVAL \
97 We can only use setjmp/longjmp. */
105 /* The restart that will resume execution at the code between
106 CATCH_MSVC_INVAL and DONE_MSVC_INVAL. It is enabled only between
107 TRY_MSVC_INVAL and CATCH_MSVC_INVAL. */
108 extern jmp_buf gl_msvc_inval_restart;
110 /* Tells whether the contents of gl_msvc_inval_restart is valid. */
111 extern int gl_msvc_inval_restart_valid;
113 /* Ensure that the invalid parameter handler in installed that passes
114 control to the gl_msvc_inval_restart if it is valid, or raises a
115 software exception with code STATUS_GNULIB_INVALID_PARAMETER otherwise.
116 Because we assume no other part of the program installs a different
117 invalid parameter handler, this solution is multithread-safe. */
118 extern void gl_msvc_inval_ensure_handler (void);
124 # define TRY_MSVC_INVAL \
127 gl_msvc_inval_ensure_handler (); \
128 /* First, initialize gl_msvc_inval_restart. */ \
129 if (setjmp (gl_msvc_inval_restart) == 0) \
131 /* Then, mark it as valid. */ \
132 gl_msvc_inval_restart_valid = 1;
133 # define CATCH_MSVC_INVAL \
134 /* Execution completed. \
135 Mark gl_msvc_inval_restart as invalid. */ \
136 gl_msvc_inval_restart_valid = 0; \
140 /* Execution triggered an invalid parameter notification. \
141 Mark gl_msvc_inval_restart as invalid. */ \
142 gl_msvc_inval_restart_valid = 0;
143 # define DONE_MSVC_INVAL \
151 /* A platform that does not need to the invalid parameter handler. */
153 /* The braces here avoid GCC warnings like
154 "warning: suggest explicit braces to avoid ambiguous `else'". */
155 # define TRY_MSVC_INVAL \
159 # define CATCH_MSVC_INVAL \
161 # define DONE_MSVC_INVAL \
167 #endif /* _MSVC_INVAL_H */