safec  2.1
Safe C Library - ISO TR24731 Bounds Checking Interface
memset16_s.c
Go to the documentation of this file.
1 /*------------------------------------------------------------------
2  * memset16_s
3  *
4  * October 2008, Bo Berry
5  * Copyright (c) 2017 Reini Urban
6  *
7  * Copyright (c) 2008-2011 Cisco Systems
8  * All rights reserved.
9  *
10  * Permission is hereby granted, free of charge, to any person
11  * obtaining a copy of this software and associated documentation
12  * files (the "Software"), to deal in the Software without
13  * restriction, including without limitation the rights to use,
14  * copy, modify, merge, publish, distribute, sublicense, and/or
15  * sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following
17  * conditions:
18  *
19  * The above copyright notice and this permission notice shall be
20  * included in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
24  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
27  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29  * OTHER DEALINGS IN THE SOFTWARE.
30  *------------------------------------------------------------------
31  */
32 
33 #include "safeclib_private.h"
34 #include "safe_mem_constraint.h"
35 #include "mem_primitives_lib.h"
36 #include "safe_mem_lib.h"
37 
38 
73 errno_t
74 memset16_s(uint16_t *dest, rsize_t smax, uint16_t value, rsize_t n)
75 {
76  errno_t err = EOK;
77 
78  if (dest == NULL) {
79  invoke_safe_mem_constraint_handler("memset16_s: dest is null",
80  NULL, ESNULLP);
81  return (RCNEGATE(ESNULLP));
82  }
83 
84  if (n == 0) {
85  invoke_safe_mem_constraint_handler("memset16_s: n is 0",
86  NULL, ESZEROL);
87  return (RCNEGATE(ESZEROL));
88  }
89 
90  if (smax > RSIZE_MAX_MEM) {
91  invoke_safe_mem_constraint_handler("memset16_s: smax exceeds max",
92  NULL, ESLEMAX);
93  return (RCNEGATE(ESLEMAX));
94  }
95 
96  if (n > RSIZE_MAX_MEM16) {
97  invoke_safe_mem_constraint_handler("memset16_s: n exceeds max",
98  NULL, ESLEMAX);
99  err = ESLEMAX;
100  n = smax/2;
101  }
102 
103  if (n > smax/2) {
104  invoke_safe_mem_constraint_handler("memset16_s: n exceeds smax/2",
105  NULL, ESNOSPC);
106  err = ESNOSPC;
107  n = smax/2;
108  }
109 
110  mem_prim_set16(dest, n, value);
111 
112  return (RCNEGATE(err));
113 }
114 EXPORT_SYMBOL(memset16_s)
errno_t memset16_s(uint16_t *dest, rsize_t smax, uint16_t value, rsize_t n)
Sets the first n uint16_t values starting at dest to the specified value, but maximal smax bytes...
Definition: memset16_s.c:74
void mem_prim_set16(uint16_t *dest, uint32_t len, uint16_t value)
Sets len uint16_t's starting at dest to the specified value.
void invoke_safe_mem_constraint_handler(const char *msg, void *ptr, errno_t error)
Invokes the currently set constraint handler or the default.