msvc-inval: Install handler globally.
[gnulib.git] / lib / msvc-inval.h
1 /* Invalid parameter handler for MSVC runtime libraries.
2    Copyright (C) 2011 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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.  */
17
18 #ifndef _MSVC_INVAL_H
19 #define _MSVC_INVAL_H
20
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)
24    instead.
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
28
29      TRY_MSVC_INVAL
30        {
31          <Code that can trigger an invalid parameter notification
32           but does not do 'return', 'break', 'continue', nor 'goto'.>
33        }
34      CATCH_MSVC_INVAL
35        {
36          <Code that handles an invalid parameter notification
37           but does not do 'return', 'break', 'continue', nor 'goto'.>
38        }
39      DONE_MSVC_INVAL;
40
41    This entire block expands to a single statement.
42  */
43
44 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
45 /* A native Windows platform with the "invalid parameter handler" concept.  */
46
47 /* Get _invalid_parameter_handler type and _set_invalid_parameter_handler
48    declaration.  */
49 # include <stdlib.h>
50 # include <excpt.h>
51
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
59        same API.  */
60 # define STATUS_GNULIB_INVALID_PARAMETER (0xE0000000 + 0x474E550 + 0)
61
62 # if defined _MSC_VER
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.  */
67
68 #  ifdef __cplusplus
69 extern "C" {
70 #  endif
71
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);
77
78 #  ifdef __cplusplus
79 }
80 #  endif
81
82 #  define TRY_MSVC_INVAL \
83      do                                                                        \
84        {                                                                       \
85          gl_msvc_inval_ensure_handler ();                                      \
86          __try
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 \
92        }                                                                       \
93      while (0)
94
95 # else
96 /* Any compiler.
97    We can only use setjmp/longjmp.  */
98
99 #  include <setjmp.h>
100
101 #  ifdef __cplusplus
102 extern "C" {
103 #  endif
104
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;
109
110 /* Tells whether the contents of gl_msvc_inval_restart is valid.  */
111 extern int gl_msvc_inval_restart_valid;
112
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);
119
120 #  ifdef __cplusplus
121 }
122 #  endif
123
124 #  define TRY_MSVC_INVAL \
125      do                                                                        \
126        {                                                                       \
127          gl_msvc_inval_ensure_handler ();                                      \
128          /* First, initialize gl_msvc_inval_restart.  */                       \
129          if (setjmp (gl_msvc_inval_restart) == 0)                              \
130            {                                                                   \
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;                                  \
137            }                                                                   \
138          else                                                                  \
139            {                                                                   \
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 \
144            }                                                                   \
145        }                                                                       \
146      while (0)
147
148 # endif
149
150 #else
151 /* A platform that does not need to the invalid parameter handler.  */
152
153 /* The braces here avoid GCC warnings like
154    "warning: suggest explicit braces to avoid ambiguous `else'".  */
155 # define TRY_MSVC_INVAL \
156     do                                                                         \
157       {                                                                        \
158         if (1)
159 # define CATCH_MSVC_INVAL \
160         else
161 # define DONE_MSVC_INVAL \
162       }                                                                        \
163     while (0)
164
165 #endif
166
167 #endif /* _MSVC_INVAL_H */