Debian Codegraph @

ext/safestringlib/safeclib/memcpy16_s.c

raw symbols SCIP
1 /*------------------------------------------------------------------
2 * memcpy16_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 * memcpy16_s
41 *
42 * SYNOPSIS
43 * #include "safe_mem_lib.h"
44 * errno_t
45 * memcpy16_s(uint16_t *dest, rsize_t dmax,
46 * const uint16_t *src, rsize_t smax)
47 *
48 * DESCRIPTION
49 * This function copies at most smax uint16_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 uint16_t of src to copy
65 *
66 *
67 * OUTPUT PARAMETERS
68 * dest is updated
69 *
70 * RUNTIME CONSTRAINTS
71 * Neither dest nor src shall be a null pointer.
72 * Neither dmax nor smax shall be 0.
73 * dmax shall not be greater than RSIZE_MAX_MEM16.
74 * smax shall not be greater than dmax.
75 * Copying shall not take place between objects that overlap.
76 * If there is a runtime-constraint violation, the memcpy_s function stores
77 * zeros in the first dmax bytes of the object pointed to by dest
78 * if dest is not a null pointer and smax is valid.
79 *
80 * RETURN VALUE
81 * EOK successful operation
82 * ESNULLP NULL pointer
83 * ESZEROL zero length
84 * ESLEMAX length exceeds max limit
85 * ESOVRLP source memory overlaps destination
86 *
87 * ALSO SEE
88 * memcpy_s(), memcpy32_s(), memmove_s(), memmove16_s(), memmove32_s()
89 *
90 */
91 errno_t
92 memcpy16_s (uint16_t *dest, rsize_t dmax, const uint16_t *src, rsize_t smax)
93 {
94 if (dest == NULL) {
95 invoke_safe_mem_constraint_handler("memcpy16_s: dest is NULL",
96 NULL, ESNULLP);
97 return (RCNEGATE(ESNULLP));
98 }
99
100 if (dmax == 0) {
101 invoke_safe_mem_constraint_handler("memcpy16_s: dmax is 0",
102 NULL, ESZEROL);
103 return (RCNEGATE(ESZEROL));
104 }
105
106 if (dmax > RSIZE_MAX_MEM16) {
107 invoke_safe_mem_constraint_handler("memcpy16_s: dmax exceeds max",
108 NULL, ESLEMAX);
109 return (RCNEGATE(ESLEMAX));
110 }
111
112 if (smax == 0) {
113 mem_prim_set16(dest, dmax, 0);
114 invoke_safe_mem_constraint_handler("memcpy16_s: smax is 0",
115 NULL, ESZEROL);
116 return (RCNEGATE(ESZEROL));
117 }
118
119 if (smax > dmax) {
120 mem_prim_set16(dest, dmax, 0);
121 invoke_safe_mem_constraint_handler("memcpy16_s: smax exceeds dmax",
122 NULL, ESLEMAX);
123 return (RCNEGATE(ESLEMAX));
124 }
125
126 if (src == NULL) {
127 mem_prim_set16(dest, dmax, 0);
128 invoke_safe_mem_constraint_handler("memcpy16_s: src is NULL",
129 NULL, ESNULLP);
130 return (RCNEGATE(ESNULLP));
131 }
132
133 /*
134 * overlap is undefined behavior, do not allow
135 */
136 if( ((dest > src) && (dest < (src+smax))) ||
137 ((src > dest) && (src < (dest+dmax))) ) {
138 mem_prim_set16(dest, dmax, 0);
139 invoke_safe_mem_constraint_handler("memcpy16_s: overlap undefined",
140 NULL, ESOVRLP);
141 return (RCNEGATE(ESOVRLP));
142 }
143
144 /*
145 * now perform the copy
146 */
147 mem_prim_move16(dest, src, smax);
148
149 return (RCNEGATE(EOK));
150 }
151 EXPORT_SYMBOL(memcpy16_s)