diff --git a/lib/libc/stdlib/set_constraint_handler_s.c b/lib/libc/stdlib/set_constraint_handler_s.c index dafdb16528b..5e13f0f9788 100644 --- a/lib/libc/stdlib/set_constraint_handler_s.c +++ b/lib/libc/stdlib/set_constraint_handler_s.c @@ -32,6 +32,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include "un-namespace.h" #include "libc_private.h" @@ -81,10 +82,11 @@ __throw_constraint_handler_s(const char * restrict msg, errno_t error) } void -abort_handler_s(const char * restrict msg __unused, - void * restrict ptr __unused, errno_t error __unused) +abort_handler_s(const char * restrict msg, void * restrict ptr __unused, + errno_t error __unused) { + (void) fprintf(stderr, "abort_handler_s : %s\n", msg); abort(); } diff --git a/lib/libc/string/memset_s.c b/lib/libc/string/memset_s.c index 9bb8e1cd5b9..859c8f130a5 100644 --- a/lib/libc/string/memset_s.c +++ b/lib/libc/string/memset_s.c @@ -42,22 +42,25 @@ memset_s(void *s, rsize_t smax, int c, rsize_t n) volatile unsigned char *dst; ret = EINVAL; - lim = smax; + lim = (n < smax) ? n : smax; v = (unsigned char)c; dst = (unsigned char *)s; if (s == NULL) { __throw_constraint_handler_s("memset_s : s is NULL", ret); } else if (smax > RSIZE_MAX) { __throw_constraint_handler_s("memset_s : smax > RSIZE_MAX", - ret); + ret); } else if (n > RSIZE_MAX) { __throw_constraint_handler_s("memset_s : n > RSIZE_MAX", ret); } else { - if (n < smax) - lim = n; while (lim > 0) dst[--lim] = v; - ret = 0; + if (n > smax) { + __throw_constraint_handler_s("memset_s : n > smax", + ret); + } else { + ret = 0; + } } return (ret); } diff --git a/lib/libc/tests/string/memset_s_test.c b/lib/libc/tests/string/memset_s_test.c index a8426eb3c78..e0cfd867ff2 100644 --- a/lib/libc/tests/string/memset_s_test.c +++ b/lib/libc/tests/string/memset_s_test.c @@ -109,13 +109,18 @@ ATF_TC_BODY(n_lt_smax, tc) assert(b[2] == 3); } -/* n > smax */ +/* n > smax, handler */ ATF_TC_WITHOUT_HEAD(n_gt_smax); ATF_TC_BODY(n_gt_smax, tc) { char b[3] = {1, 2, 3}; - assert(memset_s(&b[0], 1, 9, 3) == 0); + e = 0; + m = NULL; + set_constraint_handler_s(h); + assert(memset_s(&b[0], 1, 9, 3) != 0); + assert(e > 0); + assert(strcmp(m, "memset_s : n > smax") == 0); assert(b[0] == 9); assert(b[1] == 2); assert(b[2] == 3);