File Coverage

inc/matrixssl-3-9-3-open/core/memset_s.c
Criterion Covered Total %
statement 4 5 80.0
branch 1 2 50.0
condition n/a
subroutine n/a
pod n/a
total 5 7 71.4


line stmt bran cond sub pod time code
1             /**
2             * @file memset_s.c
3             * @version 950bba4 (HEAD -> master)
4             *
5             * Secure memset api that will not be optimized out by compiler.
6             */
7             /*
8             * Copyright (c) 2013-2017 INSIDE Secure Corporation
9             * Copyright (c) PeerSec Networks, 2002-2011
10             * All Rights Reserved
11             *
12             * The latest version of this code is available at http://www.matrixssl.org
13             *
14             * This software is open source; you can redistribute it and/or modify
15             * it under the terms of the GNU General Public License as published by
16             * the Free Software Foundation; either version 2 of the License, or
17             * (at your option) any later version.
18             *
19             * This General Public License does NOT permit incorporating this software
20             * into proprietary programs. If you are unable to comply with the GPL, a
21             * commercial license for this software may be purchased from INSIDE at
22             * http://www.insidesecure.com/
23             *
24             * This program is distributed in WITHOUT ANY WARRANTY; without even the
25             * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
26             * See the GNU General Public License for more details.
27             *
28             * You should have received a copy of the GNU General Public License
29             * along with this program; if not, write to the Free Software
30             * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31             * http://www.gnu.org/copyleft/gpl.html
32             */
33              
34             /******************************************************************************/
35             /**
36             Implementation of C11 API for platforms that do not have it.
37              
38             In contrast to the memset(3) function, calls to memset_s() will never be
39             "optimised away" by a compiler. This property is required by the follow-
40             ing sentences in section K.3.7.4.1 of ISO/IEC 9899:2011 ("ISO C11"):
41             Unlike memset(), any call to the memset_s() function shall be evalu-
42             ated strictly according to the rules of the abstract machine as
43             described in (5.1.2.3). That is, any call to the memset_s() function
44             shall assume that the memory indicated by s and n may be accessible
45             in the future and thus must contain the values indicated by c.
46              
47             On Mac OS X, this api is natively implemented.
48             On Windows, this api is mapped to SecureZeroMemory()
49             */
50             #if !defined(_WIN32) && !defined(__APPLE__)
51              
52             # ifdef __OPTIMIZE__
53             # pragma GCC diagnostic push
54             # pragma GCC diagnostic ignored "-Wpragmas"
55             # pragma GCC push_options
56             # pragma GCC optimize("O0")
57             # ifdef __clang__
58             # define NO_OPTIM \
59             __attribute__((__noinline__)) __attribute__((__optnone__))
60             # else
61             # define NO_OPTIM \
62             __attribute__((__noinline__)) __attribute__((__optimize__("O0")))
63             # endif
64             # else
65             # define NO_OPTIM /* Unrecognized compiler or not optimizing. */
66             # endif
67              
68             # include
69              
70             typedef size_t rsize_t;
71             typedef int errno_t;
72              
73 17264025           errno_t NO_OPTIM memset_s(void *s, rsize_t smax, int c, rsize_t n)
74             {
75 17264025 50         if (n > smax)
76             {
77 0           n = smax;
78             }
79 17264025           memset(s, c, n);
80 17264025           return ((unsigned char volatile *) s)[0];
81             }
82              
83             # ifdef __OPTIMIZE__
84             # pragma GCC pop_options
85             # pragma GCC diagnostic pop
86             # endif
87             # undef NO_OPTIM
88              
89             #endif /* !WIN && ! APPLE */
90              
91             /******************************************************************************/