safec  2.1
Safe C Library - ISO TR24731 Bounds Checking Interface
memset_s.c
Go to the documentation of this file.
1 /*------------------------------------------------------------------
2  * memset_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 
83 #if !(defined(__STDC_WANT_LIB_EXT1__) && (__STDC_WANT_LIB_EXT1__ >= 1))
84 errno_t
85 memset_s (void *dest, rsize_t dmax, uint8_t value, rsize_t n)
86 {
87  errno_t err = EOK;
88 
89  if (dest == NULL) {
90  invoke_safe_mem_constraint_handler("memset_s: dest is null",
91  NULL, ESNULLP);
92  return (RCNEGATE(ESNULLP));
93  }
94 
95  /* different on C11! */
96  if (n == 0) {
97  invoke_safe_mem_constraint_handler("memset_s: n is 0",
98  NULL, ESZEROL);
99  return (RCNEGATE(ESZEROL));
100  }
101 
102  if (dmax > RSIZE_MAX_MEM) {
103  invoke_safe_mem_constraint_handler("memset_s: dmax exceeds max",
104  NULL, ESLEMAX);
105  return (RCNEGATE(ESLEMAX));
106  }
107 
108  if (n > RSIZE_MAX_MEM) {
109  invoke_safe_mem_constraint_handler("memset_s: n exceeds max",
110  NULL, ESLEMAX);
111  err = ESLEMAX;
112  n = dmax;
113  }
114 
115  if (n > dmax) {
116  invoke_safe_mem_constraint_handler("memset_s: n exceeds dmax",
117  NULL, ESNOSPC);
118  err = ESNOSPC;
119  n = dmax;
120  }
121 
122  mem_prim_set(dest, n, value);
123 
124  return (RCNEGATE(err));
125 }
126 EXPORT_SYMBOL(memset_s)
127 #endif
void mem_prim_set(void *dest, uint32_t len, uint8_t value)
Sets len bytes starting at dest to the specified value.
errno_t memset_s(void *dest, rsize_t dmax, uint8_t value, rsize_t n)
Sets the first n bytes starting at dest to the specified value, but maximal dmax bytes.
Definition: memset_s.c:85
void invoke_safe_mem_constraint_handler(const char *msg, void *ptr, errno_t error)
Invokes the currently set constraint handler or the default.