]> git.ipfire.org Git - thirdparty/gcc.git/blob - libcilkrts/runtime/sslib/safe_str_constraint.c
17e7fbbb4d2f7717349182228df4ef7ddb4b50f9
[thirdparty/gcc.git] / libcilkrts / runtime / sslib / safe_str_constraint.c
1 /*------------------------------------------------------------------
2 * safe_str_constraint.c
3 *
4 * October 2008, Bo Berry
5 * 2012, Jonathan Toppins <jtoppins@users.sourceforge.net>
6 *
7 * Copyright (c) 2008, 2009, 2012 Cisco Systems
8 * All rights reserved.
9 *
10 * Permission is hereby granted, free of charge, to any person
11 * obtaining a copy of this software and associated documentation
12 * files (the "Software"), to deal in the Software without
13 * restriction, including without limitation the rights to use,
14 * copy, modify, merge, publish, distribute, sublicense, and/or
15 * sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following
17 * conditions:
18 *
19 * The above copyright notice and this permission notice shall be
20 * included in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
24 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
27 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29 * OTHER DEALINGS IN THE SOFTWARE.
30 *------------------------------------------------------------------
31 */
32
33 #include "safeclib_private.h"
34 #include "safe_str_constraint.h"
35 #include "safe_str_lib.h"
36
37
38 static constraint_handler_t str_handler = NULL;
39
40
41 /**
42 * NAME
43 * set_str_constraint_handler_s
44 *
45 * SYNOPSIS
46 * #include "safe_str_lib.h"
47 * constraint_handler_t
48 * set_str_constraint_handler_s(constraint_handler_t handler)
49 *
50 * DESCRIPTION
51 * The set_str_constraint_handler_s function sets the runtime-constraint
52 * handler to be handler. The runtime-constraint handler is the function to
53 * be called when a library function detects a runtime-constraint
54 * violation. Only the most recent handler registered with
55 * set_str_constraint_handler_s is called when a runtime-constraint
56 * violation occurs.
57 * When the handler is called, it is passed the following arguments in
58 * the following order:
59 * 1. A pointer to a character string describing the
60 * runtime-constraint violation.
61 * 2. A null pointer or a pointer to an implementation defined
62 * object.
63 * 3. If the function calling the handler has a return type declared
64 * as errno_t, the return value of the function is passed.
65 * Otherwise, a positive value of type errno_t is passed.
66 * The implementation has a default constraint handler that is used if no
67 * calls to the set_constraint_handler_s function have been made. The
68 * behavior of the default handler is implementation-defined, and it may
69 * cause the program to exit or abort. If the handler argument to
70 * set_constraint_handler_s is a null pointer, the implementation default
71 * handler becomes the current constraint handler.
72 *
73 * SPECIFIED IN
74 * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments
75 * and system software interfaces, Extensions to the C Library,
76 * Part I: Bounds-checking interfaces
77 *
78 * INPUT PARAMETERS
79 * *msg Pointer to the message describing the error
80 *
81 * *ptr Pointer to aassociated data. Can be NULL.
82 *
83 * error The error code encountered.
84 *
85 * OUTPUT PARAMETERS
86 * none
87 *
88 * RETURN VALUE
89 * none
90 *
91 * ALSO SEE
92 * set_str_constraint_handler_s()
93 */
94 constraint_handler_t
95 set_str_constraint_handler_s (constraint_handler_t handler)
96 {
97 constraint_handler_t prev_handler = str_handler;
98 if (NULL == handler) {
99 str_handler = sl_default_handler;
100 } else {
101 str_handler = handler;
102 }
103 return prev_handler;
104 }
105 EXPORT_SYMBOL(set_str_constraint_handler_s);
106
107
108 /**
109 * NAME
110 * invoke_safe_str_constraint_handler
111 *
112 * SYNOPSIS
113 * #include "safe_str_constraint.h"
114 * void
115 * invoke_safe_str_constraint_handler (const char *msg,
116 * void *ptr,
117 * errno_t error)
118 *
119 * DESCRIPTION
120 * Invokes the currently set constraint handler or the default.
121 *
122 * INPUT PARAMETERS
123 * *msg Pointer to the message describing the error
124 *
125 * *ptr Pointer to aassociated data. Can be NULL.
126 *
127 * error The error code encountered.
128 *
129 * OUTPUT PARAMETERS
130 * none
131 *
132 * RETURN VALUE
133 * none
134 *
135 */
136 void
137 invoke_safe_str_constraint_handler (const char *msg,
138 void *ptr,
139 errno_t error)
140 {
141 if (NULL != str_handler) {
142 str_handler(msg, ptr, error);
143 } else {
144 sl_default_handler(msg, ptr, error);
145 }
146 }