Debian Codegraph @

ext/safestringlib/safeclib/strncat_s.c

raw symbols SCIP
1 /*------------------------------------------------------------------
2 * strncat_s.c
3 *
4 * October 2008, Bo Berry
5 *
6 * Copyright (c) 2008-2011 by Cisco Systems, Inc
7 * All rights reserved.
8 *
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
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
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 *------------------------------------------------------------------
30 */
31
32 #include "safeclib_private.h"
33 #include "safe_str_constraint.h"
34 #include "safe_str_lib.h"
35
36
37 /**
38 * NAME
39 * strncat_s
40 *
41 * SYNOPSIS
42 * #include "safe_str_lib.h"
43 * errno_t
44 * strncat_s(char *dest, rsize_t dmax, const char *src, rsize_t slen)
45 *
46 * DESCRIPTION
47 * The strncat_s function appends a copy of the string pointed
48 * to by src (including the terminating null character) to the
49 * end of the string pointed to by dest. The initial character
50 * from src overwrites the null character at the end of dest.
51 *
52 * All elements following the terminating null character (if
53 * any) written by strncat_s in the array of dmax characters
54 * pointed to by dest take unspecified values when strncat_s returns.
55 *
56 * SPECIFIED IN
57 * ISO/IEC TR 24731, Programming languages, environments
58 * and system software interfaces, Extensions to the C Library,
59 * Part I: Bounds-checking interfaces
60 *
61 * INPUT PARAMETERS
62 * dest pointer to string that will be extended by src
63 * if dmax allows. The string is null terminated.
64 * If the resulting concatenated string is less
65 * than dmax, the remaining slack space is nulled.
66 *
67 * dmax restricted maximum length of the resulting dest,
68 * including the null
69 *
70 * src pointer to the string that will be concatenaed
71 * to string dest
72 *
73 * slen maximum characters to append
74 *
75 * OUTPUT PARAMETERS
76 * dest updated string
77 *
78 * RUNTIME CONSTRAINTS
79 * Neither dest nor src shall be a null pointer
80 * dmax shall not equal zero
81 * dmax shall not be greater than RSIZE_STR_MAX
82 * dmax shall be greater than strnlen_s(src,m).
83 * Copying shall not takeplace between objects that overlap
84 * If there is a runtime-constraint violation, then if dest is
85 * not a null pointer and dmax is greater than zero and not
86 * greater thanRSIZE_MAX, then strncat_s sets dest[0] to the
87 * null character.
88 *
89 * RETURN VALUE
90 * EOK successful operation, all the characters from src
91 * were appended to dest and the result in dest is
92 * null terminated.
93 * ESNULLP NULL pointer
94 * ESZEROL zero length
95 * ESLEMAX length exceeds max limit
96 * ESUNTERM dest not terminated
97 *
98 *
99 */
100 errno_t
101 strncat_s (char *dest, rsize_t dmax, const char *src, rsize_t slen)
102 {
103 rsize_t orig_dmax;
104 char *orig_dest;
105 const char *overlap_bumper;
106
107 if (dest == NULL) {
108 invoke_safe_str_constraint_handler("strncat_s: dest is null",
109 NULL, ESNULLP);
110 return RCNEGATE(ESNULLP);
111 }
112
113 if (src == NULL) {
114 invoke_safe_str_constraint_handler("strncat_s: src is null",
115 NULL, ESNULLP);
116 return RCNEGATE(ESNULLP);
117 }
118
119 if (slen > RSIZE_MAX_STR) {
120 invoke_safe_str_constraint_handler("strncat_s: slen exceeds max",
121 NULL, ESLEMAX);
122 return RCNEGATE(ESLEMAX);
123 }
124
125 if (dmax == 0) {
126 invoke_safe_str_constraint_handler("strncat_s: dmax is 0",
127 NULL, ESZEROL);
128 return RCNEGATE(ESZEROL);
129 }
130
131 if (dmax > RSIZE_MAX_STR) {
132 invoke_safe_str_constraint_handler("strncat_s: dmax exceeds max",
133 NULL, ESLEMAX);
134 return RCNEGATE(ESLEMAX);
135 }
136
137 /* hold base of dest in case src was not copied */
138 orig_dmax = dmax;
139 orig_dest = dest;
140
141 if (dest < src) {
142 overlap_bumper = src;
143
144 /* Find the end of dest */
145 while (*dest != '\0') {
146
147 if (dest == overlap_bumper) {
148 handle_error(orig_dest, orig_dmax, "strncat_s: "
149 "overlapping objects",
150 ESOVRLP);
151 return RCNEGATE(ESOVRLP);
152 }
153
154 dest++;
155 dmax--;
156 if (dmax == 0) {
157 handle_error(orig_dest, orig_dmax, "strncat_s: "
158 "dest unterminated",
159 ESUNTERM);
160 return RCNEGATE(ESUNTERM);
161 }
162 }
163
164 while (dmax > 0) {
165 if (dest == overlap_bumper) {
166 handle_error(orig_dest, orig_dmax, "strncat_s: "
167 "overlapping objects",
168 ESOVRLP);
169 return RCNEGATE(ESOVRLP);
170 }
171
172 /*
173 * Copying truncated before the source null is encountered
174 */
175 if (slen == 0) {
176 #ifdef SAFECLIB_STR_NULL_SLACK
177 /* null remaining string */
178 while (dmax) { *dest = '\0'; dmax--; dest++; }
179 #else
180 *dest = '\0';
181 #endif
182 return RCNEGATE(EOK);
183 }
184
185 *dest = *src;
186 if (*dest == '\0') {
187 #ifdef SAFECLIB_STR_NULL_SLACK
188 /* null slack to clear data */
189 while (dmax) { *dest = '\0'; dmax--; dest++; }
190 #endif
191 return RCNEGATE(EOK);
192 }
193
194 dmax--;
195 slen--;
196 dest++;
197 src++;
198 }
199
200 } else {
201 overlap_bumper = dest;
202
203 /* Find the end of dest */
204 while (*dest != '\0') {
205
206 /*
207 * NOTE: no need to check for overlap here since src comes first
208 * in memory and we're not incrementing src here.
209 */
210 dest++;
211 dmax--;
212 if (dmax == 0) {
213 handle_error(orig_dest, orig_dmax, "strncat_s: "
214 "dest unterminated",
215 ESUNTERM);
216 return RCNEGATE(ESUNTERM);
217 }
218 }
219
220 while (dmax > 0) {
221 if (src == overlap_bumper) {
222 handle_error(orig_dest, orig_dmax, "strncat_s: "
223 "overlapping objects",
224 ESOVRLP);
225 return RCNEGATE(ESOVRLP);
226 }
227
228 /*
229 * Copying truncated
230 */
231 if (slen == 0) {
232 #ifdef SAFECLIB_STR_NULL_SLACK
233 /* null remaining string */
234 while (dmax) { *dest = '\0'; dmax--; dest++; }
235 #else
236 *dest = '\0';
237 #endif
238 return RCNEGATE(EOK);
239 }
240
241 *dest = *src;
242 if (*dest == '\0') {
243 #ifdef SAFECLIB_STR_NULL_SLACK
244 /* null slack to clear any data */
245 while (dmax) { *dest = '\0'; dmax--; dest++; }
246 #endif
247 return RCNEGATE(EOK);
248 }
249
250 dmax--;
251 slen--;
252 dest++;
253 src++;
254 }
255 }
256
257 /*
258 * the entire src was not copied, so the string will be nulled.
259 */
260 handle_error(orig_dest, orig_dmax, "strncat_s: not enough "
261 "space for src",
262 ESNOSPC);
263 return RCNEGATE(ESNOSPC);
264 }
265 EXPORT_SYMBOL(strncat_s)