safec  2.1
Safe C Library - ISO TR24731 Bounds Checking Interface
strncat_s.c
Go to the documentation of this file.
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 
89 errno_t
90 strncat_s (char * restrict dest, rsize_t dmax, const char * restrict src, rsize_t slen)
91 {
92  rsize_t orig_dmax;
93  char *orig_dest;
94  const char *overlap_bumper;
95 
96  if (dest == NULL) {
97  invoke_safe_str_constraint_handler("strncat_s: dest is null",
98  NULL, ESNULLP);
99  return RCNEGATE(ESNULLP);
100  }
101 
102  if (src == NULL) {
103  invoke_safe_str_constraint_handler("strncat_s: src is null",
104  NULL, ESNULLP);
105  return RCNEGATE(ESNULLP);
106  }
107 
108  if (slen > RSIZE_MAX_STR) {
109  invoke_safe_str_constraint_handler("strncat_s: slen exceeds max",
110  NULL, ESLEMAX);
111  return RCNEGATE(ESLEMAX);
112  }
113 
114  if (dmax == 0) {
115  invoke_safe_str_constraint_handler("strncat_s: dmax is 0",
116  NULL, ESZEROL);
117  return RCNEGATE(ESZEROL);
118  }
119 
120  if (dmax > RSIZE_MAX_STR) {
121  invoke_safe_str_constraint_handler("strncat_s: dmax exceeds max",
122  NULL, ESLEMAX);
123  return RCNEGATE(ESLEMAX);
124  }
125 
126  /* hold base of dest in case src was not copied */
127  orig_dmax = dmax;
128  orig_dest = dest;
129 
130  if (dest < src) {
131  overlap_bumper = src;
132 
133  /* Find the end of dest */
134  while (*dest != '\0') {
135 
136  if (dest == overlap_bumper) {
137  handle_error(orig_dest, orig_dmax, "strncat_s: "
138  "overlapping objects",
139  ESOVRLP);
140  return RCNEGATE(ESOVRLP);
141  }
142 
143  dest++;
144  dmax--;
145  if (dmax == 0) {
146  handle_error(orig_dest, orig_dmax, "strncat_s: "
147  "dest unterminated",
148  ESUNTERM);
149  return RCNEGATE(ESUNTERM);
150  }
151  }
152 
153  while (dmax > 0) {
154  if (dest == overlap_bumper) {
155  handle_error(orig_dest, orig_dmax, "strncat_s: "
156  "overlapping objects",
157  ESOVRLP);
158  return RCNEGATE(ESOVRLP);
159  }
160 
161  /*
162  * Copying truncated before the source null is encountered
163  */
164  if (slen == 0) {
165 #ifdef SAFECLIB_STR_NULL_SLACK
166  /* null remaining string */
167  while (dmax) { *dest = '\0'; dmax--; dest++; }
168 #else
169  *dest = '\0';
170 #endif
171  return RCNEGATE(EOK);
172  }
173 
174  *dest = *src;
175  if (*dest == '\0') {
176 #ifdef SAFECLIB_STR_NULL_SLACK
177  /* null slack to clear data */
178  while (dmax) { *dest = '\0'; dmax--; dest++; }
179 #endif
180  return RCNEGATE(EOK);
181  }
182 
183  dmax--;
184  slen--;
185  dest++;
186  src++;
187  }
188 
189  } else {
190  overlap_bumper = dest;
191 
192  /* Find the end of dest */
193  while (*dest != '\0') {
194 
195  /*
196  * NOTE: no need to check for overlap here since src comes first
197  * in memory and we're not incrementing src here.
198  */
199  dest++;
200  dmax--;
201  if (dmax == 0) {
202  handle_error(orig_dest, orig_dmax, "strncat_s: "
203  "dest unterminated",
204  ESUNTERM);
205  return RCNEGATE(ESUNTERM);
206  }
207  }
208 
209  while (dmax > 0) {
210  if (src == overlap_bumper) {
211  handle_error(orig_dest, orig_dmax, "strncat_s: "
212  "overlapping objects",
213  ESOVRLP);
214  return RCNEGATE(ESOVRLP);
215  }
216 
217  /*
218  * Copying truncated
219  */
220  if (slen == 0) {
221 #ifdef SAFECLIB_STR_NULL_SLACK
222  /* null remaining string */
223  while (dmax) { *dest = '\0'; dmax--; dest++; }
224 #else
225  *dest = '\0';
226 #endif
227  return RCNEGATE(EOK);
228  }
229 
230  *dest = *src;
231  if (*dest == '\0') {
232 #ifdef SAFECLIB_STR_NULL_SLACK
233  /* null slack to clear any data */
234  while (dmax) { *dest = '\0'; dmax--; dest++; }
235 #endif
236  return RCNEGATE(EOK);
237  }
238 
239  dmax--;
240  slen--;
241  dest++;
242  src++;
243  }
244  }
245 
246  /*
247  * the entire src was not copied, so the string will be nulled.
248  */
249  handle_error(orig_dest, orig_dmax, "strncat_s: not enough "
250  "space for src",
251  ESNOSPC);
252  return RCNEGATE(ESNOSPC);
253 }
254 EXPORT_SYMBOL(strncat_s)
void invoke_safe_str_constraint_handler(const char *msg, void *ptr, errno_t error)
Invokes the currently set constraint handler or the default.
errno_t strncat_s(char *restrict dest, rsize_t dmax, const char *restrict src, rsize_t slen)
The strncat_s function appends a copy of the string pointed to by src (including the terminating null...
Definition: strncat_s.c:90