msvc-inval.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* Invalid parameter handler for MSVC runtime libraries.
  2. Copyright (C) 2011-2013 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef _MSVC_INVAL_H
  14. #define _MSVC_INVAL_H
  15. /* With MSVC runtime libraries with the "invalid parameter handler" concept,
  16. functions like fprintf(), dup2(), or close() crash when the caller passes
  17. an invalid argument. But POSIX wants error codes (such as EINVAL or EBADF)
  18. instead.
  19. This file defines macros that turn such an invalid parameter notification
  20. into a non-local exit. An error code can then be produced at the target
  21. of this exit. You can thus write code like
  22. TRY_MSVC_INVAL
  23. {
  24. <Code that can trigger an invalid parameter notification
  25. but does not do 'return', 'break', 'continue', nor 'goto'.>
  26. }
  27. CATCH_MSVC_INVAL
  28. {
  29. <Code that handles an invalid parameter notification
  30. but does not do 'return', 'break', 'continue', nor 'goto'.>
  31. }
  32. DONE_MSVC_INVAL;
  33. This entire block expands to a single statement.
  34. The handling of invalid parameters can be done in three ways:
  35. * The default way, which is reasonable for programs (not libraries):
  36. AC_DEFINE([MSVC_INVALID_PARAMETER_HANDLING], [DEFAULT_HANDLING])
  37. * The way for libraries that make "hairy" calls (like close(-1), or
  38. fclose(fp) where fileno(fp) is closed, or simply getdtablesize()):
  39. AC_DEFINE([MSVC_INVALID_PARAMETER_HANDLING], [HAIRY_LIBRARY_HANDLING])
  40. * The way for libraries that make no "hairy" calls:
  41. AC_DEFINE([MSVC_INVALID_PARAMETER_HANDLING], [SANE_LIBRARY_HANDLING])
  42. */
  43. #define DEFAULT_HANDLING 0
  44. #define HAIRY_LIBRARY_HANDLING 1
  45. #define SANE_LIBRARY_HANDLING 2
  46. #if HAVE_MSVC_INVALID_PARAMETER_HANDLER \
  47. && !(MSVC_INVALID_PARAMETER_HANDLING == SANE_LIBRARY_HANDLING)
  48. /* A native Windows platform with the "invalid parameter handler" concept,
  49. and either DEFAULT_HANDLING or HAIRY_LIBRARY_HANDLING. */
  50. # if MSVC_INVALID_PARAMETER_HANDLING == DEFAULT_HANDLING
  51. /* Default handling. */
  52. # ifdef __cplusplus
  53. extern "C" {
  54. # endif
  55. /* Ensure that the invalid parameter handler in installed that just returns.
  56. Because we assume no other part of the program installs a different
  57. invalid parameter handler, this solution is multithread-safe. */
  58. extern void gl_msvc_inval_ensure_handler (void);
  59. # ifdef __cplusplus
  60. }
  61. # endif
  62. # define TRY_MSVC_INVAL \
  63. do \
  64. { \
  65. gl_msvc_inval_ensure_handler (); \
  66. if (1)
  67. # define CATCH_MSVC_INVAL \
  68. else
  69. # define DONE_MSVC_INVAL \
  70. } \
  71. while (0)
  72. # else
  73. /* Handling for hairy libraries. */
  74. # include <excpt.h>
  75. /* Gnulib can define its own status codes, as described in the page
  76. "Raising Software Exceptions" on microsoft.com
  77. <http://msdn.microsoft.com/en-us/library/het71c37.aspx>.
  78. Our status codes are composed of
  79. - 0xE0000000, mandatory for all user-defined status codes,
  80. - 0x474E550, a API identifier ("GNU"),
  81. - 0, 1, 2, ..., used to distinguish different status codes from the
  82. same API. */
  83. # define STATUS_GNULIB_INVALID_PARAMETER (0xE0000000 + 0x474E550 + 0)
  84. # if defined _MSC_VER
  85. /* A compiler that supports __try/__except, as described in the page
  86. "try-except statement" on microsoft.com
  87. <http://msdn.microsoft.com/en-us/library/s58ftw19.aspx>.
  88. With __try/__except, we can use the multithread-safe exception handling. */
  89. # ifdef __cplusplus
  90. extern "C" {
  91. # endif
  92. /* Ensure that the invalid parameter handler in installed that raises a
  93. software exception with code STATUS_GNULIB_INVALID_PARAMETER.
  94. Because we assume no other part of the program installs a different
  95. invalid parameter handler, this solution is multithread-safe. */
  96. extern void gl_msvc_inval_ensure_handler (void);
  97. # ifdef __cplusplus
  98. }
  99. # endif
  100. # define TRY_MSVC_INVAL \
  101. do \
  102. { \
  103. gl_msvc_inval_ensure_handler (); \
  104. __try
  105. # define CATCH_MSVC_INVAL \
  106. __except (GetExceptionCode () == STATUS_GNULIB_INVALID_PARAMETER \
  107. ? EXCEPTION_EXECUTE_HANDLER \
  108. : EXCEPTION_CONTINUE_SEARCH)
  109. # define DONE_MSVC_INVAL \
  110. } \
  111. while (0)
  112. # else
  113. /* Any compiler.
  114. We can only use setjmp/longjmp. */
  115. # include <setjmp.h>
  116. # ifdef __cplusplus
  117. extern "C" {
  118. # endif
  119. struct gl_msvc_inval_per_thread
  120. {
  121. /* The restart that will resume execution at the code between
  122. CATCH_MSVC_INVAL and DONE_MSVC_INVAL. It is enabled only between
  123. TRY_MSVC_INVAL and CATCH_MSVC_INVAL. */
  124. jmp_buf restart;
  125. /* Tells whether the contents of restart is valid. */
  126. int restart_valid;
  127. };
  128. /* Ensure that the invalid parameter handler in installed that passes
  129. control to the gl_msvc_inval_restart if it is valid, or raises a
  130. software exception with code STATUS_GNULIB_INVALID_PARAMETER otherwise.
  131. Because we assume no other part of the program installs a different
  132. invalid parameter handler, this solution is multithread-safe. */
  133. extern void gl_msvc_inval_ensure_handler (void);
  134. /* Return a pointer to the per-thread data for the current thread. */
  135. extern struct gl_msvc_inval_per_thread *gl_msvc_inval_current (void);
  136. # ifdef __cplusplus
  137. }
  138. # endif
  139. # define TRY_MSVC_INVAL \
  140. do \
  141. { \
  142. struct gl_msvc_inval_per_thread *msvc_inval_current; \
  143. gl_msvc_inval_ensure_handler (); \
  144. msvc_inval_current = gl_msvc_inval_current (); \
  145. /* First, initialize gl_msvc_inval_restart. */ \
  146. if (setjmp (msvc_inval_current->restart) == 0) \
  147. { \
  148. /* Then, mark it as valid. */ \
  149. msvc_inval_current->restart_valid = 1;
  150. # define CATCH_MSVC_INVAL \
  151. /* Execution completed. \
  152. Mark gl_msvc_inval_restart as invalid. */ \
  153. msvc_inval_current->restart_valid = 0; \
  154. } \
  155. else \
  156. { \
  157. /* Execution triggered an invalid parameter notification. \
  158. Mark gl_msvc_inval_restart as invalid. */ \
  159. msvc_inval_current->restart_valid = 0;
  160. # define DONE_MSVC_INVAL \
  161. } \
  162. } \
  163. while (0)
  164. # endif
  165. # endif
  166. #else
  167. /* A platform that does not need to the invalid parameter handler,
  168. or when SANE_LIBRARY_HANDLING is desired. */
  169. /* The braces here avoid GCC warnings like
  170. "warning: suggest explicit braces to avoid ambiguous 'else'". */
  171. # define TRY_MSVC_INVAL \
  172. do \
  173. { \
  174. if (1)
  175. # define CATCH_MSVC_INVAL \
  176. else
  177. # define DONE_MSVC_INVAL \
  178. } \
  179. while (0)
  180. #endif
  181. #endif /* _MSVC_INVAL_H */