aboutsummaryrefslogtreecommitdiff
path: root/src/memset_s.c
blob: d2e96e4d08102b53498cf609ae7e07094fada7be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* memset_s.c
 * SPDX-License-Identifier: BSD-3-Clause
 */

#define __STDC_WANT_LIB_EXT1__ 1
#include "extlib.h"
#include <errno.h>

errno_t
memset_s (void *s, rsize_t smax, int c, rsize_t n)
{
    volatile unsigned char *dest = (unsigned char *) s;
    errno_t ret = EINVAL;
    rsize_t limit = n < smax ? n : smax;

    if (s == NULL)
        throw_constraint_handler_s ("memset_s: s == NULL", ret);
    else if (n > RSIZE_MAX)
        throw_constraint_handler_s ("memset_s: n > RSIZE_MAX", ret);
    else if (smax > RSIZE_MAX)
        throw_constraint_handler_s ("memset_s: smax > RSIZE_MAX", ret);
    else if (n > smax)
        throw_constraint_handler_s ("memset_s: n > smax", ret);
    else
    {
        while (limit > 0)
            dest[--limit] = (unsigned char) c;
        ret = 0;
    }
    return ret;
}