Debian Codegraph @

ext/safestringlib/safeclib/stpncpy_s.c

raw symbols SCIP
1 /*------------------------------------------------------------------
2 * stpncpy_s.c
3 *
4 * August 2014, D Wheeler
5 *
6 * Copyright (c) 2014 by Intel Corp
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 * stpncpy_s
40 *
41 * SYNOPSIS
42 * #include "safe_str_lib.h"
43 * char *
44 * stpncpy_s(char *dest, rsize_t dmax, const char *src, rsize_t smax, errno_t *err);
45 *
46 * DESCRIPTION
47 * The stpncpy_s function copies at most smax characters from the string
48 * pointed to by src, including the terminating null byte ('\0'), to the
49 * array pointed to by dest. Exactly smax characters are written at dest.
50 * If the length strlen_s(src) is smaller than smax, the remaining smax
51 * characters in the array pointed to by dest are filled with null bytes.
52 * If the length strlen_s(src) is greater than or equal to smax, the string
53 * pointed to by dest will contain smax characters from src plus a null
54 * characters (dest will be null-terminated).
55 *
56 * Therefore, dmax must be at least smax+1 in order to contain the terminator.
57 *
58 * The function returns a pointer to the end of the string in dest -
59 * that is to the null terminator of dest. If an error occurs,
60 * NULL is returned and err is set to the error encountered.
61 *
62 * SPECIFIED IN
63 * ISO/IEC TR 24731, Programming languages, environments
64 * and system software interfaces, Extensions to the C Library,
65 * Part I: Bounds-checking interfaces
66 *
67 * INPUT PARAMETERS
68 * dest pointer to string that will be replaced by src.
69 *
70 * dmax restricted maximum length of dest (must be at least smax+1)
71 *
72 * src pointer to the string that will be copied
73 * to dest
74 *
75 * smax the maximum number of characters from src to copy into dest
76 *
77 * err the error code upon error, or EOK if successful
78 *
79 * OUTPUT PARAMETERS
80 * dest updated
81 * err updated as follows:
82 * EOK successful operation, the characters in src were
83 * copied into dest and the result is null terminated,
84 * and dest is returned to point to the first null at end of dest.
85 * On error, NULL is returned and err is set to one of hte following:
86 * ESNULLP NULL pointer
87 * ESZEROL zero length
88 * ESLEMAX length exceeds max limit
89 * ESOVRLP strings overlap
90 * ESNOSPC not enough space to copy src
91 *
92 * RUNTIME CONSTRAINTS
93 * Neither dest nor src shall be a null pointer.
94 * dmax shall not be greater than RSIZE_MAX_STR.
95 * dmax shall not equal zero.
96 * dmax must be at least smax+1 to allow filling dest with smax characters plus NULL.
97 * If src and dest overlap, copying shall be stopped; destruction of src may have occurred.
98 * If there is a runtime-constraint violation, then:
99 * if dest is not a null pointer and dmax is greater than zero and
100 * not greater than RSIZE_MAX_STR, then stpncpy_s shall fill dest with nulls,
101 * if library was compiled with SAFECLIB_STR_NULL_SLACK.
102 *
103 * RETURN VALUE
104 * a char pointer to the terminating null at the end of dest
105 * or NULL pointer on error
106 *
107 * ALSO SEE
108 * stpcpy_s(), strcpy_s(), strcat_s(), strncat_s(), strncpy_s()
109 *
110 */
111 char *
112 stpncpy_s(char *dest, rsize_t dmax, const char *src, rsize_t smax, errno_t *err)
113 {
114 rsize_t orig_dmax;
115 char *orig_dest;
116
117 if (dest == NULL) {
118 invoke_safe_str_constraint_handler("stpncpy_s: dest is null",
119 NULL, ESNULLP);
120 *err = RCNEGATE(ESNULLP);
121 return NULL;
122 }
123
124 if (src == NULL) {
125 invoke_safe_str_constraint_handler("stpncpy_s: src is null",
126 NULL, ESNULLP);
127 *err = RCNEGATE(ESNULLP);
128 dest[0] = '\0';
129 return NULL;
130 }
131
132 if (dmax == 0) {
133 invoke_safe_str_constraint_handler("stpncpy_s: dmax is 0",
134 NULL, ESZEROL);
135 *err = RCNEGATE(ESZEROL);
136 return NULL;
137 }
138
139 if (dmax > RSIZE_MAX_STR) {
140 invoke_safe_str_constraint_handler("stpncpy_s: dmax exceeds max",
141 NULL, ESLEMAX);
142 *err = RCNEGATE(ESLEMAX);
143 return NULL;
144 }
145
146 if (smax > RSIZE_MAX_STR) {
147 invoke_safe_str_constraint_handler("stpncpy_s: smax exceeds max",
148 NULL, ESLEMAX);
149 *err = RCNEGATE(ESLEMAX);
150 return NULL;
151 }
152
153 if (dmax < (smax+1)) {
154 invoke_safe_str_constraint_handler("stpncpy_s: dmax too short for smax",
155 NULL, ESNOSPC);
156 *err = RCNEGATE(ESNOSPC);
157 dest[0] = '\0';
158 return NULL;
159 }
160
161 /* dmwheel1: Add check to prevent destruction of overlap into destination */
162 if ((src < dest) && ((src+smax) >= dest)) {
163 invoke_safe_str_constraint_handler("stpncpy_s: src+smax overlaps into dest",
164 NULL, ESOVRLP);
165 *err = RCNEGATE(ESOVRLP);
166 dest[0] = '\0';
167 return NULL;
168 }
169
170 /* dmwheel1: Add check to prevent destruction of overlap into source */
171 if ((dest < src) && ((dest+smax) >= src)) {
172 invoke_safe_str_constraint_handler("stpncpy_s: dest+smax overlaps into src",
173 NULL, ESOVRLP);
174 *err = RCNEGATE(ESOVRLP);
175 dest[0] = '\0';
176 return NULL;
177 }
178
179 #ifdef SAFECLIB_STR_NULL_SLACK
180 /* dmwheel1: Add check to prevent destruction of overlap into destination */
181 if ((src < dest) && ((src+dmax) >= dest)) {
182 invoke_safe_str_constraint_handler("stpncpy_s: src+dmax overlaps into dest",
183 NULL, ESOVRLP);
184 *err = RCNEGATE(ESOVRLP);
185 return NULL;
186 }
187
188 /* dmwheel1: Add check to prevent destruction of overlap into source */
189 if ((dest < src) && ((dest+dmax) >= src)) {
190 invoke_safe_str_constraint_handler("stpncpy_s: dest+dmax overlaps into src",
191 NULL, ESOVRLP);
192 *err = RCNEGATE(ESOVRLP);
193 return NULL;
194 }
195 #endif
196
197
198 if (src == NULL) {
199 #ifdef SAFECLIB_STR_NULL_SLACK
200 /* null string to clear data */
201 while (dmax) { *dest = '\0'; dmax--; dest++; }
202 #else
203 *dest = '\0';
204 #endif
205 invoke_safe_str_constraint_handler("stpncpy_s: src is null",
206 NULL, ESNULLP);
207 *err = RCNEGATE(ESNULLP);
208 return NULL;
209 }
210
211 /* hold base of dest in case src was not copied */
212 orig_dmax = dmax;
213 orig_dest = dest;
214
215 if (dest == src) {
216 /* look for the terminating null character, or return err if not found in dmax bytes */
217 while (dmax > 0) {
218 if (*dest == '\0') {
219 /* add nulls to complete smax */
220 char *filler = dest; /* don't change dest, because we need to return it */
221 while (smax) { *filler = '\0'; dmax--; smax--; filler++; }
222 #ifdef SAFECLIB_STR_NULL_SLACK
223 /* null dmax slack to clear any data */
224 while (dmax) { *filler = '\0'; dmax--; filler++; }
225 #endif
226 *err = RCNEGATE(EOK);
227 return dest;
228 }
229 dmax--;
230 dest++;
231 if (--smax == 0) {
232 /* we have copied smax characters, add null terminator */
233 *dest = '\0';
234 }
235 }
236 /* null terminator not found in src before end of dmax */
237 handle_error(orig_dest, orig_dmax, "stpncpy_s: not enough space for src",
238 ESNOSPC);
239 *err = RCNEGATE(ESNOSPC);
240 return NULL;
241 }
242
243
244 /* All checks for buffer overlaps were made, just do the copies */
245 while (dmax > 0) {
246
247 *dest = *src; /* Copy the data into the destination */
248
249 /* Check for maximum copy from source */
250 if (smax == 0) {
251 /* we have copied smax characters, add null terminator */
252 *dest = '\0';
253 }
254
255 /* Check for end of copying */
256 if (*dest == '\0') {
257 /* add nulls to complete smax, if fewer than smax characters
258 * were in src when the NULL was encountered */
259 char *filler = dest; /* don't change dest, because we need to return it */
260 while (smax) { *filler = '\0'; dmax--; smax--; filler++; }
261 #ifdef SAFECLIB_STR_NULL_SLACK
262 /* null dmax slack to clear any data */
263 while (dmax) { *filler = '\0'; dmax--; filler++; }
264 #endif
265 *err = RCNEGATE(EOK);
266 return dest;
267 }
268 dmax--;
269 smax--;
270 dest++;
271 src++;
272
273 }
274 /*
275 * Ran out of space in dest, and did not find the null terminator in src
276 */
277 handle_error(orig_dest, orig_dmax, "stpncpy_s: not enough space for src",
278 ESNOSPC);
279 *err = RCNEGATE(ESNOSPC);
280 return NULL;
281 }
282 EXPORT_SYMBOL(stpncpy_s)