| 1 | /*------------------------------------------------------------------ |
| 2 | * safe_mem_constraint.c |
| 3 | * |
| 4 | * October 2008, Bo Berry |
| 5 | * 2012, Jonathan Toppins <[email protected]> |
| 6 | * |
| 7 | * Copyright (c) 2008-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_mem_constraint.h" |
| 35 | #include "safe_mem_lib.h" |
| 36 | |
| 37 | |
| 38 | static constraint_handler_t mem_handler = NULL; |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * NAME |
| 43 | * set_mem_constraint_handler_s |
| 44 | * |
| 45 | * SYNOPSIS |
| 46 | * #include "safe_mem_lib.h" |
| 47 | * constraint_handler_t |
| 48 | * set_mem_constraint_handler_straint_handler_t handler) |
| 49 | * |
| 50 | * DESCRIPTION |
| 51 | * The set_mem_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 | * order: |
| 55 | * 1. A pointer to a character string describing the |
| 56 | * runtime-constraint violation. |
| 57 | * 2. A null pointer or a pointer to an implementation defined |
| 58 | * object. |
| 59 | * 3. If the function calling the handler has a return type declared |
| 60 | * as errno_t, the return value of the function is passed. |
| 61 | * Otherwise, a positive value of type errno_t is passed. |
| 62 | * The implementation has a default constraint handler that is used if no |
| 63 | * calls to the set_constraint_handler_s function have been made. The |
| 64 | * behavior of the default handler is implementation-defined, and it may |
| 65 | * cause the program to exit or abort. If the handler argument to |
| 66 | * set_constraint_handler_s is a null pointer, the implementation default |
| 67 | * handler becomes the current constraint handler. |
| 68 | * |
| 69 | * SPECIFIED IN |
| 70 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments |
| 71 | * and system software interfaces, Extensions to the C Library, |
| 72 | * Part I: Bounds-checking interfaces |
| 73 | * |
| 74 | * INPUT PARAMETERS |
| 75 | * *msg Pointer to the message describing the error |
| 76 | * |
| 77 | * *ptr Pointer to aassociated data. Can be NULL. |
| 78 | * |
| 79 | * error The error code encountered. |
| 80 | * |
| 81 | * OUTPUT PARAMETERS |
| 82 | * none |
| 83 | * |
| 84 | * RETURN VALUE |
| 85 | * none |
| 86 | * |
| 87 | * ALSO SEE |
| 88 | * set_str_constraint_handler_s() |
| 89 | */ |
| 90 | constraint_handler_t |
| 91 | set_mem_constraint_handler_s (constraint_handler_t handler) |
| 92 | { |
| 93 | constraint_handler_t prev_handler = mem_handler; |
| 94 | if (NULL == handler) { |
| 95 | mem_handler = sl_default_handler; |
| 96 | } else { |
| 97 | mem_handler = handler; |
| 98 | } |
| 99 | return prev_handler; |
| 100 | } |
| 101 | EXPORT_SYMBOL(set_mem_constraint_handler_s) |
| 102 | |
| 103 | |
| 104 | /** |
| 105 | * NAME |
| 106 | * invoke_safe_mem_constraint_handler |
| 107 | * |
| 108 | * SYNOPSIS |
| 109 | * #include "safe_mem_constraint.h" |
| 110 | * void |
| 111 | * invoke_safe_mem_constraint_handler(const char *msg, |
| 112 | * void *ptr, |
| 113 | * errno_t error) |
| 114 | * |
| 115 | * DESCRIPTION |
| 116 | * Invokes the currently set constraint handler or the default. |
| 117 | * |
| 118 | * INPUT PARAMETERS |
| 119 | * *msg Pointer to the message describing the error |
| 120 | * |
| 121 | * *ptr Pointer to aassociated data. Can be NULL. |
| 122 | * |
| 123 | * error The error code encountered. |
| 124 | * |
| 125 | * OUTPUT PARAMETERS |
| 126 | * none |
| 127 | * |
| 128 | * RETURN VALUE |
| 129 | * none |
| 130 | * |
| 131 | */ |
| 132 | void |
| 133 | invoke_safe_mem_constraint_handler (const char *msg, |
| 134 | void *ptr, |
| 135 | errno_t error) |
| 136 | { |
| 137 | if (NULL != mem_handler) { |
| 138 | mem_handler(msg, ptr, error); |
| 139 | } else { |
| 140 | sl_default_handler(msg, ptr, error); |
| 141 | } |
| 142 | } |