]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libcilkrts/runtime/sslib/strcpy_s.c
1 /*------------------------------------------------------------------
4 * October 2008, Bo Berry
6 * Copyright (c) 2008-2011 by Cisco Systems, Inc
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or
14 * sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 *------------------------------------------------------------------
32 #include "safeclib_private.h"
33 #include "safe_str_constraint.h"
34 #include "safe_str_lib.h"
42 * #include "safe_str_lib.h"
44 * strcpy_s(char *dest, rsize_t dmax, const char *src)
47 * The strcpy_s function copies the string pointed to by src
48 * (including the terminating null character) into the array
49 * pointed to by dest. All elements following the terminating
50 * null character (if any) written by strcpy_s in the array
51 * of dmax characters pointed to by dest are nulled when
55 * ISO/IEC TR 24731, Programming languages, environments
56 * and system software interfaces, Extensions to the C Library,
57 * Part I: Bounds-checking interfaces
60 * dest pointer to string that will be replaced by src.
62 * dmax restricted maximum length of dest
64 * src pointer to the string that will be copied
71 * Neither dest nor src shall be a null pointer.
72 * dmax shall not be greater than RSIZE_MAX_STR.
73 * dmax shall not equal zero.
74 * dmax shall be greater than strnlen_s(src, dmax).
75 * Copying shall not take place between objects that overlap.
76 * If there is a runtime-constraint violation, then if dest
77 * is not a null pointer and destmax is greater than zero and
78 * not greater than RSIZE_MAX_STR, then strcpy_s nulls dest.
81 * EOK successful operation, the characters in src were
82 * copied into dest and the result is null terminated.
83 * ESNULLP NULL pointer
85 * ESLEMAX length exceeds max limit
86 * ESOVRLP strings overlap
87 * ESNOSPC not enough space to copy src
90 * strcat_s(), strncat_s(), strncpy_s()
94 strcpy_s (char *dest
, rsize_t dmax
, const char *src
)
98 const char *overlap_bumper
;
101 invoke_safe_str_constraint_handler("strcpy_s: dest is null",
103 return RCNEGATE(ESNULLP
);
107 invoke_safe_str_constraint_handler("strcpy_s: dmax is 0",
109 return RCNEGATE(ESZEROL
);
112 if (dmax
> RSIZE_MAX_STR
) {
113 invoke_safe_str_constraint_handler("strcpy_s: dmax exceeds max",
115 return RCNEGATE(ESLEMAX
);
119 #ifdef SAFECLIB_STR_NULL_SLACK
120 /* null string to clear data */
121 while (dmax
) { *dest
= '\0'; dmax
--; dest
++; }
125 invoke_safe_str_constraint_handler("strcpy_s: src is null",
127 return RCNEGATE(ESNULLP
);
131 return RCNEGATE(EOK
);
134 /* hold base of dest in case src was not copied */
139 overlap_bumper
= src
;
142 if (dest
== overlap_bumper
) {
143 handle_error(orig_dest
, orig_dmax
, "strcpy_s: "
144 "overlapping objects",
146 return RCNEGATE(ESOVRLP
);
151 #ifdef SAFECLIB_STR_NULL_SLACK
152 /* null slack to clear any data */
153 while (dmax
) { *dest
= '\0'; dmax
--; dest
++; }
155 return RCNEGATE(EOK
);
164 overlap_bumper
= dest
;
167 if (src
== overlap_bumper
) {
168 handle_error(orig_dest
, orig_dmax
, "strcpy_s: "
169 "overlapping objects",
171 return RCNEGATE(ESOVRLP
);
176 #ifdef SAFECLIB_STR_NULL_SLACK
177 /* null slack to clear any data */
178 while (dmax
) { *dest
= '\0'; dmax
--; dest
++; }
180 return RCNEGATE(EOK
);
190 * the entire src must have been copied, if not reset dest
191 * to null the string.
193 handle_error(orig_dest
, orig_dmax
, "strcpy_s: not "
194 "enough space for src",
196 return RCNEGATE(ESNOSPC
);
198 EXPORT_SYMBOL(strcpy_s
);