From f09179f91aea0ca0b427a5097c0932dc64589fe0 Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 7 Jan 2019 20:32:01 -0800 Subject: strncpy_s_inline copies more bytes than necessary Given n equals to the maximum number of bytes to copy from src in the API, or the rough estimate strlen of src, strncpy_s_inline should not copy more than the number of bytes, computed by strlen(src), to dst if n is greater than strlen(src). The number of bytes to copy is computed by strnlen(src,n), not n. Change-Id: I088b46125d9776962750e121f1fbf441952efc2b Signed-off-by: Steven --- src/plugins/unittest/string_test.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/plugins') diff --git a/src/plugins/unittest/string_test.c b/src/plugins/unittest/string_test.c index 0d41bb27918..41b4c61dff4 100644 --- a/src/plugins/unittest/string_test.c +++ b/src/plugins/unittest/string_test.c @@ -628,7 +628,7 @@ test_strncpy_s (vlib_main_t * vm, unformat_input_t * input) { char src[] = "Those who dare to fail miserably can achieve greatly."; char dst[100], old_dst[100]; - int indicator; + int indicator, i; size_t s1size = sizeof (dst); // including null errno_t err; @@ -658,6 +658,10 @@ test_strncpy_s (vlib_main_t * vm, unformat_input_t * input) return -1; /* n > string len of src */ + err = clib_memset (dst, 1, sizeof (dst)); + if (err != EOK) + return -1; + err = strncpy_s (dst, s1size, src, clib_strnlen (src, sizeof (src)) + 10); if (err != EOK) return -1; @@ -667,6 +671,11 @@ test_strncpy_s (vlib_main_t * vm, unformat_input_t * input) if (indicator != 0) return -1; + /* Make sure bytes after strlen(dst) is untouched */ + for (i = 1 + clib_strnlen (dst, sizeof (dst)); i < sizeof (dst); i++) + if (dst[i] != 1) + return -1; + /* truncation, n >= dmax */ err = strncpy_s (dst, clib_strnlen (src, sizeof (src)), src, clib_strnlen (src, sizeof (src))); -- cgit 1.2.3-korg