if (src[i] != dst[i])
return -1;
- /* Size fail */
- err = memcpy_s (dst + 1, sizeof (dst) - 1, src, sizeof (src));
+ /*
+ * Size test: sizeof (src) > sizeof (dst)
+ * Skip this test when __builtin_constant_p (sizeof (src)) is true.
+ * This is because memcpy_s_inline skips all the errors checking when the
+ * the above buildin function returns true which may cause overrun problem
+ * for dst buffer if this test is executed.
+ */
+ if (__builtin_constant_p (sizeof (src)) == 0)
+ {
+ err = memcpy_s (dst + 1, sizeof (dst) - 1, src, sizeof (src));
- if (err == EOK)
- return -1;
+ if (err == EOK)
+ return -1;
+ }
/* overlap fail */
err = memcpy_s (dst, sizeof (dst), dst + 1, sizeof (dst) - 1);
/* Null pointers comparison */
s = 0;
indicator = clib_strcmp (s, s);
- if (indicator != 0)
- return -1;
- /* verify it against strcmp */
- indicator = strcmp (s, s);
if (indicator != 0)
return -1;
v_indicator = strncmp (s1, "Every moment is a fresh beginning", s1len);
if (v_indicator != 0)
return -1;
- if (v_indicator != indicator)
- return -1;
/* s1 > s2, 0 is expected since comparison is no more than n character */
indicator = clib_strncmp (s1, "Every moment is a fresh begin",
sizeof ("Every moment is a fresh begin") - 1);
if (v_indicator != 0)
return -1;
- if (v_indicator != indicator)
- return -1;
/* s1 < s2, < 0 is expected */
indicator = clib_strncmp (s1, "Every moment is fresh beginning",
v_indicator = strncmp (s1, "Every moment is a fresh beginning", s1len + 1);
if (v_indicator != 0)
return -1;
- if (v_indicator != indicator)
- return -1;
/* unterminated s1 */
s1[s1len] = 0x1;
+ CLIB_MEM_UNPOISON (s1, CLIB_STRING_MACRO_MAX);
indicator = clib_strncmp (s1, "Every moment is a fresh beginning",
sizeof ("every moment is a fresh beginning") - 1);
if (indicator != 0)
sizeof ("Every moment is a fresh beginning") - 1);
if (v_indicator != 0)
return -1;
- if (v_indicator != indicator)
- return -1;
/* OK, seems to work */
return 0;
{
char src[] = "To err is human.";
char dst[64];
- char *dst_alias = dst;
int indicator;
size_t s1size = sizeof (dst); // including null
errno_t err;
return -1;
/* overlap fail */
- err = strcpy_s (dst, s1size, dst_alias);
+#if __GNUC__ < 8
+ /* GCC 8 flunks this one at compile time... */
+ err = strcpy_s (dst, s1size, dst);
if (err == EOK)
return -1;
+#endif
/* overlap fail */
err = strcpy_s (dst, s1size, dst + 1);
{
char src[] = "The journey of a one thousand miles begins with one step.";
char dst[100];
- char *dst_alias = dst;
int indicator;
errno_t err;
return -1;
/* verify it against strcpy */
- strcpy (dst, src);
+ strcpy (dst, src); //NOSONAR
/* This better not fail but check anyhow */
if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
return -1;
/* overlap fail */
- err = clib_strcpy (dst, dst_alias);
+#if __GNUC__ < 8
+ /* GCC 8 flunks this one at compile time... */
+ err = clib_strcpy (dst, dst);
if (err == EOK)
return -1;
+#endif
/* overlap fail */
err = clib_strcpy (dst, dst + 1);
{
char src[] = "Those who dare to fail miserably can achieve greatly.";
char dst[100], old_dst[100];
- char *dst_alias = dst;
- int indicator;
+ int indicator, i;
size_t s1size = sizeof (dst); // including null
errno_t err;
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;
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)));
return -1;
/* overlap fail */
- err = strncpy_s (dst, s1size, dst_alias, s1size);
+#if __GNUC__ < 8
+ /* GCC 8 flunks this one at compile time... */
+ err = strncpy_s (dst, s1size, dst, s1size);
if (err == EOK)
return -1;
+#endif
/* OK, seems to work */
return 0;
{
char src[] = "Those who dare to fail miserably can achieve greatly.";
char dst[100], old_dst[100];
- char *dst_alias = dst;
int indicator;
size_t s1size = sizeof (dst); // including null
errno_t err;
return -1;
/* Verify it against strncpy */
+#if __GNUC__ < 8
+ /* GCC 8 debian flunks this one at compile time */
strncpy (dst, src, strlen (src));
/* This better not fail but check anyhow */
return -1;
if (indicator != 0)
return -1;
+#endif
/* limited copy -- strlen src > n, copy up to n */
err = clib_strncpy (dst, "The price of greatness is responsibility.", 10);
/* verify it against strncpy */
memset_s (dst, sizeof (dst), 0, sizeof (dst));
+#if __GNUC__ < 8
+ /* GCC 8 flunks this one at compile time... */
strncpy (dst, "The price of greatness is responsibility.", 10);
if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), "The price ",
&indicator) != EOK)
return -1;
if (indicator != 0)
return -1;
+#endif
/* n > string len of src */
err = clib_strncpy (dst, src, clib_strnlen (src, sizeof (src)) + 10);
if (indicator != 0)
return -1;
/* Verify it against strncpy */
+#if __GNUC__ < 8
+ /* GCC 8 debian flunks this one at compile time */
strncpy (dst, src, strlen (src));
if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
EOK)
return -1;
if (indicator != 0)
return -1;
+#endif
/* zero length copy */
clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
return -1;
/* overlap fail */
- err = clib_strncpy (dst, dst + 1, s1size - 1);
+ err = clib_strncpy (dst, dst + 1, s1size);
if (err == EOK)
return -1;
/* overlap fail */
- err = clib_strncpy (dst, dst_alias, s1size);
+#if __GNUC__ < 8
+ /* GCC 8 flunks this one at compile time... */
+ err = clib_strncpy (dst, dst, s1size);
if (err == EOK)
return -1;
+#endif
/* OK, seems to work */
return 0;
test_strcat_s (vlib_main_t * vm, unformat_input_t * input)
{
char src[100], dst[100], old_dst[100];
- char *dst_alias = dst;
size_t s1size = sizeof (dst); // including null
errno_t err;
int indicator;
return -1;
/* overlap fail */
- err = strcat_s (dst, s1size, dst_alias);
+#if __GNUC__ < 8
+ /* GCC 8 flunks this one at compile time... */
+ err = strcat_s (dst, s1size, dst);
if (err != EINVAL)
return -1;
+#endif
/* not enough space for dst */
err = strcat_s (dst, 10, src);
test_clib_strcat (vlib_main_t * vm, unformat_input_t * input)
{
char src[100], dst[100], old_dst[100];
- char *dst_alias = dst;
size_t s1size = sizeof (dst); // including null
errno_t err;
int indicator;
return -1;
/* overlap fail */
- err = clib_strcat (dst, dst_alias);
+#if __GNUC__ < 8
+ /* GCC 8 flunks this one at compile time... */
+ err = clib_strcat (dst, dst);
if (err != EINVAL)
return -1;
+#endif
/* OK, seems to work */
return 0;
test_strncat_s (vlib_main_t * vm, unformat_input_t * input)
{
char src[100], dst[100], old_dst[100];
- char *dst_alias = dst;
size_t s1size = sizeof (dst); // including null
errno_t err;
char s1[] = "Two things are infinite: ";
if (indicator != 0)
return -1;
/* verify it against strncat */
+#if __GNUC__ < 8
+ /* GCC 8 debian flunks this one at compile time */
strcpy_s (dst, sizeof (dst), s1);
strncat (dst, s2, 13);
if (strcmp_s (dst, s1size - 1, "Two things are infinite: the universe ",
return -1;
if (indicator != 0)
return -1;
+#endif
/* negative stuff */
err = strncat_s (0, 0, 0, 1);
return -1;
/* overlap fail */
- err = strncat_s (dst, s1size, dst_alias, clib_strnlen (dst, sizeof (dst)));
+#if __GNUC__ < 8
+ /* GCC 8 flunks this one at compile time... */
+ err = strncat_s (dst, s1size, dst, clib_strnlen (dst, sizeof (dst)));
if (err != EINVAL)
return -1;
+#endif
/* OK, seems to work */
return 0;
test_clib_strncat (vlib_main_t * vm, unformat_input_t * input)
{
char src[100], dst[100], old_dst[100];
- char *dst_alias = dst;
size_t s1size = sizeof (dst); // including null
errno_t err;
char s1[] = "Two things are infinite: ";
if (indicator != 0)
return -1;
/* verify it against strncat */
+#if __GNUC__ < 8
+ /* GCC 8 debian flunks this one at compile time */
strcpy_s (dst, sizeof (dst), s1);
strncat (dst, s2, 13);
if (strcmp_s (dst, s1size - 1, "Two things are infinite: the universe ",
return -1;
if (indicator != 0)
return -1;
+#endif
/* negative stuff */
err = clib_strncat (0, 0, 1);
return -1;
/* overlap fail */
- err = clib_strncat (dst, dst_alias, clib_strnlen (dst, sizeof (dst)));
+#if __GNUC__ < 8
+ /* GCC 8 flunks this one at compile time... */
+ err = clib_strncat (dst, dst, clib_strnlen (dst, sizeof (dst)));
if (err != EINVAL)
return -1;
+#endif
/* OK, seems to work */
return 0;
test_clib_strtok (vlib_main_t * vm, unformat_input_t * input)
{
int indicator;
- char *s1, *s1_alias, *tok, *ptr;
+ char *s1 __attribute__ ((unused));
+ char *tok __attribute__ ((unused));
+ char *ptr __attribute__ ((unused));
char str1[40];
char *p2str;
char *tok1, *tok2, *tok3, *tok4, *tok5, *tok6, *tok7;
/* negative stuff */
s1 = 0;
ptr = 0;
- s1_alias = s1;
- tok = clib_strtok (s1, s1_alias, (char **) 0);
+#if __GNUC__ < 8
+ /* GCC 8 flunks this one at compile time... */
+ tok = clib_strtok (s1, s1, (char **) 0);
if (tok != 0)
return -1;
/* s1 and ptr contents are null */
- s1_alias = s1;
- tok = clib_strtok (s1, s1_alias, &ptr);
+ tok = clib_strtok (s1, s1, &ptr);
if (tok != 0)
return -1;
+#endif
/* verify it against strtok_r */
/* No can do. This causes a crash in strtok_r */
/* unterminated s1 and s2 */
memset_s (s1, ARRAY_LEN (s1), 0xfe, ARRAY_LEN (s1));
+ CLIB_MEM_UNPOISON (s1, CLIB_STRING_MACRO_MAX);
sub = clib_strstr (s1, s1);
if (sub == 0)
return -1;
return 0;
}
+static int
+test_clib_count_equal (vlib_main_t * vm, unformat_input_t * input)
+{
+ u64 s64[15];
+ u32 s32[31];
+ u16 s16[63];
+ u8 s8[127];
+ uword count;
+
+ vlib_cli_output (vm, "Test clib_count_equal_u64...");
+ memset (s64, 0, sizeof (s64));
+ count = clib_count_equal_u64 (s64, 0);
+ if (0 != count)
+ return -1;
+ count = clib_count_equal_u64 (s64, 1);
+ if (1 != count)
+ return -1;
+ count = clib_count_equal_u64 (s64, 3);
+ if (3 != count)
+ return -1;
+ count = clib_count_equal_u64 (s64, 15);
+ if (15 != count)
+ return -1;
+ s64[10] = 0xcafe;
+ count = clib_count_equal_u64 (s64, 13);
+ if (10 != count)
+ return -1;
+ s64[10] = 0;
+
+ vlib_cli_output (vm, "Test clib_count_equal_u32...");
+ memset (s32, 0, sizeof (s32));
+ count = clib_count_equal_u32 (s32, 0);
+ if (0 != count)
+ return -1;
+ count = clib_count_equal_u32 (s32, 1);
+ if (1 != count)
+ return -1;
+ count = clib_count_equal_u32 (s32, 3);
+ if (3 != count)
+ return -1;
+ count = clib_count_equal_u32 (s32, 31);
+ if (31 != count)
+ return -1;
+ s32[10] = 0xcafe;
+ count = clib_count_equal_u32 (s32, 13);
+ if (10 != count)
+ return -1;
+ s32[10] = 0;
+
+ vlib_cli_output (vm, "Test clib_count_equal_u16...");
+ memset (s16, 0, sizeof (s16));
+ count = clib_count_equal_u16 (s16, 0);
+ if (0 != count)
+ return -1;
+ count = clib_count_equal_u16 (s16, 1);
+ if (1 != count)
+ return -1;
+ count = clib_count_equal_u16 (s16, 3);
+ if (3 != count)
+ return -1;
+ count = clib_count_equal_u16 (s16, 63);
+ if (63 != count)
+ return -1;
+ s16[10] = 0xcafe;
+ count = clib_count_equal_u16 (s16, 13);
+ if (10 != count)
+ return -1;
+ s16[10] = 0;
+
+ vlib_cli_output (vm, "Test clib_count_equal_u8...");
+ memset (s8, 0, sizeof (s8));
+ count = clib_count_equal_u8 (s8, 0);
+ if (0 != count)
+ return -1;
+ count = clib_count_equal_u8 (s8, 1);
+ if (1 != count)
+ return -1;
+ count = clib_count_equal_u8 (s8, 3);
+ if (3 != count)
+ return -1;
+ count = clib_count_equal_u8 (s8, 127);
+ if (127 != count)
+ return -1;
+ s8[10] = 0xfe;
+ count = clib_count_equal_u8 (s8, 13);
+ if (10 != count)
+ return -1;
+ s8[10] = 0;
+
+ return 0;
+}
+
+
#define foreach_string_test \
_ (0, MEMCPY_S, "memcpy_s", memcpy_s) \
_ (1, CLIB_MEMCPY, "clib_memcpy", clib_memcpy) \
_ (20, STRNLEN_S, "strnlen_s", strnlen_s) \
_ (21, CLIB_STRNLEN, "clib_strnlen", clib_strnlen) \
_ (22, STRSTR_S, "strstr_s", strstr_s) \
- _ (23, CLIB_STRSTR, "clib_strstr", clib_strstr)
+ _ (23, CLIB_STRSTR, "clib_strstr", clib_strstr) \
+ _ (24, CLIB_COUNT_EQUAL, "clib_count_equal", clib_count_equal)
typedef enum
{
#define _(v,f,s,p) STRING_TEST_##f = v,
foreach_string_test
#undef _
+#define STRING_TEST_FIRST STRING_TEST_MEMCPY_S
+#define STRING_TEST_LAST STRING_TEST_CLIB_COUNT_EQUAL
} string_test_t;
static uword
if (specific_test == ~0)
{
- for (specific_test = STRING_TEST_MEMCPY_S;
- specific_test <= STRING_TEST_CLIB_STRSTR; specific_test++)
+ for (specific_test = STRING_TEST_FIRST;
+ specific_test <= STRING_TEST_LAST; specific_test++)
{
ok = (string_func[specific_test]).test (vm, input);
res += ok;
"strncmp_s | clib_strncmp | strcpy_s | clib_strcpy | strncpy_s | "
"clib_strncpy | strcat_s | clib_strcat | strncat_s | clib_strncat | "
"strtok_s | clib_strtok | strnlen_s | clib_strnlen | strstr_s | "
- "clib_strstr]",
+ "clib_strstr | clib_count_equal ]",
.function = string_test_command_fn,
};
/* *INDENT-ON* */