Debian Codegraph @

ext/safestringlib/safeclib/memcpy32_s.c

raw symbols SCIP
1 /*------------------------------------------------------------------
2 * memcpy32_s
3 *
4 * October 2008, Bo Berry
5 *
6 * Copyright (c) 2008-2011 Cisco Systems
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
37
38 /**
39 * NAME
40 * memcpy32_s
41 *
42 * SYNOPSIS
43 * #include "safe_mem_lib.h"
44 * errno_t
45 * memcpy32_s(uint32_t *dest, rsize_t dmax,
46 * const uint32_t *src, rsize_t smax)
47 *
48 * DESCRIPTION
49 * This function copies at most smax uint32_ts from src to dest, up to
50 * dmax.
51 *
52 * EXTENSION TO
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 uint32_t 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 0.
72 * dmax shall not be greater than RSIZE_MAX_MEM32.
73 * smax shall not be greater than dmax.
74 * Copying shall not take place between objects that overlap.
75 * If there is a runtime-constraint violation, the memcpy_s function stores
76 * zeros in the first dmax bytes of the object pointed to by dest
77 * if dest is not a null pointer and smax is valid.
78 *
79 * RETURN VALUE
80 * EOK operation sucessful
81 * ESNULLP NULL pointer
82 * ESZEROL length was zero
83 * ESLEMAX length exceeds max
84 * ESOVRLP source memory overlaps destination
85 *
86 * ALSO SEE
87 * memcpy_s(), memcpy16_s(), memmove_s(), memmove16_s(), memmove32_s()
88 *
89 */
90 errno_t
91 memcpy32_s (uint32_t *dest, rsize_t dmax, const uint32_t *src, rsize_t smax)
92 {
93 if (dest == NULL) {
94 invoke_safe_mem_constraint_handler("memcpy32_s: dest is NULL",
95 NULL, ESNULLP);
96 return (RCNEGATE(ESNULLP));
97 }
98
99 if (dmax == 0) {
100 invoke_safe_mem_constraint_handler("memcpy32_s: dmax is 0",
101 NULL, ESZEROL);
102 return (RCNEGATE(ESZEROL));
103 }
104
105 if (dmax > RSIZE_MAX_MEM32) {
106 invoke_safe_mem_constraint_handler("memcpy32_s: dmax exceeds max",
107 NULL, ESLEMAX);
108 return (RCNEGATE(ESLEMAX));
109 }
110
111 if (smax == 0) {
112 mem_prim_set32(dest, dmax, 0);
113 invoke_safe_mem_constraint_handler("memcpy32_s: smax is 0",
114 NULL, ESZEROL);
115 return (RCNEGATE(ESZEROL));
116 }
117
118 if (smax > dmax) {
119 mem_prim_set32(dest, dmax, 0);
120 invoke_safe_mem_constraint_handler("memcpy32_s: smax exceeds dmax",
121 NULL, ESLEMAX);
122 return (RCNEGATE(ESLEMAX));
123 }
124
125 if (src == NULL) {
126 mem_prim_set32(dest, dmax, 0);
127 invoke_safe_mem_constraint_handler("memcpy32_s: src is NULL",
128 NULL, ESNULLP);
129 return (RCNEGATE(ESNULLP));
130 }
131
132 /*
133 * overlap is undefined behavior, do not allow
134 */
135 if( ((dest > src) && (dest < (src+smax))) ||
136 ((src > dest) && (src < (dest+dmax))) ) {
137 mem_prim_set32(dest, dmax, 0);
138 invoke_safe_mem_constraint_handler("memcpy32_s: overlap undefined",
139 NULL, ESOVRLP);
140 return (RCNEGATE(ESOVRLP));
141 }
142
143 /*
144 * now perform the copy
145 */
146 mem_prim_move32(dest, src, smax);
147
148 return (RCNEGATE(EOK));
149 }
150 EXPORT_SYMBOL(memcpy32_s)