1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright © 2024, Synopsys Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above
14  *    copyright notice, this list of conditions and the following
15  *    disclaimer in the documentation and/or other materials provided
16  *    with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #define __STDC_WANT_LIB_EXT1__ 1
36 #include <string.h>
37 #include <stdbool.h>
38 #include <errno.h>
39 #include "string_private.h"
40 
41 /* C11 version; required by LLVM's C++11 library */
42 __errno_t
strerror_s(char * buf,rsize_t buflen,__errno_t errnum)43 strerror_s(char *buf, rsize_t buflen, __errno_t errnum)
44 {
45     int32_t result = 0;
46     const char *msg = "";
47 
48     if (buf == NULL) {
49         msg = "strerror_s: dest is NULL";
50         goto handle_error;
51     }
52 
53     if ((buflen == 0u) || (CHECK_RSIZE(buflen))) {
54         msg = "strerror_s: dest buffer size is 0 or exceeds RSIZE_MAX";
55         goto handle_error;
56     }
57 
58     const char *cp = _strerror_r(errnum, 0, NULL);
59     uint32_t len = strnlen_s(cp, MAX_ERROR_MSG);
60 
61     if (len < buflen) {
62         (void)strncpy(buf, cp, MAX_ERROR_MSG);
63     } else {
64         /* Standard allows truncation of error message with '...' to
65             indicate truncation. */
66         (void)memcpy(buf, cp, (buflen - 1u));
67         buf[(buflen - 1u)] = '\0';
68 
69         if (buflen > 3u) {
70             (void)strncpy(&buf[(buflen - 4u)], "...", 4u);
71         }
72 
73         result = ERANGE;
74     }
75 
76     // Normal return path
77     return result;
78 
79 handle_error:
80     if (__cur_handler != NULL) {
81         __cur_handler(msg, NULL, -1);
82     }
83 
84     return -1;
85 }
86