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