Debian Codegraph @

ext/safestringlib/safeclib/wmemcpy_s.c

raw symbols SCIP
1 /*------------------------------------------------------------------
2 * wmemcpy_s
3 *
4 * AUgust 2014, D. Wheeler
5 *
6 * Copyright (c) 2014 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_mem_constraint.h"
34 #include "mem_primitives_lib.h"
35 #include "safe_mem_lib.h"
36 #include <wchar.h>
37
38
39 /**
40 * NAME
41 * wmemcpy_s
42 *
43 * SYNOPSIS
44 * #include "safe_mem_lib.h"
45 * errno_t
46 * wmemcpy_s(wchar_t* dest, rsize_t dmax, const wchar_t* src, rsize_t smax)
47 *
48 * DESCRIPTION
49 * This function copies at most smax wide characters from src to dest, up to
50 * dmax.
51 *
52 * SPECIFIED IN
53 * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments
54 * and system software interfaces, Extensions to the C Library,
55 * Part I: Bounds-checking interfaces
56 *
57 * INPUT PARAMETERS
58 * dest pointer to memory that will be replaced by src.
59 *
60 * dmax maximum length of the resulting dest
61 *
62 * src pointer to the memory that will be copied to dest
63 *
64 * smax maximum number bytes of src to copy
65 *
66 * OUTPUT PARAMETERS
67 * dest is updated
68 *
69 * RUNTIME CONSTRAINTS
70 * Neither dest nor src shall be a null pointer.
71 * Neither dmax nor smax shall be zero.
72 * dmax shall not be greater than RSIZE_MAX_MEM.
73 * smax shall not be greater than dmax.
74 * Copying shall not take place between regions that overlap.
75 * If there is a runtime-constraint violation, the memcpy_s function
76 * stores zeros in the first dmax bytes of the region pointed to
77 * by dest if dest is not a null pointer and smax is valid.
78 *
79 * RETURN VALUE
80 * EOK successful operation
81 * ESNULLP NULL pointer
82 * ESZEROL zero length
83 * ESLEMAX length exceeds max limit
84 * ESOVRLP source memory overlaps destination
85 *
86 * ALSO SEE
87 * memcpy16_s(), memcpy32_s(), memcpy_s(),
88 * wmemmove_s(), memmove_s(), memmove16_s(), memmove32_s(),
89 *
90 */
91 errno_t
92 wmemcpy_s(wchar_t* dest, rsize_t dmax, const wchar_t* src, rsize_t smax)
93 {
94 wchar_t *dp;
95 const wchar_t *sp;
96
97 dp = dest;
98 sp = src;
99
100 if (dp == NULL) {
101 invoke_safe_mem_constraint_handler("wmemcpy_s: dest is NULL",
102 NULL, ESNULLP);
103 return RCNEGATE(ESNULLP);
104 }
105
106 if (dmax == 0) {
107 invoke_safe_mem_constraint_handler("wmemcpy_s: dmax is 0",
108 NULL, ESZEROL);
109 return RCNEGATE(ESZEROL);
110 }
111
112 if (dmax > RSIZE_MAX_MEM) {
113 invoke_safe_mem_constraint_handler("wmemcpy_s: dmax exceeds max",
114 NULL, ESLEMAX);
115 return RCNEGATE(ESLEMAX);
116 }
117
118 if (smax == 0) {
119 mem_prim_set(dp, dmax*sizeof(wchar_t), 0);
120 invoke_safe_mem_constraint_handler("wmemcpy_s: smax is 0",
121 NULL, ESZEROL);
122 return RCNEGATE(ESZEROL);
123 }
124
125 if (smax > dmax) {
126 mem_prim_set(dp, dmax*sizeof(wchar_t), 0);
127 invoke_safe_mem_constraint_handler("wmemcpy_s: smax exceeds dmax",
128 NULL, ESLEMAX);
129 return RCNEGATE(ESLEMAX);
130 }
131
132 if (sp == NULL) {
133 mem_prim_set(dp, dmax*sizeof(wchar_t), 0);
134 invoke_safe_mem_constraint_handler("wmemcpy_s: src is NULL",
135 NULL, ESNULLP);
136 return RCNEGATE(ESNULLP);
137 }
138
139
140 /*
141 * overlap is undefined behavior, do not allow
142 */
143 if( ((dp > sp) && (dp < (sp+smax))) ||
144 ((sp > dp) && (sp < (dp+dmax))) ) {
145 mem_prim_set(dp, dmax*sizeof(wchar_t), 0);
146 invoke_safe_mem_constraint_handler("wmemcpy_s: overlap undefined",
147 NULL, ESOVRLP);
148 return RCNEGATE(ESOVRLP);
149 }
150
151 /*
152 * now perform the copy
153 */
154 mem_prim_move(dp, sp, smax*sizeof(wchar_t));
155
156 return RCNEGATE(EOK);
157 }
158 EXPORT_SYMBOL(wmemcpy_s)