safec  2.1
Safe C Library - ISO TR24731 Bounds Checking Interface
strcpy_s.c
Go to the documentation of this file.
1 /*------------------------------------------------------------------
2  * strcpy_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 
82 errno_t
83 strcpy_s (char * restrict dest, rsize_t dmax, const char * restrict src)
84 {
85  rsize_t orig_dmax;
86  char *orig_dest;
87  const char *overlap_bumper;
88 
89  if (dest == NULL) {
90  invoke_safe_str_constraint_handler("strcpy_s: dest is null",
91  NULL, ESNULLP);
92  return RCNEGATE(ESNULLP);
93  }
94 
95  if (dmax == 0) {
96  invoke_safe_str_constraint_handler("strcpy_s: dmax is 0",
97  NULL, ESZEROL);
98  return RCNEGATE(ESZEROL);
99  }
100 
101  if (dmax > RSIZE_MAX_STR) {
102  invoke_safe_str_constraint_handler("strcpy_s: dmax exceeds max",
103  NULL, ESLEMAX);
104  return RCNEGATE(ESLEMAX);
105  }
106 
107  if (src == NULL) {
108 #ifdef SAFECLIB_STR_NULL_SLACK
109  /* null string to clear data */
110  while (dmax) { *dest = '\0'; dmax--; dest++; }
111 #else
112  *dest = '\0';
113 #endif
114  invoke_safe_str_constraint_handler("strcpy_s: src is null",
115  NULL, ESNULLP);
116  return RCNEGATE(ESNULLP);
117  }
118 
119  if (dest == src) {
120  return RCNEGATE(EOK);
121  }
122 
123  /* hold base of dest in case src was not copied */
124  orig_dmax = dmax;
125  orig_dest = dest;
126 
127  if (dest < src) {
128  overlap_bumper = src;
129 
130  while (dmax > 0) {
131  if (dest == overlap_bumper) {
132  handle_error(orig_dest, orig_dmax, "strcpy_s: "
133  "overlapping objects",
134  ESOVRLP);
135  return RCNEGATE(ESOVRLP);
136  }
137 
138  *dest = *src;
139  if (*dest == '\0') {
140 #ifdef SAFECLIB_STR_NULL_SLACK
141  /* null slack to clear any data */
142  while (dmax) { *dest = '\0'; dmax--; dest++; }
143 #endif
144  return RCNEGATE(EOK);
145  }
146 
147  dmax--;
148  dest++;
149  src++;
150  }
151 
152  } else {
153  overlap_bumper = dest;
154 
155  while (dmax > 0) {
156  if (src == overlap_bumper) {
157  handle_error(orig_dest, orig_dmax, "strcpy_s: "
158  "overlapping objects",
159  ESOVRLP);
160  return RCNEGATE(ESOVRLP);
161  }
162 
163  *dest = *src;
164  if (*dest == '\0') {
165 #ifdef SAFECLIB_STR_NULL_SLACK
166  /* null slack to clear any data */
167  while (dmax) { *dest = '\0'; dmax--; dest++; }
168 #endif
169  return RCNEGATE(EOK);
170  }
171 
172  dmax--;
173  dest++;
174  src++;
175  }
176  }
177 
178  /*
179  * the entire src must have been copied, if not reset dest
180  * to null the string. (only with SAFECLIB_STR_NULL_SLACK)
181  */
182  handle_error(orig_dest, orig_dmax, "strcpy_s: not "
183  "enough space for src",
184  ESNOSPC);
185  return RCNEGATE(ESNOSPC);
186 }
187 EXPORT_SYMBOL(strcpy_s)
errno_t strcpy_s(char *restrict dest, rsize_t dmax, const char *restrict src)
The strcpy_s function copies the string pointed to by src (including the terminating null character) ...
Definition: strcpy_s.c:83
void invoke_safe_str_constraint_handler(const char *msg, void *ptr, errno_t error)
Invokes the currently set constraint handler or the default.