safec  2.1
Safe C Library - ISO TR24731 Bounds Checking Interface
memset32_s.c
Go to the documentation of this file.
1 /*------------------------------------------------------------------
2  * memset32_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 
72 errno_t
73 memset32_s(uint32_t *dest, rsize_t smax, uint32_t value, rsize_t n)
74 {
75  errno_t err = EOK;
76 
77  if (dest == NULL) {
78  invoke_safe_mem_constraint_handler("memset32_s: dest is null",
79  NULL, ESNULLP);
80  return (RCNEGATE(ESNULLP));
81  }
82 
83  if (n == 0) {
84  invoke_safe_mem_constraint_handler("memset32_s: n is 0",
85  NULL, ESZEROL);
86  return (RCNEGATE(ESZEROL));
87  }
88 
89  if (smax > RSIZE_MAX_MEM) {
90  invoke_safe_mem_constraint_handler("memset32_s: smax exceeds max",
91  NULL, ESLEMAX);
92  return (RCNEGATE(ESLEMAX));
93  }
94 
95  if (n > RSIZE_MAX_MEM16) {
96  invoke_safe_mem_constraint_handler("memset32_s: n exceeds max",
97  NULL, ESLEMAX);
98  err = ESLEMAX;
99  n = smax/4;
100  }
101 
102  if (n > smax/4) {
103  invoke_safe_mem_constraint_handler("memset32_s: n exceeds smax/4",
104  NULL, ESNOSPC);
105  err = ESNOSPC;
106  n = smax/4;
107  }
108 
109  mem_prim_set32(dest, n, value);
110 
111  return (RCNEGATE(err));
112 }
113 EXPORT_SYMBOL(memset32_s)
void mem_prim_set32(uint32_t *dest, uint32_t len, uint32_t value)
Sets len uint32_t's starting at dest to the specified value.
errno_t memset32_s(uint32_t *dest, rsize_t smax, uint32_t value, rsize_t n)
Sets len uint32_t starting at dest to the specified value.
Definition: memset32_s.c:73
void invoke_safe_mem_constraint_handler(const char *msg, void *ptr, errno_t error)
Invokes the currently set constraint handler or the default.