summaryrefslogtreecommitdiff
path: root/devdocs/c/error%2Fset_constraint_handler_s.html
blob: d8c0c19eb95f251419af071cf0dc4073d68a9fba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    <h1 id="firstHeading" class="firstHeading">set_constraint_handler_s, constraint_handler_t</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;stdlib.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">constraint_handler_t set_constraint_handler_s( constraint_handler_t handler );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr>  </table> <p>Configures the handler to be called by all <a href="../error#Bounds_checking" title="c/error">bounds-checked functions</a> on a runtime constraint violation or restores the default handler (if <code>handler</code> is a null pointer)</p>
<p>The handler must be a pointer to function of type <code>constraint_handler_t</code>, which is defined as</p>
<table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;stdlib.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">typedef void (*constraint_handler_t)( const char *restrict msg,
                                      void *restrict ptr,
                                      errno_t error);</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr>  </table> <p>On a runtime constraint violation, it is called with the following arguments:</p>
<div class="t-li1">
<span class="t-li">1)</span> pointer to character string that describes the error</div> <div class="t-li1">
<span class="t-li">2)</span> pointer to an implementation-defined object or a null pointer. Examples of implementation-defined objects are objects that give the name of the function that detected the violation and the line number when the violation was detected</div> <div class="t-li1">
<span class="t-li">3)</span> the error about to be returned by the calling function, if it happens to be one of the functions that return <code>errno_t</code>
</div> <p>If <code>set_constraint_handler_s</code> is never called, the default handler is implementation-defined: it may be <code>abort_handler_s</code>, <code>ignore_handler_s</code>, or some other implementation-defined handler.  As with all bounds-checked functions, <code>set_constraint_handler_s</code> and <code>constraint_handler_t</code> are only guaranteed to be available if <code>__STDC_LIB_EXT1__</code> is defined by the implementation and if the user defines <code>__STDC_WANT_LIB_EXT1__</code> to the integer constant <code>1</code> before including <code>&lt;stdlib.h&gt;</code>.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> handler </td> <td> - </td> <td> pointer to function of type <code>constraint_handler_t</code> or a null pointer </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>A pointer to the previously-installed runtime constraints handler. (note: this pointer is never a null pointer because calling <code>set_constraint_handler_s<span class="br0">(</span><a href="http://en.cppreference.com/w/c/types/NULL"><span class="kw103">NULL</span></a><span class="br0">)</span></code> sets up the system default handler)</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#define __STDC_WANT_LIB_EXT1__ 1
#include &lt;string.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
 
int main(void)
{
#ifdef __STDC_LIB_EXT1__
    char dst[2];
    set_constraint_handler_s(ignore_handler_s);
    int r = strcpy_s(dst, sizeof dst, "Too long!");
    printf("dst = \"%s\", r = %d\n", dst, r);
    set_constraint_handler_s(abort_handler_s);
    r = strcpy_s(dst, sizeof dst, "Too long!");
    printf("dst = \"%s\", r = %d\n", dst, r);
#endif
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">dst = "", r = 22
abort_handler_s was called in response to a runtime-constraint violation.
 
The runtime-constraint violation was caused by the following expression in strcpy_s:
(s1max &lt;= (s2_len=strnlen_s(s2, s1max)) ) (in string_s.c:62)
 
Note to end users: This program was terminated as a result
of a bug present in the software. Please reach out to your
software's vendor to get more help.
Aborted</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> K.3.6/2 constraint_handler_t (p: 604) </li>
<li> K.3.6.1.1 The set_constraint_handler_s function (p: 604-605) </li>
</ul>
</ul>    <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="abort_handler_s" title="c/error/abort handler s"> <span class="t-lines"><span>abort_handler_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> abort callback for the bounds-checked functions <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="ignore_handler_s" title="c/error/ignore handler s"> <span class="t-lines"><span>ignore_handler_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> ignore callback for the bounds-checked functions <br> <span class="t-mark">(function)</span>  </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/error/set_constraint_handler_s" class="_attribution-link">https://en.cppreference.com/w/c/error/set_constraint_handler_s</a>
  </p>
</div>