Annotation of embedaddon/sudo/compat/memset_s.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (c) 2013 Todd C. Miller <Todd.Miller@courtesan.com>
        !             3:  *
        !             4:  * Permission to use, copy, modify, and distribute this software for any
        !             5:  * purpose with or without fee is hereby granted, provided that the above
        !             6:  * copyright notice and this permission notice appear in all copies.
        !             7:  *
        !             8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !             9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
        !            13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
        !            14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
        !            15:  */
        !            16: 
        !            17: #include <config.h>
        !            18: 
        !            19: #include <sys/types.h>
        !            20: #include <errno.h>
        !            21: #include <limits.h>
        !            22: #if defined(HAVE_STDINT_H)
        !            23: # include <stdint.h>
        !            24: #elif defined(HAVE_INTTYPES_H)
        !            25: # include <inttypes.h>
        !            26: #endif
        !            27: 
        !            28: #include "missing.h"
        !            29: 
        !            30: #ifndef RSIZE_MAX
        !            31: # if defined(SIZE_MAX)
        !            32: #  define RSIZE_MAX (SIZE_MAX >> 1)
        !            33: # elif defined(__LP64__)
        !            34: #  define RSIZE_MAX 0x7fffffffffffffffUL
        !            35: # else
        !            36: #  define RSIZE_MAX 0x7fffffffU
        !            37: # endif
        !            38: #endif
        !            39: 
        !            40: /*
        !            41:  * Simple implementation of C11 memset_s() function.
        !            42:  * We use a volatile pointer when updating the byte string.
        !            43:  * Most compilers will avoid optimizing away access to a
        !            44:  * volatile pointer, even if the pointer appears to be unused
        !            45:  * after the call.
        !            46:  *
        !            47:  * Note that C11 does not specify the return value on error, only
        !            48:  * that it be non-zero.  We use EINVAL for all errors.
        !            49:  */
        !            50: errno_t
        !            51: memset_s(void *v, rsize_t smax, int c, rsize_t n)
        !            52: {
        !            53:     errno_t ret = 0;
        !            54:     volatile unsigned char *s = v;
        !            55: 
        !            56:     /* Fatal runtime-constraint violations. */
        !            57:     if (s == NULL || smax > RSIZE_MAX) {
        !            58:        ret = errno = EINVAL;
        !            59:        goto done;
        !            60:     }
        !            61:     /* Non-fatal runtime-constraint violation, n must not exceed smax. */
        !            62:     if (n > smax) {
        !            63:        n = smax;
        !            64:        ret = errno = EINVAL;
        !            65:     }
        !            66:     /* Updating through a volatile pointer should not be optimized away. */
        !            67:     while (n--)
        !            68:        *s++ = (unsigned char)c;
        !            69: done:
        !            70:     return ret;
        !            71: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>