Tweak memset_s() and its unit tests, mostly renaming variables.

This commit is contained in:
Dag-Erling Smørgrav 2015-12-16 00:41:26 +01:00
parent 1a86caaf37
commit bf624acec1
2 changed files with 24 additions and 17 deletions

View file

@ -32,9 +32,11 @@
#endif #endif
#include <errno.h> #include <errno.h>
#include <limits.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <cryb/coverage.h>
#include <cryb/memset_s.h> #include <cryb/memset_s.h>
/* /*
@ -42,15 +44,19 @@
* is overwritten even if the data will never be read. * is overwritten even if the data will never be read.
*/ */
int int
cryb_memset_s(void *s, size_t smax, int c, size_t n) cryb_memset_s(void *d, size_t dsz, int ch, size_t n)
{ {
unsigned int i; unsigned int i;
if (s == NULL) if (d == NULL)
return (EINVAL); return (EINVAL);
for (i = 0; i < n && i < smax; ++i) CRYB_DISABLE_COVERAGE
((volatile unsigned char *)s)[i] = (unsigned char)c; if (dsz > SIZE_MAX || n > SIZE_MAX)
if (n > smax) return (ERANGE);
CRYB_RESTORE_COVERAGE
for (i = 0; i < n && i < dsz; ++i)
((volatile unsigned char *)d)[i] = (unsigned char)ch;
if (n > dsz)
return (EOVERFLOW); return (EOVERFLOW);
return (0); return (0);
} }

View file

@ -44,8 +44,8 @@
struct t_case { struct t_case {
const char *desc; const char *desc;
const char in[T_BUF_LEN]; const char in[T_BUF_LEN];
size_t smax; size_t dsz;
int c; int ch;
size_t len; size_t len;
const char out[T_BUF_LEN]; const char out[T_BUF_LEN];
size_t outlen; size_t outlen;
@ -59,8 +59,8 @@ static struct t_case t_cases[] = {
{ {
.desc = "zero", .desc = "zero",
.in = "squeamish ossifrage", .in = "squeamish ossifrage",
.smax = sizeof "squeamish ossifrage" - 1, .dsz = sizeof "squeamish ossifrage" - 1,
.c = 'x', .ch = 'x',
.len = 0, .len = 0,
.out = "squeamish ossifrage", .out = "squeamish ossifrage",
.ret = 0, .ret = 0,
@ -68,8 +68,8 @@ static struct t_case t_cases[] = {
{ {
.desc = "short", .desc = "short",
.in = "squeamish ossifrage", .in = "squeamish ossifrage",
.smax = sizeof "squeamish ossifrage" - 1, .dsz = sizeof "squeamish ossifrage" - 1,
.c = 'x', .ch = 'x',
.len = 9, .len = 9,
.out = "xxxxxxxxx ossifrage", .out = "xxxxxxxxx ossifrage",
.ret = 0, .ret = 0,
@ -77,8 +77,8 @@ static struct t_case t_cases[] = {
{ {
.desc = "exact", .desc = "exact",
.in = "squeamish ossifrage", .in = "squeamish ossifrage",
.smax = sizeof "squeamish ossifrage" - 1, .dsz = sizeof "squeamish ossifrage" - 1,
.c = 'x', .ch = 'x',
.len = sizeof "squeamish ossifrage" - 1, .len = sizeof "squeamish ossifrage" - 1,
.out = "xxxxxxxxxxxxxxxxxxx", .out = "xxxxxxxxxxxxxxxxxxx",
.ret = 0, .ret = 0,
@ -86,8 +86,8 @@ static struct t_case t_cases[] = {
{ {
.desc = "long", .desc = "long",
.in = "squeamish ossifrage", .in = "squeamish ossifrage",
.smax = sizeof "squeamish ossifrage" - 1, .dsz = sizeof "squeamish ossifrage" - 1,
.c = 'x', .ch = 'x',
.len = sizeof "squeamish ossifrage" + 1, .len = sizeof "squeamish ossifrage" + 1,
.out = "xxxxxxxxxxxxxxxxxxx", .out = "xxxxxxxxxxxxxxxxxxx",
.ret = EOVERFLOW, .ret = EOVERFLOW,
@ -104,8 +104,9 @@ t_memset_s(char **desc CRYB_UNUSED, void *arg)
char buf[T_BUF_LEN]; char buf[T_BUF_LEN];
int ret; int ret;
memcpy(buf, t->in, T_BUF_LEN); memset(buf, 0, sizeof buf);
ret = memset_s(buf, t->smax, t->c, t->len); strncpy(buf, t->in, sizeof buf);
ret = memset_s(buf, t->dsz, t->ch, t->len);
return (t_compare_i(t->ret, ret) & return (t_compare_i(t->ret, ret) &
t_compare_mem(t->out, buf, T_BUF_LEN)); t_compare_mem(t->out, buf, T_BUF_LEN));
} }