From d01120230288d6a3e64f664554a1963296834a05 Mon Sep 17 00:00:00 2001 From: Mark Rustad Date: Sun, 24 Nov 2019 23:17:19 -0800 Subject: [PATCH 01/18] safestringlib: no static build and no relro Do not build the test program as static. Static programs are not supported on some modern distros. relro is also rejected, so drop it. Signed-off-by: Mark Rustad --- makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/makefile b/makefile index f7cc268..8a417c6 100644 --- a/makefile +++ b/makefile @@ -2,7 +2,7 @@ IDIR = include MKDIR_P = mkdir -p CC=gcc CFLAGS=-I$(IDIR) -fstack-protector-strong -fPIE -fPIC -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -LDFLAGS=-z noexecstack -z relo -z now +LDFLAGS=-z noexecstack -z now ODIR=obj OTDIR=objtest @@ -48,7 +48,7 @@ $(OTDIR)/%.o: $(TESTDIR)/%.c $(TESTDIR)/test_private.h safestringtest: directories libsafestring.a $(TOBJ) - $(CC) $(LDFLAGS) -static -o $@ $(TOBJ) libsafestring.a + $(CC) $(LDFLAGS) -o $@ $(TOBJ) libsafestring.a .PHONY: directories From eea1afe912f2e9dd1daa79d387e8c960f5ad6c69 Mon Sep 17 00:00:00 2001 From: Mark Rustad Date: Sun, 24 Nov 2019 20:27:42 -0800 Subject: [PATCH 02/18] safestringlib: Resolve gcc9 null string warnings Gcc9 complains about passing null string pointers, so resolve the obvious cases that the compiler reports by passing "" instead of the null pointer. Signed-off-by: Mark Rustad --- unittests/test_strtok_s.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/unittests/test_strtok_s.c b/unittests/test_strtok_s.c index 8cba76f..e1fbf4b 100644 --- a/unittests/test_strtok_s.c +++ b/unittests/test_strtok_s.c @@ -114,7 +114,7 @@ int test_strtok_s() p2tok = strtok_s(str1, &len, str2, &p2str); if (p2tok == NULL) { printf("%s %u token -%s- remaining -%s- \n", - __FUNCTION__, __LINE__, p2tok, p2str); + __FUNCTION__, __LINE__, "", p2str); } /*--------------------------------------------------*/ @@ -129,7 +129,7 @@ int test_strtok_s() p2tok = strtok_s(str1, &len, str2, &p2str); if (p2tok == NULL) { printf("%s %u token -%s- remaining -%s- \n", - __FUNCTION__, __LINE__, p2tok, p2str); + __FUNCTION__, __LINE__, "", p2str); } if (strcmp(p2tok, "mnopqrst")) { From ea8c58e9671167884c87d1785a003da077f89ee9 Mon Sep 17 00:00:00 2001 From: Mark Rustad Date: Sun, 6 May 2018 21:46:21 -0700 Subject: [PATCH 03/18] safestring: Always print test failures Many test failures only print when DEBUG is defined. This is crazy. Always print test failures. Also count test failures and return the failures count instead of uselessly returning 0 from each test routine. That allows the test to stop and clearly report the presence of errors. Signed-off-by: Mark Rustad --- unittests/Safe_String_UnitTestMain.c | 163 ++++++++++++++------------ unittests/test_memcmp16_s.c | 101 +++++++++------- unittests/test_memcmp32_s.c | 95 +++++++++------ unittests/test_memcmp_s.c | 113 +++++++++++------- unittests/test_memcpy16_s.c | 88 +++++++++----- unittests/test_memcpy32_s.c | 88 +++++++++----- unittests/test_memcpy_s.c | 100 ++++++++++------ unittests/test_memmove16_s.c | 65 ++++++----- unittests/test_memmove32_s.c | 76 +++++++----- unittests/test_memmove_s.c | 74 +++++++----- unittests/test_memset16_s.c | 45 ++++--- unittests/test_memset32_s.c | 45 ++++--- unittests/test_memset_s.c | 43 ++++--- unittests/test_memzero16_s.c | 44 ++++--- unittests/test_memzero32_s.c | 44 ++++--- unittests/test_memzero_s.c | 44 ++++--- unittests/test_stpcpy_s.c | 50 +++++++- unittests/test_stpncpy_s.c | 64 +++++++++- unittests/test_strcasecmp_s.c | 32 ++++- unittests/test_strcasestr_s.c | 52 ++++++++- unittests/test_strcat_s.c | 168 ++++++++++++++++----------- unittests/test_strcmp_s.c | 32 ++++- unittests/test_strcmpfld_s.c | 110 +++++++++++------- unittests/test_strcpy_s.c | 138 +++++++++++++--------- unittests/test_strcpyfld_s.c | 90 ++++++++------ unittests/test_strcpyfldin_s.c | 120 +++++++++++-------- unittests/test_strcpyfldout_s.c | 118 +++++++++++-------- unittests/test_strcspn_s.c | 35 +++++- unittests/test_strfirstchar_s.c | 22 +++- unittests/test_strfirstdiff_s.c | 32 ++++- unittests/test_strfirstsame_s.c | 34 +++++- unittests/test_strisalphanumeric_s.c | 16 ++- unittests/test_strisascii_s.c | 15 ++- unittests/test_strisdigit_s.c | 14 ++- unittests/test_strishex_s.c | 16 ++- unittests/test_strislowercase_s.c | 13 ++- unittests/test_strismixed_s.c | 14 ++- unittests/test_strispassword_s.c | 16 ++- unittests/test_strisuppercase_s.c | 14 ++- unittests/test_strlastchar_s.c | 24 +++- unittests/test_strlastdiff_s.c | 28 ++++- unittests/test_strlastsame_s.c | 34 +++++- unittests/test_strljustify_s.c | 29 ++++- unittests/test_strncat_s.c | 118 +++++++++++-------- unittests/test_strncpy_s.c | 168 ++++++++++++++++----------- unittests/test_strnlen_s.c | 13 ++- unittests/test_strnterminate_s.c | 19 ++- unittests/test_strpbrk_s.c | 36 +++++- unittests/test_strprefix_s.c | 18 ++- unittests/test_strremovews_s.c | 28 ++++- unittests/test_strspn_s.c | 36 +++++- unittests/test_strstr_s.c | 39 ++++++- unittests/test_strtok_s.c | 19 ++- unittests/test_strtolowercase_s.c | 19 ++- unittests/test_strtouppercase_s.c | 18 ++- unittests/test_wcpcpy_s.c | 54 ++++++++- unittests/test_wcscat_s.c | 40 ++++++- unittests/test_wcscpy_s.c | 37 +++++- unittests/test_wcsncat_s.c | 44 ++++++- unittests/test_wcsncpy_s.c | 49 +++++++- unittests/test_wcsnlen_s.c | 10 +- unittests/test_wmemcmp_s.c | 33 +++++- unittests/test_wmemcpy_s.c | 39 ++++++- unittests/test_wmemmove_s.c | 43 ++++++- unittests/test_wmemset_s.c | 19 ++- 65 files changed, 2503 insertions(+), 954 deletions(-) diff --git a/unittests/Safe_String_UnitTestMain.c b/unittests/Safe_String_UnitTestMain.c index fa40511..04fceae 100644 --- a/unittests/Safe_String_UnitTestMain.c +++ b/unittests/Safe_String_UnitTestMain.c @@ -79,6 +79,8 @@ extern int test_wmemmove_s(void); extern int test_wmemset_s(void); extern int test_wmemcmp_s(void); +#define TEST_FUNC(f) { #f, f } + int main(void) { char dest[128]; @@ -86,82 +88,90 @@ int main(void) { rsize_t dmax = 128; rsize_t smax = 21; errno_t err; + static const struct { + const char * const name; + int (*func)(void); + } funcs[] = { + TEST_FUNC(test_memcmp_s), + TEST_FUNC(test_memcmp16_s), + TEST_FUNC(test_memcmp32_s), + TEST_FUNC(test_memcpy_s), + TEST_FUNC(test_memcpy16_s), + TEST_FUNC(test_memcpy32_s), + TEST_FUNC(test_memmove_s), + TEST_FUNC(test_memmove16_s), + TEST_FUNC(test_memmove32_s), + TEST_FUNC(test_memset_s), + TEST_FUNC(test_memset16_s), + TEST_FUNC(test_memset32_s), + TEST_FUNC(test_memzero_s), + TEST_FUNC(test_memzero16_s), + TEST_FUNC(test_memzero32_s), + TEST_FUNC(test_stpcpy_s), + TEST_FUNC(test_stpncpy_s), + TEST_FUNC(test_strcasecmp_s), + TEST_FUNC(test_strcasestr_s), + TEST_FUNC(test_strcat_s), + TEST_FUNC(test_strcmp_s), + TEST_FUNC(test_strcmpfld_s), + TEST_FUNC(test_strcpy_s), + TEST_FUNC(test_strcpyfld_s), + TEST_FUNC(test_strcpyfldin_s), + TEST_FUNC(test_strcpyfldout_s), + TEST_FUNC(test_strcspn_s), + TEST_FUNC(test_strfirstchar_s), + TEST_FUNC(test_strfirstdiff_s), + TEST_FUNC(test_strfirstsame_s), + TEST_FUNC(test_strisalphanumeric_s), + TEST_FUNC(test_strisascii_s), + TEST_FUNC(test_strisdigit_s), + TEST_FUNC(test_strishex_s), + TEST_FUNC(test_strislowercase_s), + TEST_FUNC(test_strismixed_s), + TEST_FUNC(test_strispassword_s), + TEST_FUNC(test_strisuppercase_s), + TEST_FUNC(test_strlastchar_s), + TEST_FUNC(test_strlastdiff_s), + TEST_FUNC(test_strlastsame_s), + TEST_FUNC(test_strljustify_s), + TEST_FUNC(test_strncat_s), + TEST_FUNC(test_strncpy_s), + TEST_FUNC(test_strnlen_s), + TEST_FUNC(test_strnterminate_s), + TEST_FUNC(test_strpbrk_s), + TEST_FUNC(test_strprefix_s), + TEST_FUNC(test_strremovews_s), + TEST_FUNC(test_strspn_s), + TEST_FUNC(test_strstr_s), + TEST_FUNC(test_strtok_s), + TEST_FUNC(test_strtolowercase_s), + TEST_FUNC(test_strtouppercase_s), + TEST_FUNC(test_strzero_s), + TEST_FUNC(test_wcpcpy_s), + TEST_FUNC(test_wcscat_s), + TEST_FUNC(test_wcscpy_s), + TEST_FUNC(test_wcsncat_s), + TEST_FUNC(test_wcsncpy_s), + TEST_FUNC(test_wcsnlen_s), + TEST_FUNC(test_wmemcpy_s), + TEST_FUNC(test_wmemmove_s), + TEST_FUNC(test_wmemset_s), + TEST_FUNC(test_wmemcmp_s), + }; + unsigned int i; + int errs; puts("!!!Basic Test Suite for Safe String Operations!!!"); puts(" ERRORS PRINT TO CONSOLE"); puts("----------------BEGIN TEST--------------------"); - test_memcmp_s(); - test_memcmp16_s(); - test_memcmp32_s(); - test_memcpy_s(); - test_memcpy16_s(); - test_memcpy32_s(); - test_memmove_s(); - test_memmove16_s(); - test_memmove32_s(); - test_memset_s(); - test_memset16_s(); - test_memset32_s(); - test_memzero_s(); - test_memzero16_s(); - test_memzero32_s(); - - - test_stpcpy_s(); /* New - Done */ - test_stpncpy_s(); /* New - Done */ - - test_strcasecmp_s(); - test_strcasestr_s(); - test_strcat_s(); - test_strcmp_s(); - test_strcmpfld_s(); - test_strcpy_s(); - test_strcpyfld_s(); - test_strcpyfldin_s(); - test_strcpyfldout_s(); - test_strcspn_s (); - - test_strfirstchar_s(); - test_strfirstdiff_s(); - test_strfirstsame_s(); - test_strisalphanumeric_s(); - test_strisascii_s(); - test_strisdigit_s(); - test_strishex_s(); - test_strislowercase_s(); - test_strismixed_s(); - test_strispassword_s(); - test_strisuppercase_s(); - - test_strlastchar_s(); - test_strlastdiff_s(); - test_strlastsame_s(); - test_strljustify_s(); - - test_strncat_s (); - test_strncpy_s (); - test_strnlen_s (); - test_strnterminate_s(); - test_strpbrk_s (); - test_strprefix_s(); - test_strremovews_s(); - test_strspn_s (); - test_strstr_s (); - test_strtok_s(); - test_strtolowercase_s(); - test_strtouppercase_s(); - test_strzero_s(); - - test_wcpcpy_s(); /* New - Done */ - test_wcscat_s(); /* New - Done */ - test_wcscpy_s(); /* New - Done */ - test_wcsncat_s(); /* New - Done */ - test_wcsncpy_s(); /* New - Done */ - test_wcsnlen_s(); /* New - Done */ - test_wmemcpy_s(); /* New - Done */ - test_wmemmove_s(); /* New - Done */ - test_wmemset_s(); /* New - Done */ - test_wmemcmp_s(); /* New - Done */ + for (i = 0; i < sizeof(funcs) / sizeof(funcs[0]); ++i) { + printf("test %d = %s\n", i, funcs[i].name); + errs = funcs[i].func(); + if (errs) { + printf("%d error(s) found, test halted\n", errs); + return 1; + } + } char str[] = "This is a char array"; char str2[] = "...And another one."; @@ -171,20 +181,29 @@ int main(void) { char *ret = stpcpy_s(&dest[20], 108, str2, &err); if (err != EOK) { puts("stpcpy_s returned failure"); + ++errs; } int val = 55; if ( (err = snprintf_s_si(src, 128, "Test sprintf_s_si() with simple format string [%s] and %d", dest, val)) < 0 ) { if ( SNPRFNEGATE(ESBADFMT) == err ) { puts("snprintf_s_si returned failure: Error Bad Format String."); + ++errs; } else if ( SNPRFNEGATE(ESFMTTYP) == err ) { puts("snprintf_s_si returned failure: Error Bad Argument Type for format string."); + ++errs; } else { printf("snprintf_s_si failed with unknown error: %d\n", err); + ++errs; } } else if ((strcmp_s(src, smax, "Test sprintf_s_si() with simple format string [This is a char array...And another one.] and 55", &indicator) != EOK ) || (indicator != 0 )) { puts("snprintf_s_si() failed - output was:"); puts(src); + ++errs; + } + if (errs) { + printf("%d error(s) found, test halted\n", errs); + return 1; } puts("----------------END TEST--------------------"); return EXIT_SUCCESS; diff --git a/unittests/test_memcmp16_s.c b/unittests/test_memcmp16_s.c index d4ed057..81b9f83 100644 --- a/unittests/test_memcmp16_s.c +++ b/unittests/test_memcmp16_s.c @@ -9,10 +9,10 @@ #define LEN ( 128 ) -int test_memcmp16_s() +int test_memcmp16_s(void) { errno_t rc; - + int errs = 0; uint32_t len; int32_t ind; int32_t std_ind; @@ -25,56 +25,63 @@ int test_memcmp16_s() rc = memcmp16_s(NULL, LEN, mem2, LEN, &ind); if (rc != ESNULLP) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp16_s(mem1, LEN, NULL, LEN, &ind); if (rc != ESNULLP) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp16_s(mem1, LEN, mem2, LEN, NULL); if (rc != ESNULLP) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp16_s(mem1, 0, mem2, LEN, &ind); if (rc != ESZEROL) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp16_s(mem1, LEN, mem2, 0, &ind); if (rc != ESZEROL) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp16_s(mem1, RSIZE_MAX_MEM+1, mem2, LEN, &ind); if (rc != ESLEMAX) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp16_s(mem1, LEN, mem2, RSIZE_MAX_MEM+1, &ind); if (rc != ESLEMAX) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -85,19 +92,22 @@ int test_memcmp16_s() len = 1; rc = memcmp16_s(mem1, len, mem2, len, &ind); if (rc != EOK) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } if (ind != 0) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } std_ind = memcmp(mem1, mem2, len*2); if (ind != std_ind) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -108,19 +118,22 @@ int test_memcmp16_s() len = 2; rc = memcmp16_s(mem1, len, mem2, len, &ind); if (rc != EOK) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } if (ind != 0) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } std_ind = memcmp(mem1, mem2, len*2); if (ind != std_ind) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -130,19 +143,22 @@ int test_memcmp16_s() rc = memcmp16_s(mem1, LEN, mem2, LEN, &ind); if (rc != EOK) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } if (ind != 0) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } std_ind = memcmp(mem1, mem2, LEN*2); if (ind != std_ind) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -154,23 +170,26 @@ int test_memcmp16_s() rc = memcmp16_s(mem1, LEN, mem2, LEN, &ind); if (rc != EOK) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } if (ind == 0) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } std_ind = memcmp(mem1, mem2, LEN*2); if (ind != std_ind) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ /*--------------------------------------------------*/ - return (0); + return errs; } diff --git a/unittests/test_memcmp32_s.c b/unittests/test_memcmp32_s.c index 4913e31..c6bdb20 100644 --- a/unittests/test_memcmp32_s.c +++ b/unittests/test_memcmp32_s.c @@ -9,9 +9,10 @@ #define LEN ( 128 ) -int test_memcmp32_s() +int test_memcmp32_s(void) { errno_t rc; + int errs = 0; uint32_t len; int32_t ind; int32_t std_ind; @@ -24,56 +25,63 @@ int test_memcmp32_s() rc = memcmp32_s(NULL, LEN, mem2, LEN, &ind); if (rc != ESNULLP) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp32_s(mem1, LEN, NULL, LEN, &ind); if (rc != ESNULLP) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp32_s(mem1, LEN, mem2, LEN, NULL); if (rc != ESNULLP) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp32_s(mem1, 0, mem2, LEN, &ind); if (rc != ESZEROL) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp32_s(mem1, LEN, mem2, 0, &ind); if (rc != ESZEROL) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp32_s(mem1, RSIZE_MAX_MEM+1, mem2, LEN, &ind); if (rc != ESLEMAX) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp32_s(mem1, LEN, mem2, RSIZE_MAX_MEM+1, &ind); if (rc != ESLEMAX) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -84,19 +92,22 @@ int test_memcmp32_s() len = 1; rc = memcmp32_s(mem1, len, mem2, len, &ind); if (rc != EOK) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } if (ind != 0) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } std_ind = memcmp(mem1, mem2, len*4); if (ind != 0) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -107,19 +118,22 @@ int test_memcmp32_s() len = 2; rc = memcmp32_s(mem1, len, mem2, len, &ind); if (rc != EOK) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } if (ind != 0) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } std_ind = memcmp(mem1, mem2, len*4); if (ind != 0) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -129,19 +143,22 @@ int test_memcmp32_s() rc = memcmp32_s(mem1, LEN, mem2, LEN, &ind); if (rc != EOK) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } if (ind != 0) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } ind = memcmp(mem1, mem2, LEN*4); if (ind != 0) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -153,17 +170,19 @@ int test_memcmp32_s() rc = memcmp32_s(mem1, LEN, mem2, LEN, &ind); if (rc != EOK) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } if (ind == 0) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc ); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ /*--------------------------------------------------*/ - return (0); + return errs; } diff --git a/unittests/test_memcmp_s.c b/unittests/test_memcmp_s.c index fb9bdb5..65e4505 100644 --- a/unittests/test_memcmp_s.c +++ b/unittests/test_memcmp_s.c @@ -16,6 +16,7 @@ static uint8_t mem2[LEN]; int test_memcmp_s (void) { errno_t rc; + int errs = 0; int32_t ind; int32_t std_ind; uint32_t len; @@ -24,56 +25,63 @@ int test_memcmp_s (void) rc = memcmp_s(NULL, LEN, mem2, LEN, &ind); if (rc != ESNULLP) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp_s(mem1, LEN, NULL, LEN, &ind); if (rc != ESNULLP) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp_s(mem1, LEN, mem2, LEN, NULL); if (rc != ESNULLP) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp_s(mem1, 0, mem2, LEN, &ind); if (rc != ESZEROL) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp_s(mem1, LEN, mem2, 0, &ind); if (rc != ESZEROL) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp_s(mem1, LEN, mem2, RSIZE_MAX_MEM+1, &ind); if (rc != ESLEMAX) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcmp_s(mem1, RSIZE_MAX_MEM+1, mem2, LEN, &ind); if (rc != ESLEMAX) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -85,19 +93,22 @@ int test_memcmp_s (void) rc = memcmp_s(mem1, len, mem2, len, &ind); if (rc != EOK) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } if (ind != 0) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } std_ind = memcmp(mem1, mem2, len); if (ind != std_ind) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -109,19 +120,22 @@ int test_memcmp_s (void) rc = memcmp_s(&mem1[1], len, &mem2[1], len, &ind); if (rc != EOK) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } if (ind != 0) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } std_ind = memcmp(mem1, mem2, len); if (ind != std_ind) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -133,19 +147,22 @@ int test_memcmp_s (void) rc = memcmp_s(mem1, len, mem2, len, &ind); if (rc != EOK) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } if (ind != 0) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } std_ind = memcmp(mem1, mem2, len); if (ind != std_ind) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -158,13 +175,15 @@ int test_memcmp_s (void) rc = memcmp_s(mem1, len, mem2, len, &ind); if (rc != EOK) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } if (ind < 1) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -177,13 +196,15 @@ int test_memcmp_s (void) rc = memcmp_s(mem1, len, mem2, len, &ind); if (rc != EOK) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } if (ind > -1) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -195,17 +216,19 @@ int test_memcmp_s (void) rc = memcmp_s(mem1, len, mem2, len, &ind); if (rc != EOK) { - debug_printf("%s %u Ind=%d Error rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d Error rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } if (ind != 0) { - debug_printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + printf("%s %u Ind=%d rc=%u \n", + __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ - return (0); + return errs; } diff --git a/unittests/test_memcpy16_s.c b/unittests/test_memcpy16_s.c index 0e5da19..24283a2 100644 --- a/unittests/test_memcpy16_s.c +++ b/unittests/test_memcpy16_s.c @@ -11,9 +11,10 @@ #define LEN ( 1024 ) -int test_memcpy16_s() +int test_memcpy16_s(void) { errno_t rc; + int errs = 0; uint32_t len; uint32_t i; @@ -24,48 +25,54 @@ int test_memcpy16_s() rc = memcpy16_s(NULL, LEN, mem2, LEN); if (rc != ESNULLP) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcpy16_s(mem1, 0, mem2, LEN); if (rc != ESZEROL) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcpy16_s(mem1, RSIZE_MAX_MEM16+1, mem2, LEN); if (rc != ESLEMAX) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcpy16_s(mem1, LEN, NULL, LEN); if (rc != ESNULLP) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcpy16_s(mem1, 10, mem2, 0); if (rc != ESZEROL) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ rc = memcpy16_s(mem1, LEN, mem2, RSIZE_MAX_MEM16+1); if (rc != ESLEMAX) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -76,14 +83,16 @@ int test_memcpy16_s() len = 1; rc = memcpy16_s(mem1, len, mem2, len); if (rc != EOK) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } for (i=0; i -%s- (smax=%zu) Error rc=%u \n", __FUNCTION__, __LINE__, str1, str2, sz, rc ); + ++errs; } if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) { printf("Returned pointer incorrect: %s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -376,11 +409,13 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) { printf("Returned pointer incorrect: %s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } else { /* be sure the results are the same as strcmp */ @@ -389,12 +424,14 @@ printf("Test #%d:\n", ++testno); if (ind != 0 || sz != 3) { printf("%s %u -%s- <> -%s- (smax=%zu) Error rc=%u \n", __FUNCTION__, __LINE__, str1, str2, sz, rc ); + ++errs; } /* be sure that the slack is correct */ for (i=1; i<6; i++) { if (ret[i] != 'x') { printf("%s %u Incorrect Slack at returned ptr index %d Error rc=%u \n", __FUNCTION__, __LINE__, i, rc ); + ++errs; } } } @@ -410,11 +447,13 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) { printf("Returned pointer incorrect: %s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } else { /* be sure the results are the same as strcmp */ @@ -422,12 +461,14 @@ printf("Test #%d:\n", ++testno); if (ind != 0) { printf("%s %u -%s- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } else { /* be sure that the nulls are correct */ for (i=0; i<5; i++) { if (ret[i] != '\0') { printf("%s %u Incorrect NULL fill at returned ptr index %d Error rc=%u \n", __FUNCTION__, __LINE__, i, rc ); + ++errs; } } /* be sure that the slack is correct */ @@ -435,6 +476,7 @@ printf("Test #%d:\n", ++testno); if (ret[i] != 'x') { printf("%s %u Incorrect Slack at returned ptr index %d Error rc=%u \n", __FUNCTION__, __LINE__, i, rc ); + ++errs; } } } @@ -451,11 +493,13 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) { printf("Returned pointer incorrect: %s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } else { /* be sure the results are the same as strcmp */ @@ -463,12 +507,14 @@ printf("Test #%d:\n", ++testno); if (ind != 0) { printf("%s %u -%s- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } else { /* be sure that the slack is correct */ for (i=1; i<5; i++) { if (ret[i] != 'x') { printf("%s %u Incorrect Slack at returned ptr index %d Error rc=%u \n", __FUNCTION__, __LINE__, i, rc ); + ++errs; } } } @@ -485,11 +531,13 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) { printf("Returned pointer incorrect: %s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } else { /* be sure the results are the same as strcmp */ @@ -497,12 +545,14 @@ printf("Test #%d:\n", ++testno); if (ind != 0) { printf("%s %u -%s- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } else { /* be sure that the slack is correct */ for (i=1; i<5; i++) { if (ret[i] != 'x') { printf("%s %u Incorrect Slack at returned ptr index %d Error rc=%u \n", __FUNCTION__, __LINE__, i, rc ); + ++errs; } } } @@ -520,15 +570,18 @@ printf("Test #%d:\n", ++testno); if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (ret != NULL) { printf("Returned pointer incorrect: %s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (*str1 != '\0') { printf("%s %u -%s- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -543,15 +596,18 @@ printf("Test #%d:\n", ++testno); if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (ret != NULL) { printf("Returned pointer incorrect: %s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (*str1 != '\0') { printf("%s %u -%s- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -566,6 +622,7 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /* be sure the results are the same as strcmp */ @@ -573,10 +630,12 @@ printf("Test #%d:\n", ++testno); if (ind != 0) { printf("%s %u -%s- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) { printf("Returned pointer incorrect: %s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -590,10 +649,12 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) { printf("Returned pointer incorrect: %s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /* be sure the results are the same as strcpy */ @@ -601,10 +662,11 @@ printf("Test #%d:\n", ++testno); if (ind != 0) { printf("%s %u -%s- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } /*--------------------------------------------------*/ - return (0); + return errs; } diff --git a/unittests/test_strcasecmp_s.c b/unittests/test_strcasecmp_s.c index c54892f..001e3bd 100644 --- a/unittests/test_strcasecmp_s.c +++ b/unittests/test_strcasecmp_s.c @@ -17,6 +17,7 @@ static char str2[LEN]; int test_strcasecmp_s (void) { errno_t rc; + int errs = 0; int ind; int std_ind; @@ -27,11 +28,13 @@ int test_strcasecmp_s (void) if (rc != ESNULLP) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -40,11 +43,13 @@ int test_strcasecmp_s (void) if (rc != ESNULLP) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -53,6 +58,7 @@ int test_strcasecmp_s (void) if (rc != ESNULLP) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -61,11 +67,13 @@ int test_strcasecmp_s (void) if (rc != ESZEROL) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -74,11 +82,13 @@ int test_strcasecmp_s (void) if (rc != ESLEMAX) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -90,17 +100,20 @@ int test_strcasecmp_s (void) if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } std_ind = strcasecmp(str1, str2); if (ind != std_ind) { printf("%s %u ind=%d std_ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, std_ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -112,11 +125,13 @@ int test_strcasecmp_s (void) if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -127,11 +142,13 @@ int test_strcasecmp_s (void) if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -143,11 +160,13 @@ int test_strcasecmp_s (void) if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -159,17 +178,20 @@ int test_strcasecmp_s (void) if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } std_ind = strcasecmp(str1, str2); if (ind != std_ind) { printf("%s %u ind=%d std_ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, std_ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -181,11 +203,13 @@ int test_strcasecmp_s (void) if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -196,11 +220,13 @@ int test_strcasecmp_s (void) if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -212,11 +238,13 @@ int test_strcasecmp_s (void) if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != ('I' - 'E')) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -228,15 +256,17 @@ int test_strcasecmp_s (void) if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != ('1' - 'I')) { printf("%s %u Error ind=%d rc=%d %d \n", __FUNCTION__, __LINE__, ind, rc, ('1' - 'I')); + ++errs; } /*--------------------------------------------------*/ - return (0); + return errs; } diff --git a/unittests/test_strcasestr_s.c b/unittests/test_strcasestr_s.c index 6e9eb5b..fb5de23 100644 --- a/unittests/test_strcasestr_s.c +++ b/unittests/test_strcasestr_s.c @@ -15,6 +15,7 @@ extern errno_t strcasestr_s (char *dest, rsize_t dmax, const char *src, rsize_t int test_strcasestr_s() { + int errs = 0; errno_t rc; char *sub; char *std_sub; @@ -32,11 +33,13 @@ int test_strcasestr_s() if (rc != ESNULLP) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -45,11 +48,13 @@ int test_strcasestr_s() if (rc != ESNULLP) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -58,6 +63,7 @@ int test_strcasestr_s() if (rc != ESNULLP) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -66,11 +72,13 @@ int test_strcasestr_s() if (rc != ESZEROL) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -79,11 +87,13 @@ int test_strcasestr_s() if (rc != ESLEMAX) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -92,11 +102,13 @@ int test_strcasestr_s() if (rc != ESZEROL) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -105,11 +117,13 @@ int test_strcasestr_s() if (rc != ESLEMAX) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -121,11 +135,13 @@ int test_strcasestr_s() if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub != str1) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /* compare to legacy */ @@ -137,6 +153,7 @@ int test_strcasestr_s() printf("str2:[%s]\n", str2); printf("strcasestr_s returns:[%p]\n", sub); printf("strcasestr returns:[%p]\n\n", std_sub); + ++errs; } /*--------------------------------------------------*/ @@ -148,11 +165,13 @@ int test_strcasestr_s() if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub != str1) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /* compare to legacy */ @@ -164,6 +183,7 @@ int test_strcasestr_s() printf("str2:[%s]\n", str2); printf("strcasestr_s returns:[%p]\n", sub); printf("strcasestr returns:[%p]\n\n", std_sub); + ++errs; } /*--------------------------------------------------*/ @@ -176,11 +196,13 @@ int test_strcasestr_s() if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub != str1) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /* compare to legacy */ @@ -192,6 +214,7 @@ int test_strcasestr_s() printf("str2:[%s]\n", str2); printf("strcasestr_s returns:[%p]\n", sub); printf("strcasestr returns:[%p]\n\n", std_sub); + ++errs; } /*--------------------------------------------------*/ @@ -204,11 +227,13 @@ int test_strcasestr_s() if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub != &str1[0]) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -221,11 +246,13 @@ int test_strcasestr_s() if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub != &str1[1]) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -238,11 +265,13 @@ int test_strcasestr_s() if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub != &str1[15]) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -258,11 +287,13 @@ int test_strcasestr_s() if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub != &str1[18]) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /* compare to legacy */ @@ -274,6 +305,7 @@ int test_strcasestr_s() printf("str2:[%s]\n", str2); printf("strcasestr_s returns:[%p]\n", sub); printf("strcasestr returns:[%p]\n\n", std_sub); + ++errs; } /*--------------------------------------------------*/ @@ -289,11 +321,13 @@ int test_strcasestr_s() if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub != &str1[18]) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /* compare to legacy */ @@ -305,6 +339,7 @@ int test_strcasestr_s() printf("str2:[%s]\n", str2); printf("strcasestr_s returns:[%p]\n", sub); printf("strcasestr returns:[%p]\n\n", std_sub); + ++errs; } /*--------------------------------------------------*/ @@ -316,11 +351,13 @@ int test_strcasestr_s() if (rc != ESNOTFND) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub != NULL) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -332,11 +369,13 @@ int test_strcasestr_s() if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub != &str1[5]) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -348,11 +387,13 @@ int test_strcasestr_s() if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub != &str1[5]) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -364,11 +405,13 @@ int test_strcasestr_s() if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub != &str1[5]) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -381,11 +424,13 @@ int test_strcasestr_s() if (rc != ESNOTFND) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub != NULL) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -397,11 +442,13 @@ int test_strcasestr_s() if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub != &str1[5]) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -414,11 +461,13 @@ int test_strcasestr_s() if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (sub != &str1[1]) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /* compare to legacy */ @@ -426,9 +475,10 @@ int test_strcasestr_s() if ((int)sub != (int)std_sub) { // comparison to handle 32-bit library return and 64-bit library return printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ - return (0); + return errs; } diff --git a/unittests/test_strcat_s.c b/unittests/test_strcat_s.c index 2e6f700..614c26a 100644 --- a/unittests/test_strcat_s.c +++ b/unittests/test_strcat_s.c @@ -16,6 +16,7 @@ static char str2[LEN]; int test_strcat_s (void) { errno_t rc; + int errs = 0; int32_t ind; int32_t len1; int32_t len2; @@ -25,32 +26,36 @@ int test_strcat_s (void) rc = strcat_s(NULL, LEN, str2); if (rc != ESNULLP) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ rc = strcat_s(str1, LEN, NULL); if (rc != ESNULLP) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ rc = strcat_s(str1, 0, str2); if (rc != ESZEROL) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ rc = strcat_s(str1, (RSIZE_MAX_STR+1), str2); if (rc != ESLEMAX) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -61,13 +66,15 @@ int test_strcat_s (void) rc = strcat_s(str1, 1, str2); if (rc != ESUNTERM) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != '\0') { - debug_printf("%s %u Expected null \n", - __FUNCTION__, __LINE__); + printf("%s %u Expected null \n", + __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -77,13 +84,15 @@ int test_strcat_s (void) rc = strcat_s(str1, 2, str2); if (rc != ESUNTERM) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != '\0') { - debug_printf("%s %u Expected null \n", - __FUNCTION__, __LINE__); + printf("%s %u Expected null \n", + __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -96,14 +105,16 @@ int test_strcat_s (void) rc = strcat_s(str1, 50, str2); if (rc != EOK) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } len3 = strlen(str1); if (len3 != (len1+len2)) { - debug_printf("%s %u lengths wrong: %u %u %u \n", - __FUNCTION__, __LINE__, len1, len2, len3); + printf("%s %u lengths wrong: %u %u %u \n", + __FUNCTION__, __LINE__, len1, len2, len3); + ++errs; } /*--------------------------------------------------*/ @@ -114,13 +125,15 @@ int test_strcat_s (void) rc = strcat_s(str1, 1, str2); if (rc != ESNOSPC) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != '\0') { - debug_printf("%s %u Expected null \n", - __FUNCTION__, __LINE__); + printf("%s %u Expected null \n", + __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -130,13 +143,15 @@ int test_strcat_s (void) rc = strcat_s(str1, 2, str2); if (rc != ESNOSPC) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != '\0') { - debug_printf("%s %u Expected null \n", - __FUNCTION__, __LINE__); + printf("%s %u Expected null \n", + __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -146,14 +161,16 @@ int test_strcat_s (void) rc = strcat_s(str1, 20, str2); if (rc != EOK) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } ind = strcmp(str1, str2 ); if (ind != 0) { - debug_printf("%s %u Error -%s- \n", - __FUNCTION__, __LINE__, str1); + printf("%s %u Error -%s- \n", + __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -164,13 +181,15 @@ int test_strcat_s (void) rc = strcat_s(str1, LEN, str2); if (rc != EOK) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != '\0') { - debug_printf("%s %u Expected null \n", - __FUNCTION__, __LINE__); + printf("%s %u Expected null \n", + __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -180,14 +199,16 @@ int test_strcat_s (void) rc = strcat_s(str1, LEN, str2); if (rc != EOK) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } ind = strcmp(str1, str2); if (ind != 0) { - debug_printf("%s %u Error -%s- \n", - __FUNCTION__, __LINE__, str1); + printf("%s %u Error -%s- \n", + __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -197,14 +218,16 @@ int test_strcat_s (void) rc = strcat_s(str1, LEN, str2); if (rc != EOK) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } ind = strcmp(str1, "qqweqqkeep it simple"); if (ind != 0) { - debug_printf("%s %u Error -%s- \n", - __FUNCTION__, __LINE__, str1); + printf("%s %u Error -%s- \n", + __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -214,8 +237,9 @@ int test_strcat_s (void) rc = strcat_s(str1, 12, str2); if (rc != ESNOSPC) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -225,14 +249,16 @@ int test_strcat_s (void) rc = strcat_s(str1, 52, str2); if (rc != EOK) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } ind = strcmp(str1, "1234keep it simple"); if (ind != 0) { - debug_printf("%s %u Error -%s- \n", - __FUNCTION__, __LINE__, str1); + printf("%s %u Error -%s- \n", + __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -241,13 +267,15 @@ int test_strcat_s (void) rc = strcat_s(str1, 8, &str1[7]); if (rc != ESOVRLP) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != '\0') { - debug_printf("%s %u Expected null \n", - __FUNCTION__, __LINE__); + printf("%s %u Expected null \n", + __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -256,13 +284,15 @@ int test_strcat_s (void) rc = strcat_s(str1, 9, &str1[8]); if (rc != ESOVRLP) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != '\0') { - debug_printf("%s %u Expected null \n", - __FUNCTION__, __LINE__); + printf("%s %u Expected null \n", + __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -272,14 +302,16 @@ int test_strcat_s (void) rc = strcat_s(str2, 31, &str1[0]); if (rc != EOK) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } ind = strcmp(str2, "123keep it simple"); if (ind != 0) { - debug_printf("%s %u Error -%s- \n", - __FUNCTION__, __LINE__, str1); + printf("%s %u Error -%s- \n", + __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -289,19 +321,21 @@ int test_strcat_s (void) rc = strcat_s(str2, 10, str1); if (rc != EOK) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc); + ++errs; } ind = strcmp(str2, "123456789"); if (ind != 0) { - debug_printf("%s %u Error -%s- \n", - __FUNCTION__, __LINE__, str1); + printf("%s %u Error -%s- \n", + __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ - return (0); + return errs; } diff --git a/unittests/test_strcmp_s.c b/unittests/test_strcmp_s.c index b15fff5..4920a6e 100644 --- a/unittests/test_strcmp_s.c +++ b/unittests/test_strcmp_s.c @@ -17,6 +17,7 @@ static char str2[LEN]; int test_strcmp_s (void) { errno_t rc; + int errs = 0; int ind; int std_ind; @@ -26,11 +27,13 @@ int test_strcmp_s (void) if (rc != ESNULLP) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -39,11 +42,13 @@ int test_strcmp_s (void) if (rc != ESNULLP) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -52,6 +57,7 @@ int test_strcmp_s (void) if (rc != ESNULLP) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -60,11 +66,13 @@ int test_strcmp_s (void) if (rc != ESZEROL) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -73,11 +81,13 @@ int test_strcmp_s (void) if (rc != ESLEMAX) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -89,17 +99,20 @@ int test_strcmp_s (void) if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } std_ind = strcmp(str1, str2); if (ind != std_ind) { printf("%s %u ind=%d std_ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, std_ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -111,11 +124,13 @@ int test_strcmp_s (void) if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -128,17 +143,20 @@ int test_strcmp_s (void) if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != (-32)) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } std_ind = strcmp(str1, str2); if (ind != std_ind) { printf("%s %u ind=%d std_ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, std_ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -151,17 +169,20 @@ int test_strcmp_s (void) if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 32) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } std_ind = strcmp(str1, str2); if (ind != std_ind) { printf("%s %u ind=%d std_ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, std_ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -172,11 +193,13 @@ int test_strcmp_s (void) if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind != 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /* be sure the results are the same as strcmp */ @@ -184,6 +207,7 @@ int test_strcmp_s (void) if (ind != std_ind) { printf("%s %u ind=%d std_ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, std_ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -195,11 +219,13 @@ int test_strcmp_s (void) if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind <= 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /* be sure the results are the same as strcmp */ @@ -207,6 +233,7 @@ int test_strcmp_s (void) if (ind != std_ind) { printf("%s %u ind=%d std_ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, std_ind, rc); + ++errs; } /*--------------------------------------------------*/ @@ -218,11 +245,13 @@ int test_strcmp_s (void) if (rc != EOK) { printf("%s %u Error rc=%d \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (ind >= 0) { printf("%s %u Error ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, rc); + ++errs; } /* be sure the results are the same as strcmp */ @@ -230,11 +259,12 @@ int test_strcmp_s (void) if (ind != std_ind) { printf("%s %u ind=%d std_ind=%d rc=%d \n", __FUNCTION__, __LINE__, ind, std_ind, rc); + ++errs; } /*--------------------------------------------------*/ - return (0); + return errs; } diff --git a/unittests/test_strcmpfld_s.c b/unittests/test_strcmpfld_s.c index 8a2d8d9..0a55b0e 100644 --- a/unittests/test_strcmpfld_s.c +++ b/unittests/test_strcmpfld_s.c @@ -11,9 +11,10 @@ #define MAX ( 128 ) #define LEN ( 128 ) -int test_strcmpfld_s() +int test_strcmpfld_s(void) { errno_t rc; + int errs = 0; uint32_t i; rsize_t len; int ind; @@ -26,8 +27,9 @@ int test_strcmpfld_s() rc = strcmpfld_s(NULL, LEN, str2, &ind); if (rc != ESNULLP) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc ); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -35,8 +37,9 @@ int test_strcmpfld_s() len = 5; rc = strcmpfld_s(str1, len, NULL, &ind); if (rc != ESNULLP) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc ); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -44,24 +47,27 @@ int test_strcmpfld_s() len = 5; rc = strcmpfld_s(str1, len, str2, NULL); if (rc != ESNULLP) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc ); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ rc = strcmpfld_s(str1, 0, str2, &ind); if (rc != ESZEROL) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc ); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ rc = strcmpfld_s(str1, (RSIZE_MAX_STR+1), str2, &ind); if (rc != ESLEMAX) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc ); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -73,14 +79,16 @@ int test_strcmpfld_s() len = 1; rc = strcmpfld_s(str1, len, str2, &ind); if (rc != EOK) { - debug_printf("%s %u Error rc=%u \n", - __FUNCTION__, __LINE__, rc ); + printf("%s %u Error rc=%u \n", + __FUNCTION__, __LINE__, rc ); + ++errs; } for (i=0; i -%ls- (smax=%lu) Error rc=%u \n", __FUNCTION__, __LINE__, str1, str2, sz, rc ); + ++errs; } if (ret == NULL || ret[0] != L'\0' || ret != str1+wcsnlen_s(str1, LEN) ) { printf("Returned pointer incorrect: %s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -376,22 +409,26 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (ret == NULL || ret[0] != L'\0' || ret != str1+wcsnlen_s(str1, LEN) ) { printf("Returned pointer incorrect: %s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } sz = wcsnlen_s(str1, LEN); if (sz != 5) { printf("%s %u (sz=%lu <> 5) Error rc=%u \n", __FUNCTION__, __LINE__, sz, rc ); + ++errs; } rc = memcmp_s(str1, LEN, str2, (6)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u -%ls- <> -%ls- (size=%lu) Error rc=%u \n", __FUNCTION__, __LINE__, str1, str2, sz, rc ); + ++errs; } @@ -407,15 +444,18 @@ printf("Test #%d:\n", ++testno); if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (ret != NULL) { printf("Returned pointer incorrect: %s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (*str1 != L'\0') { printf("%s %u -%ls- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -430,15 +470,18 @@ printf("Test #%d:\n", ++testno); if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (ret != NULL) { printf("Returned pointer incorrect: %s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (*str2 != L'\0') { printf("%s %u -%ls- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -452,16 +495,19 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } rc = memcmp_s(str1, LEN, str2, (3)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u -%ls- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } if (ret == NULL || ret[0] != L'\0' || ret != str2+wcsnlen_s(str2, LEN) ) { printf("Returned pointer incorrect: %s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -475,16 +521,19 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (ret == NULL || ret[0] != L'\0' || ret != str1+wcsnlen_s(str1, LEN) ) { printf("Returned pointer incorrect: %s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } rc = memcmp_s(str1, LEN, str2, (3)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u -%ls- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } @@ -500,19 +549,22 @@ printf("Test #%d:\n", ++testno); if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (ret != NULL) { printf("Returned pointer incorrect: %s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (*str2 != L'\0') { printf("%s %u -%ls- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } /*--------------------------------------------------*/ - return (0); + return errs; } diff --git a/unittests/test_wcscat_s.c b/unittests/test_wcscat_s.c index 689af75..e05bb22 100644 --- a/unittests/test_wcscat_s.c +++ b/unittests/test_wcscat_s.c @@ -44,6 +44,7 @@ extern errno_t wcscat_s(wchar_t* dest, rsize_t dmax, const wchar_t* src); int test_wcscat_s (void) { errno_t rc; + int errs = 0; int32_t ind; int32_t len1; int32_t len2; @@ -60,6 +61,7 @@ int test_wcscat_s (void) if (rc != ESNULLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -70,6 +72,7 @@ int test_wcscat_s (void) if (rc != ESNULLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -80,6 +83,7 @@ int test_wcscat_s (void) if (rc != ESZEROL) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -90,6 +94,7 @@ int test_wcscat_s (void) if (rc != ESLEMAX) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -103,11 +108,13 @@ int test_wcscat_s (void) if (rc != ESOVRLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != L'\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -121,11 +128,13 @@ int test_wcscat_s (void) if (rc != ESUNTERM) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != L'\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -139,11 +148,13 @@ int test_wcscat_s (void) if (rc != ESUNTERM) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != L'\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -157,11 +168,13 @@ int test_wcscat_s (void) if (rc != ESOVRLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != L'\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -178,12 +191,14 @@ int test_wcscat_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } len3 = wcsnlen_s(str1, LEN); if (len3 != (len1+len2)) { printf("%s %u lengths wrong: %u %u %u \n", __FUNCTION__, __LINE__, len1, len2, len3); + ++errs; } /*--------------------------------------------------*/ @@ -198,11 +213,13 @@ int test_wcscat_s (void) if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != L'\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -216,11 +233,13 @@ int test_wcscat_s (void) if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != L'\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -234,12 +253,14 @@ int test_wcscat_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } rc = memcmp_s(str1, LEN, str2, wcsnlen_s(str2, LEN)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u Error -%ls- \n", __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -254,11 +275,13 @@ int test_wcscat_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != L'\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -272,12 +295,14 @@ int test_wcscat_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } rc = memcmp_s(str2, LEN, L"keep it simple", (15)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u Error -%ls- \n", __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -291,12 +316,14 @@ int test_wcscat_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } rc = memcmp_s(str1, LEN, L"qqweqqkeep it simple", (20)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u Error -%ls- \n", __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -310,6 +337,7 @@ int test_wcscat_s (void) if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -323,12 +351,14 @@ int test_wcscat_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } rc = memcmp_s(str2, LEN, L"keep it simple1234", (19)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u Error -%ls- \n", __FUNCTION__, __LINE__, str1); + ++errs; } @@ -343,11 +373,13 @@ int test_wcscat_s (void) if (rc != ESOVRLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[13] != L'\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -361,12 +393,14 @@ int test_wcscat_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } rc = memcmp_s(str2, LEN, L"123keep it simple", (17)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u Error -%ls- \n", __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -380,6 +414,7 @@ int test_wcscat_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } rc = memcmp_s(str2, LEN, L"123456789", (9)*sizeof(wchar_t), &ind ); @@ -387,6 +422,7 @@ int test_wcscat_s (void) if (ind != 0) { printf("%s %u Error -%ls- \n", __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -400,17 +436,19 @@ int test_wcscat_s (void) if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str2[0] != L'\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ - return (0); + return errs; } diff --git a/unittests/test_wcscpy_s.c b/unittests/test_wcscpy_s.c index 294c168..90a627b 100644 --- a/unittests/test_wcscpy_s.c +++ b/unittests/test_wcscpy_s.c @@ -84,6 +84,7 @@ extern errno_t wcscpy_s(wchar_t* dest, rsize_t dmax, const wchar_t* src); int test_wcscpy_s (void) { errno_t rc; + int errs = 0; #ifdef SAFE_LIB_STR_NULL_SLACK uint32_t i; #endif /*SAFE_LIB_STR_NULL_SLACK*/ @@ -102,6 +103,7 @@ printf("Test #%d:\n", ++testno); if (rc != ESNULLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -112,6 +114,7 @@ printf("Test #%d:\n", ++testno); if (rc != ESZEROL) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -122,6 +125,7 @@ printf("Test #%d:\n", ++testno); if (rc != ESLEMAX) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -134,6 +138,7 @@ printf("Test #%d:\n", ++testno); if (rc != ESNULLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #ifdef SAFE_LIB_STR_NULL_SLACK @@ -141,12 +146,14 @@ printf("Test #%d:\n", ++testno); if (str1[i] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } } #else if (str1[0] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #endif @@ -161,6 +168,7 @@ printf("Test #%d:\n", ++testno); if (rc != ESLEMAX) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -173,6 +181,7 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -188,6 +197,7 @@ printf("Test #%d:\n", ++testno); if (rc != ESOVRLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #ifdef SAFE_LIB_STR_NULL_SLACK @@ -195,12 +205,14 @@ printf("Test #%d:\n", ++testno); if (str1[i] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } } #else if (str1[0] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #endif @@ -218,6 +230,7 @@ printf("Test #%d:\n", ++testno); if (rc != ESOVRLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #ifdef SAFE_LIB_STR_NULL_SLACK @@ -225,12 +238,14 @@ printf("Test #%d:\n", ++testno); if (str1[i] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } } #else if (str1[8] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #endif @@ -248,6 +263,7 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #ifdef SAFE_LIB_STR_NULL_SLACK @@ -255,12 +271,14 @@ printf("Test #%d:\n", ++testno); if (str1[i] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } } #else if (str1[0] != L'\0') { printf("%s %u Error - string should be NULL rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #endif @@ -277,12 +295,14 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } rc = memcmp_s(str2, LEN, str1, (sz+1)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u Error -%ls- <> -%ls-\n", __FUNCTION__, __LINE__, str2, str1); + ++errs; } @@ -298,12 +318,14 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } rc = memcmp_s(str1, LEN, str2, (sz)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u -%ls- <> -%ls- (smax=%lu) Error rc=%u \n", __FUNCTION__, __LINE__, str1, str2, sz, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -318,17 +340,20 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } sz = wcsnlen_s(str1, LEN); if (sz != sz_orig && sz == 14) { printf("%s %u (sz=%lu <> 5) Error rc=%u \n", __FUNCTION__, __LINE__, sz, rc ); + ++errs; } rc = memcmp_s(str1, LEN, str2, (sz)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u -%ls- <> -%ls- (size=%lu) Error rc=%u \n", __FUNCTION__, __LINE__, str1, str2, sz, rc ); + ++errs; } @@ -344,11 +369,13 @@ printf("Test #%d:\n", ++testno); if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (*str1 != L'\0') { printf("%s %u -%ls- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -362,11 +389,13 @@ printf("Test #%d:\n", ++testno); if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (*str2 != L'\0') { printf("%s %u -%ls- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -381,12 +410,14 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } rc = memcmp_s(str1, LEN, str2, (3)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u -%ls- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -402,12 +433,14 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } rc = memcmp_s(str1, LEN, str2, (3)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u -%ls- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } @@ -424,14 +457,16 @@ printf("Test #%d:\n", ++testno); if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (*str2 != L'\0') { printf("%s %u -%ls- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } /*--------------------------------------------------*/ - return (0); + return errs; } diff --git a/unittests/test_wcsncat_s.c b/unittests/test_wcsncat_s.c index 865ccf8..832a1d4 100644 --- a/unittests/test_wcsncat_s.c +++ b/unittests/test_wcsncat_s.c @@ -87,6 +87,7 @@ extern errno_t wcsncat_s (wchar_t *dest, rsize_t dmax, const wchar_t *src, rsize int test_wcsncat_s (void) { errno_t rc; + int errs = 0; int32_t ind; int32_t len1; int32_t len2; @@ -103,6 +104,7 @@ int test_wcsncat_s (void) if (rc != ESNULLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -113,6 +115,7 @@ int test_wcsncat_s (void) if (rc != ESNULLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -123,6 +126,7 @@ int test_wcsncat_s (void) if (rc != ESLEMAX) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ /* 4 Test zero maximum length of destination */ @@ -132,6 +136,7 @@ int test_wcsncat_s (void) if (rc != ESZEROL) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -142,6 +147,7 @@ int test_wcsncat_s (void) if (rc != ESLEMAX) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } @@ -156,11 +162,13 @@ int test_wcsncat_s (void) if (rc != ESOVRLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != '\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -173,11 +181,13 @@ int test_wcsncat_s (void) if (rc != ESUNTERM) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != '\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -191,11 +201,13 @@ int test_wcsncat_s (void) if (rc != ESOVRLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != '\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -212,18 +224,21 @@ int test_wcsncat_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } len3 = wcsnlen_s(str1, LEN); if (len3 != (len1+len2)) { printf("%s %u lengths wrong: %u %u %u \n", __FUNCTION__, __LINE__, len1, len2, len3); + ++errs; } rc = memcmp_s(str1, LEN, L"aaaaaaaaaakeep it si", (len3+1)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u Error -%ls- \n", __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -240,18 +255,21 @@ int test_wcsncat_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } len3 = wcsnlen_s(str1, LEN); if (len3 != (len1+len2)) { printf("%s %u lengths wrong: %u %u %u \n", __FUNCTION__, __LINE__, len1, len2, len3); + ++errs; } rc = memcmp_s(str1, LEN, L"aaaaaaaaaakeep it simple", (len3+1)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u Error -%ls- \n", __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -266,11 +284,13 @@ int test_wcsncat_s (void) if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != '\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -284,11 +304,13 @@ int test_wcsncat_s (void) if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != '\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -302,12 +324,14 @@ int test_wcsncat_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } rc = memcmp_s(str1, LEN, str2, wcsnlen_s(str2, LEN)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u Error -%ls- \n", __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -322,11 +346,13 @@ int test_wcsncat_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[0] != '\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -340,12 +366,14 @@ int test_wcsncat_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } rc = memcmp_s(str2, LEN, L"keep it simple", (15)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u Error -%ls- \n", __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -359,12 +387,14 @@ int test_wcsncat_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } rc = memcmp_s(str2, LEN, L"qqweqqkeep it simple", (20)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u Error -%ls- \n", __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -378,11 +408,13 @@ int test_wcsncat_s (void) if (rc != ESUNTERM) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str2[0] != '\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -396,11 +428,13 @@ int test_wcsncat_s (void) if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str2[0] != '\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -413,11 +447,13 @@ int test_wcsncat_s (void) if (rc != ESOVRLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str1[7] != '\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ @@ -431,12 +467,14 @@ int test_wcsncat_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } rc = memcmp_s(str2, LEN, L"keep it simple1234", (19)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u Error -%ls- \n", __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -450,12 +488,14 @@ int test_wcsncat_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } rc = memcmp_s(str2, LEN, L"123456789keep it", (17)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u Error -%ls- \n", __FUNCTION__, __LINE__, str1); + ++errs; } /*--------------------------------------------------*/ @@ -469,17 +509,19 @@ int test_wcsncat_s (void) if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } if (str2[0] != L'\0') { printf("%s %u Expected null \n", __FUNCTION__, __LINE__); + ++errs; } /*--------------------------------------------------*/ - return (0); + return errs; } diff --git a/unittests/test_wcsncpy_s.c b/unittests/test_wcsncpy_s.c index 009f47a..3a86ffa 100644 --- a/unittests/test_wcsncpy_s.c +++ b/unittests/test_wcsncpy_s.c @@ -84,6 +84,7 @@ extern errno_t wmemset_s (wchar_t *dest, wchar_t value, rsize_t len); int test_wcsncpy_s (void) { errno_t rc; + int errs = 0; #ifdef SAFE_LIB_STR_NULL_SLACK uint32_t i; #endif /*SAFE_LIB_STR_NULL_SLACK*/ @@ -102,6 +103,7 @@ printf("Test #%d:\n", ++testno); if (rc != ESNULLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -112,6 +114,7 @@ printf("Test #%d:\n", ++testno); if (rc != ESZEROL) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -122,6 +125,7 @@ printf("Test #%d:\n", ++testno); if (rc != ESLEMAX) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -134,6 +138,7 @@ printf("Test #%d:\n", ++testno); if (rc != ESNULLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #ifdef SAFE_LIB_STR_NULL_SLACK @@ -141,12 +146,14 @@ printf("Test #%d:\n", ++testno); if (str1[i] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } } #else if (str1[0] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #endif @@ -158,18 +165,21 @@ printf("Test #%d:\n", ++testno); if (rc != ESZEROL) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #ifdef SAFE_LIB_STR_NULL_SLACK for (i=0; i -%ls-\n", __FUNCTION__, __LINE__, str1, str2); + ++errs; } @@ -284,17 +306,20 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (str1[sz] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } rc = memcmp_s(str1, LEN, str2, (sz+1)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u Error -%ls- <> -%ls-\n", __FUNCTION__, __LINE__, str1, str2); + ++errs; } /*--------------------------------------------------*/ @@ -310,18 +335,21 @@ printf("Test #%d:\n", ++testno); if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #ifdef SAFE_LIB_STR_NULL_SLACK for (i=0; i<5; i++) { if (str1[i] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } } #else if (str1[0] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #endif @@ -335,18 +363,21 @@ printf("Test #%d:\n", ++testno); if (rc != ESOVRLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #ifdef SAFE_LIB_STR_NULL_SLACK for (i=0; i<5; i++) { if (str1[i] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } } #else if (str1[5] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #endif @@ -362,17 +393,20 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (str2[16] == L'\0' && str2[17] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } rc = memcmp_s(str1, LEN, str2, (17)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u Error -%ls- <> -%ls-\n", __FUNCTION__, __LINE__, str1, str2); + ++errs; } @@ -388,17 +422,20 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (str1[sz] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } rc = memcmp_s(str2, LEN, str1, (sz+1)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u Error -%ls- <> -%ls-\n", __FUNCTION__, __LINE__, str2, str1); + ++errs; } /*--------------------------------------------------*/ @@ -414,18 +451,21 @@ printf("Test #%d:\n", ++testno); if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #ifdef SAFE_LIB_STR_NULL_SLACK for (i=0; i<5; i++) { if (str1[i] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } } #else if (str2[0] != L'\0') { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } #endif @@ -442,17 +482,20 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } sz = wcsnlen_s(str1, LEN); if (sz != sz_orig && sz == 14) { printf("%s %u (sz=%lu <> 5) Error rc=%u \n", __FUNCTION__, __LINE__, sz, rc ); + ++errs; } rc = memcmp_s(str1, LEN, str2, (sz)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u -%ls- <> -%ls- (size=%lu) Error rc=%u \n", __FUNCTION__, __LINE__, str1, str2, sz, rc ); + ++errs; } @@ -469,12 +512,14 @@ printf("Test #%d:\n", ++testno); if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } rc = memcmp_s(str1, LEN, str2, (3)*sizeof(wchar_t), &ind ); if (ind != 0) { printf("%s %u -%ls- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } @@ -492,14 +537,16 @@ printf("Test #%d:\n", ++testno); if (rc != ESNOSPC) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } if (*str2 != L'\0') { printf("%s %u -%ls- Error rc=%u \n", __FUNCTION__, __LINE__, str1, rc ); + ++errs; } /*--------------------------------------------------*/ - return (0); + return errs; } diff --git a/unittests/test_wcsnlen_s.c b/unittests/test_wcsnlen_s.c index 755adee..5c16e56 100644 --- a/unittests/test_wcsnlen_s.c +++ b/unittests/test_wcsnlen_s.c @@ -64,6 +64,7 @@ extern errno_t wmemset_s (wchar_t *dest, wchar_t value, rsize_t len); int test_wcsnlen_s (void) { errno_t rc; + int errs = 0; unsigned int testno = 0; @@ -76,6 +77,7 @@ printf("Test #%d:\n", ++testno); if (rc != 0) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -86,6 +88,7 @@ printf("Test #%d:\n", ++testno); if (rc != 0) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -98,6 +101,7 @@ printf("Test #%d:\n", ++testno); if (rc != 40) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -110,6 +114,7 @@ printf("Test #%d:\n", ++testno); if (rc != 20) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -122,6 +127,7 @@ printf("Test #%d:\n", ++testno); if (rc != 0) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } @@ -136,6 +142,7 @@ printf("Test #%d:\n", ++testno); if (rc != 1) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -148,9 +155,10 @@ printf("Test #%d:\n", ++testno); if (rc != 2) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc ); + ++errs; } - return (0); + return errs; } diff --git a/unittests/test_wmemcmp_s.c b/unittests/test_wmemcmp_s.c index 532a569..3617db6 100644 --- a/unittests/test_wmemcmp_s.c +++ b/unittests/test_wmemcmp_s.c @@ -70,9 +70,10 @@ extern errno_t wmemcmp_s (const wchar_t *dest, rsize_t dmax, const wchar_t *src, rsize_t smax, int *diff); -int test_wmemcmp_s() +int test_wmemcmp_s(void) { errno_t rc; + int errs = 0; uint32_t len; int32_t ind; uint32_t i; @@ -91,6 +92,7 @@ int test_wmemcmp_s() if (rc != ESNULLP) { printf("%s %u Ind=%d Error rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -101,6 +103,7 @@ int test_wmemcmp_s() if (rc != ESNULLP) { printf("%s %u Ind=%d Error rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -111,6 +114,7 @@ int test_wmemcmp_s() if (rc != ESNULLP) { printf("%s %u Ind=%d Error rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -121,6 +125,7 @@ int test_wmemcmp_s() if (rc != ESZEROL) { printf("%s %u Ind=%d Error rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -131,6 +136,7 @@ int test_wmemcmp_s() if (rc != ESZEROL) { printf("%s %u Ind=%d Error rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -141,6 +147,7 @@ int test_wmemcmp_s() if (rc != ESLEMAX) { printf("%s %u Ind=%d Error rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -151,6 +158,7 @@ int test_wmemcmp_s() if (rc != ESLEMAX) { printf("%s %u Ind=%d Error rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -165,17 +173,20 @@ int test_wmemcmp_s() if (rc != EOK) { printf("%s %u Ind=%d Error rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } if (ind != 0) { printf("%s %u Ind=%d rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } ind = memcmp(mem1, mem2, len*4); if (ind != 0) { printf("%s %u Ind=%d rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -190,17 +201,20 @@ int test_wmemcmp_s() if (rc != EOK) { printf("%s %u Ind=%d Error rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } if (ind != 0) { printf("%s %u Ind=%d rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } ind = memcmp(mem1, mem2, len*4); if (ind != 0) { printf("%s %u Ind=%d rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -214,17 +228,20 @@ int test_wmemcmp_s() if (rc != EOK) { printf("%s %u Ind=%d Error rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } if (ind != 0) { printf("%s %u Ind=%d rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } ind = memcmp(mem1, mem2, LEN*4); if (ind != 0) { printf("%s %u Ind=%d rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -240,11 +257,13 @@ int test_wmemcmp_s() if (rc != EOK) { printf("%s %u Ind=%d Error rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } if (ind <= 0) { printf("%s %u Ind=%d rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } @@ -261,11 +280,13 @@ int test_wmemcmp_s() if (rc != EOK) { printf("%s %u Ind=%d Error rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } if (ind >= 0) { printf("%s %u Ind=%d rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -281,11 +302,13 @@ int test_wmemcmp_s() if (rc != EOK) { printf("%s %u Ind=%d Error rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } if (ind >= 0) { printf("%s %u Ind=%d rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -301,11 +324,13 @@ int test_wmemcmp_s() if (rc != EOK) { printf("%s %u Ind=%d Error rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } if (ind <= 0) { printf("%s %u Ind=%d rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -321,11 +346,13 @@ int test_wmemcmp_s() if (rc != EOK) { printf("%s %u Ind=%d Error rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } if (ind >= 0) { printf("%s %u Ind=%d rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } /*--------------------------------------------------*/ @@ -341,15 +368,17 @@ int test_wmemcmp_s() if (rc != EOK) { printf("%s %u Ind=%d Error rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } if (ind <= 0) { printf("%s %u Ind=%d rc=%u \n", __FUNCTION__, __LINE__, ind, rc ); + ++errs; } /*--------------------------------------------------*/ /*--------------------------------------------------*/ - return (0); + return errs; } diff --git a/unittests/test_wmemcpy_s.c b/unittests/test_wmemcpy_s.c index 568b616..ed2a83a 100644 --- a/unittests/test_wmemcpy_s.c +++ b/unittests/test_wmemcpy_s.c @@ -69,6 +69,7 @@ extern errno_t wmemcpy_s(wchar_t* dest, rsize_t dmax, const wchar_t* src, rsize_ int test_wmemcpy_s (void) { errno_t rc; + int errs = 0; uint32_t i; rsize_t len; unsigned int testno = 0; @@ -83,6 +84,7 @@ int test_wmemcpy_s (void) if (rc != ESNULLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -94,6 +96,7 @@ int test_wmemcpy_s (void) if (rc != ESZEROL) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -104,6 +107,7 @@ int test_wmemcpy_s (void) if (rc != ESLEMAX) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -114,6 +118,7 @@ int test_wmemcpy_s (void) if (rc != ESNULLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -124,6 +129,7 @@ int test_wmemcpy_s (void) if (rc != ESZEROL) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -134,6 +140,7 @@ int test_wmemcpy_s (void) if (rc != ESLEMAX) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -144,6 +151,7 @@ int test_wmemcpy_s (void) if (rc != ESOVRLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } /*--------------------------------------------------*/ @@ -154,6 +162,7 @@ int test_wmemcpy_s (void) if (rc != ESOVRLP) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } @@ -169,16 +178,19 @@ int test_wmemcpy_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } else { if ( mem1[0] != 40 && mem1[10] != 40) { printf("%d - %d m1[0]=%d m1[10]=%d should be 40 \n", __LINE__, i, mem1[0], mem1[10]); + ++errs; } for (i=1; i<10; i++) { if (mem1[i] != 44) { printf("%d - %d m1=%d should be 44 \n", __LINE__, i, mem1[i]); + ++errs; } } } @@ -195,19 +207,21 @@ int test_wmemcpy_s (void) if (rc == EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); - + ++errs; } else { for (i=0; i m2=%d \n", __LINE__, i, mem1[i], mem2[i]); + ++errs; } } if (mem1[len] != 33 && mem1[len+1] != 33) { printf("%d - %lu m1[len]=%d m1[len+1]=%d \n", __LINE__, len, mem1[len], mem1[len+1]); + ++errs; } } @@ -291,6 +309,7 @@ int test_wmemmove_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } else { /* verify mem1 was copied properly */ @@ -298,6 +317,7 @@ int test_wmemmove_s (void) if (mem1[i] != 44) { printf("%d - %d m1=%d m2=%d \n", __LINE__, i, mem1[i], mem2[i]); + ++errs; } } @@ -315,6 +335,7 @@ int test_wmemmove_s (void) if (rc != EOK) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); + ++errs; } else { /* verify mem1 was copied properly */ @@ -322,18 +343,21 @@ int test_wmemmove_s (void) if (mem1[i] != i) { printf("%d - %d m1=%d m2=%d \n", __LINE__, i, mem1[i], mem2[i]); + ++errs; } } for (i=10; i Date: Sat, 5 May 2018 15:39:00 -0700 Subject: [PATCH 04/18] safestringlib: Add objtest, libsafestring.a and safestringtest to .gitignore Ignore more built items. Signed-off-by: Mark Rustad --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 60bfe41..8ee853d 100644 --- a/.gitignore +++ b/.gitignore @@ -53,6 +53,9 @@ x64/ build/ [Bb]in/ [Oo]bj/ +/libsafestring.a +/objtest +/safestringtest # MSTest test Results [Tt]est[Rr]esult*/ From 7035f8cc25b6244498cadcad051a50bec10fccce Mon Sep 17 00:00:00 2001 From: Mark Rustad Date: Sat, 5 May 2018 15:46:52 -0700 Subject: [PATCH 05/18] safestringlib: Fix bad pointer comparisons in strcasestr_s tests The return values from strcasestr and strcasestr_s were being cast to int for comparison. This is bogus because pointers are only guaranteed to fit in longs. I suspect that this was done due to compiler warnings. Those warnings were because strcasestr was compiled without a prototype because it is _GNU_SOURCE only, and that symbol was not defined. So without the prototype, the return value of strcasestr was an integer and not a pointer. I assume that the warnings were not enabled to reveal the missing prototype. Fix both problems. Signed-off-by: Mark Rustad > --- unittests/test_private.h | 1 + unittests/test_strcasestr_s.c | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/unittests/test_private.h b/unittests/test_private.h index 24f9c99..ca2aa5a 100644 --- a/unittests/test_private.h +++ b/unittests/test_private.h @@ -43,6 +43,7 @@ #else +#define _GNU_SOURCE 1 /* needed to define strcasestr */ #include #include diff --git a/unittests/test_strcasestr_s.c b/unittests/test_strcasestr_s.c index fb5de23..9ac64ea 100644 --- a/unittests/test_strcasestr_s.c +++ b/unittests/test_strcasestr_s.c @@ -13,7 +13,7 @@ extern errno_t strcasestr_s (char *dest, rsize_t dmax, const char *src, rsize_t slen, char **substring); -int test_strcasestr_s() +int test_strcasestr_s(void) { int errs = 0; errno_t rc; @@ -146,7 +146,7 @@ int test_strcasestr_s() /* compare to legacy */ std_sub = strcasestr(str1, str2); - if ((int)sub != (int)std_sub) { // comparison to handle 32-bit library return and 64-bit library return + if (sub != std_sub) { printf("%s %u Error strcasestr_s() does not have same return as strcasestr() when str1 & str2 are zero length strings. rc=%u \n", __FUNCTION__, __LINE__, rc); printf("str1:[%s]\n", str1); @@ -176,7 +176,7 @@ int test_strcasestr_s() /* compare to legacy */ std_sub = strcasestr(str1, str2); - if ((int)sub != (int)std_sub) { // comparison to handle 32-bit library return and 64-bit library return + if (sub != std_sub) { printf("%s %u Error strcasestr_s() does not have same return value as strcasestr() when str2 is zero length string. rc=%u \n", __FUNCTION__, __LINE__, rc); printf("str1:[%s]\n", str1); @@ -207,7 +207,7 @@ int test_strcasestr_s() /* compare to legacy */ std_sub = strcasestr(str1, str2); - if ((int)sub != (int)std_sub) { // comparison to handle 32-bit library return and 64-bit library return + if (sub != std_sub) { printf("%s %u Error strcasestr_s() does not have same return value as strcasestr() when str2 is zero length string. rc=%u \n", __FUNCTION__, __LINE__, rc); printf("str1:[%s]\n", str1); @@ -298,7 +298,7 @@ int test_strcasestr_s() /* compare to legacy */ std_sub = strcasestr(str1, str2); - if ((int)sub != (int)std_sub) { // comparison to handle 32-bit library return and 64-bit library return + if (sub != std_sub) { printf("%s %u Error strcasestr_s() does not have same return value as strcasestr() when str2 is substring of the end of str1. rc=%u \n", __FUNCTION__, __LINE__, rc); printf("str1:[%s]\n", str1); @@ -332,7 +332,7 @@ int test_strcasestr_s() /* compare to legacy */ std_sub = strcasestr(str1, str2); - if ((int)sub != (int)std_sub) { // comparison to handle 32-bit library return and 64-bit library return + if (sub != std_sub) { printf("%s %u Error strcasestr_s() does not have same return value as strcasestr() when str2 is substring of middle of str1. rc=%u \n", __FUNCTION__, __LINE__, rc); printf("str1:[%s]\n", str1); @@ -472,7 +472,7 @@ int test_strcasestr_s() /* compare to legacy */ std_sub = strcasestr(str1, str2); - if ((int)sub != (int)std_sub) { // comparison to handle 32-bit library return and 64-bit library return + if (sub != std_sub) { printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc); ++errs; From 4c51224c03f794b58c6576f39fb205416744ae86 Mon Sep 17 00:00:00 2001 From: Mark Rustad Date: Sat, 5 May 2018 16:24:40 -0700 Subject: [PATCH 06/18] safestringlib: Fix bad casts reported by compiler warnings Casting a char * to a wchar_t makes no sense, especially when the value is supposed to be a wchar_t *. I don't know how this could even be working. Signed-off-by: Mark Rustad --- unittests/test_wmemmove_s.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/unittests/test_wmemmove_s.c b/unittests/test_wmemmove_s.c index eccf16d..d4567be 100644 --- a/unittests/test_wmemmove_s.c +++ b/unittests/test_wmemmove_s.c @@ -433,7 +433,7 @@ int test_wmemmove_s (void) for (i=0; i Date: Sat, 5 May 2018 16:36:04 -0700 Subject: [PATCH 07/18] safestringlib: Add missing includes to resolve warnings Include ctype.h and stdlib.h to get prototypes for functions referenced. This resolves implicit declaration warnings. Signed-off-by: Mark Rustad --- safeclib/safeclib_private.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/safeclib/safeclib_private.h b/safeclib/safeclib_private.h index 7280e87..3967d55 100644 --- a/safeclib/safeclib_private.h +++ b/safeclib/safeclib_private.h @@ -51,6 +51,9 @@ #if HAVE_CONFIG_H #include "config.h" +#else +#include +#include #endif #include From 5358ad3b74a7aaae84d73f19220b0b9cdb3303cb Mon Sep 17 00:00:00 2001 From: Mark Rustad Date: Sat, 5 May 2018 16:57:09 -0700 Subject: [PATCH 08/18] safestringlib: Fix bad check in memcmp32_s test The code is checking the wrong variable when testing memcmp_s. This was found by compiling with -Wall and finding a set but unused variable. Also changed the type of the variable, since memcmp returns an int, not an int32_t. Signed-off-by: Mark Rustad --- unittests/test_memcmp32_s.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/unittests/test_memcmp32_s.c b/unittests/test_memcmp32_s.c index c6bdb20..d6251e7 100644 --- a/unittests/test_memcmp32_s.c +++ b/unittests/test_memcmp32_s.c @@ -15,7 +15,7 @@ int test_memcmp32_s(void) int errs = 0; uint32_t len; int32_t ind; - int32_t std_ind; + int std_ind; uint32_t i; uint32_t mem1[LEN]; @@ -104,9 +104,9 @@ int test_memcmp32_s(void) } std_ind = memcmp(mem1, mem2, len*4); - if (ind != 0) { + if (std_ind != 0) { printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + __FUNCTION__, __LINE__, std_ind, rc); ++errs; } @@ -130,9 +130,9 @@ int test_memcmp32_s(void) } std_ind = memcmp(mem1, mem2, len*4); - if (ind != 0) { + if (std_ind != 0) { printf("%s %u Ind=%d rc=%u \n", - __FUNCTION__, __LINE__, ind, rc); + __FUNCTION__, __LINE__, std_ind, rc); ++errs; } From af3ac9deb539756cf9452e793334d46b481d81d7 Mon Sep 17 00:00:00 2001 From: Mark Rustad Date: Sat, 5 May 2018 17:02:17 -0700 Subject: [PATCH 09/18] safestringlib: Shorten long lines Extremely long lines make patches hard to review. Shorten them by using simply expanded variables and appending to them. These variables have no need for recursive expansion. Signed-off-by: Mark Rustad --- makefile | 53 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/makefile b/makefile index 8a417c6..eb2f960 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,8 @@ IDIR = include MKDIR_P = mkdir -p CC=gcc -CFLAGS=-I$(IDIR) -fstack-protector-strong -fPIE -fPIC -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security +CFLAGS := -I${IDIR} -fstack-protector-strong -fPIE -fPIC -O2 +CFLAGS += -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security LDFLAGS=-z noexecstack -z now ODIR=obj @@ -9,10 +10,11 @@ OTDIR=objtest SRCDIR=safeclib TESTDIR=unittests +_DEPS := safe_lib_errno.h safe_lib.h safe_str_lib.h safe_types.h.in +_DEPS += safe_lib_errno.h.in safe_mem_lib.h safe_types.h -_DEPS = safe_lib_errno.h safe_lib.h safe_str_lib.h safe_types.h.in safe_lib_errno.h.in safe_mem_lib.h safe_types.h - -_ODEPS = mem_primitives_lib.h safeclib_private.h safe_mem_constraint.h safe_str_constraint.h +_ODEPS := mem_primitives_lib.h safeclib_private.h safe_mem_constraint.h +_ODEPS += safe_str_constraint.h all: directories libsafestring.a safestringtest @@ -20,8 +22,23 @@ all: directories libsafestring.a safestringtest DEPS = $(addprefix $(IDIR)/,$(_DEPS)) ODEPS = $(addprefix $(SRCDIR)/,$(_ODEPS)) - -_CLIB = abort_handler_s.c stpcpy_s.c strlastsame_s.c ignore_handler_s.c stpncpy_s.c strljustify_s.c memcmp16_s.c strcasecmp_s.c strncat_s.c memcmp32_s.c strcasestr_s.c strncpy_s.c memcmp_s.c strcat_s.c strnlen_s.c memcpy16_s.c strcmpfld_s.c strnterminate_s.c memcpy32_s.c strcmp_s.c strpbrk_s.c memcpy_s.c strcpyfldin_s.c strprefix_s.c memmove16_s.c strcpyfldout_s.c strremovews_s.c memmove32_s.c strcpyfld_s.c strspn_s.c memmove_s.c strcpy_s.c strstr_s.c mem_primitives_lib.c strcspn_s.c strtok_s.c strfirstchar_s.c strtolowercase_s.c memset16_s.c strfirstdiff_s.c strtouppercase_s.c memset32_s.c strfirstsame_s.c strzero_s.c memset_s.c strisalphanumeric_s.c wcpcpy_s.c memzero16_s.c strisascii_s.c wcscat_s.c memzero32_s.c strisdigit_s.c wcscpy_s.c memzero_s.c strishex_s.c wcsncat_s.c strislowercase_s.c wcsncpy_s.c safe_mem_constraint.c strismixedcase_s.c wcsnlen_s.c strispassword_s.c wmemcmp_s.c safe_str_constraint.c strisuppercase_s.c wmemcpy_s.c strlastchar_s.c wmemmove_s.c snprintf_support.c strlastdiff_s.c wmemset_s.c +_CLIB := abort_handler_s.c stpcpy_s.c strlastsame_s.c ignore_handler_s.c +_CLIB += stpncpy_s.c strljustify_s.c memcmp16_s.c strcasecmp_s.c strncat_s.c +_CLIB += memcmp32_s.c strcasestr_s.c strncpy_s.c memcmp_s.c strcat_s.c +_CLIB += strnlen_s.c memcpy16_s.c strcmpfld_s.c strnterminate_s.c memcpy32_s.c +_CLIB += strcmp_s.c strpbrk_s.c memcpy_s.c strcpyfldin_s.c strprefix_s.c +_CLIB += memmove16_s.c strcpyfldout_s.c strremovews_s.c memmove32_s.c +_CLIB += strcpyfld_s.c strspn_s.c memmove_s.c strcpy_s.c strstr_s.c +_CLIB += mem_primitives_lib.c strcspn_s.c strtok_s.c strfirstchar_s.c +_CLIB += strtolowercase_s.c memset16_s.c strfirstdiff_s.c strtouppercase_s.c +_CLIB += memset32_s.c strfirstsame_s.c strzero_s.c memset_s.c +_CLIB += strisalphanumeric_s.c wcpcpy_s.c memzero16_s.c strisascii_s.c +_CLIB += wcscat_s.c memzero32_s.c strisdigit_s.c wcscpy_s.c memzero_s.c +_CLIB += strishex_s.c wcsncat_s.c strislowercase_s.c wcsncpy_s.c +_CLIB += safe_mem_constraint.c strismixedcase_s.c wcsnlen_s.c +_CLIB += strispassword_s.c wmemcmp_s.c safe_str_constraint.c +_CLIB += strisuppercase_s.c wmemcpy_s.c strlastchar_s.c wmemmove_s.c +_CLIB += snprintf_support.c strlastdiff_s.c wmemset_s.c _TLIST = $(addprefix $(ODIR)/,$(_CLIB)) OBJ = $(patsubst %.c,%.o,$(_TLIST)) @@ -35,8 +52,28 @@ $(ODIR)/%.o: $(SRCDIR)/%.c $(DEPS) $(ODEPS) libsafestring.a: $(OBJ) ar rcs $@ $^ - -_TESTFUNCS = Safe_String_UnitTestMain.c test_strcmp_s.c test_strnlen_s.c test_memcmp16_s.c test_strcpyfldin_s.c test_strnterminate_s.c test_memcmp32_s.c test_strcpyfldout_s.c test_strpbrk_s.c test_memcmp_s.c test_strcpyfld_s.c test_strprefix_s.c test_memcpy16_s.c test_strcpy_s.c test_strremovews_s.c test_memcpy32_s.c test_strcspn_s.c test_strspn_s.c test_memcpy_s.c test_strfirstchar_s.c test_strstr_s.c test_memmove16_s.c test_strfirstdiff_s.c test_strtok_s.c test_memmove32_s.c test_strfirstsame_s.c test_strtolowercase_s.c test_memmove_s.c test_strisalphanumeric_s.c test_strtouppercase_s.c test_memset16_s.c test_strisascii_s.c test_strzero_s.c test_memset32_s.c test_strisdigit_s.c test_wcpcpy_s.c test_memset_s.c test_strishex_s.c test_wcscat_s.c test_memzero16_s.c test_strislowercase_s.c test_wcscpy_s.c test_memzero32_s.c test_strismixed_s.c test_wcsncat_s.c test_memzero_s.c test_strispassword_s.c test_wcsncpy_s.c test_strisuppercase_s.c test_wcsnlen_s.c test_stpcpy_s.c test_strlastchar_s.c test_wmemcmp_s.c test_stpncpy_s.c test_strlastdiff_s.c test_wmemcpy_s.c test_strcasecmp_s.c test_strlastsame_s.c test_wmemmove_s.c test_strcasestr_s.c test_strljustify_s.c test_wmemset_s.c test_strcat_s.c test_strncat_s.c test_strcmpfld_s.c test_strncpy_s.c +_TESTFUNCS := Safe_String_UnitTestMain.c test_strcmp_s.c test_strnlen_s.c +_TESTFUNCS += test_memcmp16_s.c test_strcpyfldin_s.c test_strnterminate_s.c +_TESTFUNCS += test_memcmp32_s.c test_strcpyfldout_s.c test_strpbrk_s.c +_TESTFUNCS += test_memcmp_s.c test_strcpyfld_s.c test_strprefix_s.c +_TESTFUNCS += test_memcpy16_s.c test_strcpy_s.c test_strremovews_s.c +_TESTFUNCS += test_memcpy32_s.c test_strcspn_s.c test_strspn_s.c +_TESTFUNCS += test_memcpy_s.c test_strfirstchar_s.c test_strstr_s.c +_TESTFUNCS += test_memmove16_s.c test_strfirstdiff_s.c test_strtok_s.c +_TESTFUNCS += test_memmove32_s.c test_strfirstsame_s.c test_strtolowercase_s.c +_TESTFUNCS += test_memmove_s.c test_strisalphanumeric_s.c +_TESTFUNCS += test_strtouppercase_s.c test_memset16_s.c test_strisascii_s.c +_TESTFUNCS += test_strzero_s.c test_memset32_s.c test_strisdigit_s.c +_TESTFUNCS += test_wcpcpy_s.c test_memset_s.c test_strishex_s.c test_wcscat_s.c +_TESTFUNCS += test_memzero16_s.c test_strislowercase_s.c test_wcscpy_s.c +_TESTFUNCS += test_memzero32_s.c test_strismixed_s.c test_wcsncat_s.c +_TESTFUNCS += test_memzero_s.c test_strispassword_s.c test_wcsncpy_s.c +_TESTFUNCS += test_strisuppercase_s.c test_wcsnlen_s.c test_stpcpy_s.c +_TESTFUNCS += test_strlastchar_s.c test_wmemcmp_s.c test_stpncpy_s.c +_TESTFUNCS += test_strlastdiff_s.c test_wmemcpy_s.c test_strcasecmp_s.c +_TESTFUNCS += test_strlastsame_s.c test_wmemmove_s.c test_strcasestr_s.c +_TESTFUNCS += test_strljustify_s.c test_wmemset_s.c test_strcat_s.c +_TESTFUNCS += test_strncat_s.c test_strcmpfld_s.c test_strncpy_s.c _TLIST2 = $(addprefix $(OTDIR)/,$(_TESTFUNCS)) TOBJ = $(patsubst %.c,%.o,$(_TLIST2)) From bf361f4f72df127a5c4319d9eda3df585c5546cf Mon Sep 17 00:00:00 2001 From: Mark Rustad Date: Sat, 5 May 2018 17:04:43 -0700 Subject: [PATCH 10/18] safestringlib: Use compiler-generated dependencies instead of a fixed list The compiler generates perfectly good dependencies. Use them. Signed-off-by: Mark Rustad --- makefile | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/makefile b/makefile index eb2f960..0a57977 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,7 @@ IDIR = include MKDIR_P = mkdir -p CC=gcc -CFLAGS := -I${IDIR} -fstack-protector-strong -fPIE -fPIC -O2 +CFLAGS := -I${IDIR} -fstack-protector-strong -fPIE -fPIC -O2 -MMD CFLAGS += -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security LDFLAGS=-z noexecstack -z now @@ -10,18 +10,8 @@ OTDIR=objtest SRCDIR=safeclib TESTDIR=unittests -_DEPS := safe_lib_errno.h safe_lib.h safe_str_lib.h safe_types.h.in -_DEPS += safe_lib_errno.h.in safe_mem_lib.h safe_types.h - -_ODEPS := mem_primitives_lib.h safeclib_private.h safe_mem_constraint.h -_ODEPS += safe_str_constraint.h - all: directories libsafestring.a safestringtest - -DEPS = $(addprefix $(IDIR)/,$(_DEPS)) -ODEPS = $(addprefix $(SRCDIR)/,$(_ODEPS)) - _CLIB := abort_handler_s.c stpcpy_s.c strlastsame_s.c ignore_handler_s.c _CLIB += stpncpy_s.c strljustify_s.c memcmp16_s.c strcasecmp_s.c strncat_s.c _CLIB += memcmp32_s.c strcasestr_s.c strncpy_s.c memcmp_s.c strcat_s.c @@ -45,8 +35,7 @@ OBJ = $(patsubst %.c,%.o,$(_TLIST)) CLIB =$(addprefix $(SRCDIR)/,$(_CLIB)) - -$(ODIR)/%.o: $(SRCDIR)/%.c $(DEPS) $(ODEPS) +${ODIR}/%.o: ${SRCDIR}/%.c $(CC) $(LDFLAGS) -c -o $@ $< $(CFLAGS) libsafestring.a: $(OBJ) @@ -101,6 +90,9 @@ ${OTDIR}: .PHONY: clean clean: - rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~ $(OTDIR)/*.o + rm -f ${ODIR}/* *~ core ${INCDIR}/*~ ${OTDIR}/* rm -f libsafestring.a rm -f safestringtest + +-include $(wildcard ${ODIR}/*.d) +-include $(wildcard ${OTDIR}/*.d) From fcc740ae4f917d3eb1749589ea4f68b522b9a16a Mon Sep 17 00:00:00 2001 From: Mark Rustad Date: Sat, 5 May 2018 21:09:25 -0700 Subject: [PATCH 11/18] safestringlib: Fix used but uninitialized variables Turning on -Wall revealed some uninitialized variables, so fix them. Signed-off-by: Mark Rustad --- unittests/test_strcpyfld_s.c | 1 + unittests/test_strcpyfldout_s.c | 1 + 2 files changed, 2 insertions(+) diff --git a/unittests/test_strcpyfld_s.c b/unittests/test_strcpyfld_s.c index 97b2bfd..fbab232 100644 --- a/unittests/test_strcpyfld_s.c +++ b/unittests/test_strcpyfld_s.c @@ -25,6 +25,7 @@ int test_strcpyfld_s(void) /*--------------------------------------------------*/ + slen = sizeof(str2); rc = strcpyfld_s(NULL, LEN, str2, slen); if (rc != ESNULLP) { printf("%s %u Error rc=%u \n", diff --git a/unittests/test_strcpyfldout_s.c b/unittests/test_strcpyfldout_s.c index 2bd7211..e3c1e31 100644 --- a/unittests/test_strcpyfldout_s.c +++ b/unittests/test_strcpyfldout_s.c @@ -25,6 +25,7 @@ int test_strcpyfldout_s(void) /*--------------------------------------------------*/ + slen = sizeof(str2); rc = strcpyfldout_s(NULL, LEN, str2, slen); if (rc != ESNULLP) { printf("%s %u Error rc=%u \n", From c1c77640f244ef06a6d071c8b184d0216d6c4b76 Mon Sep 17 00:00:00 2001 From: Mark Rustad Date: Sat, 5 May 2018 21:13:52 -0700 Subject: [PATCH 12/18] safestringlib: Turn on -Wall and resolve remaining warning Turn on -Wall and resolve the remaining warning. This involved eliminating a set but unused variable. Signed-off-by: Mark Rustad --- makefile | 2 +- unittests/Safe_String_UnitTestMain.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/makefile b/makefile index 0a57977..380e9fd 100644 --- a/makefile +++ b/makefile @@ -2,7 +2,7 @@ IDIR = include MKDIR_P = mkdir -p CC=gcc CFLAGS := -I${IDIR} -fstack-protector-strong -fPIE -fPIC -O2 -MMD -CFLAGS += -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security +CFLAGS += -D_FORTIFY_SOURCE=2 -Wall -Wformat -Wformat-security LDFLAGS=-z noexecstack -z now ODIR=obj diff --git a/unittests/Safe_String_UnitTestMain.c b/unittests/Safe_String_UnitTestMain.c index 04fceae..50428fd 100644 --- a/unittests/Safe_String_UnitTestMain.c +++ b/unittests/Safe_String_UnitTestMain.c @@ -178,7 +178,7 @@ int main(void) { int indicator = -1; strcpy_s(dest, dmax, str); - char *ret = stpcpy_s(&dest[20], 108, str2, &err); + stpcpy_s(&dest[20], 108, str2, &err); if (err != EOK) { puts("stpcpy_s returned failure"); ++errs; From 01ab7ddd99edaa202c150c90619813719d6e51c3 Mon Sep 17 00:00:00 2001 From: Mark Rustad Date: Sat, 5 May 2018 21:19:45 -0700 Subject: [PATCH 13/18] safestringlib: Add -Wextra and resolve the warnings Add the -Wextra flag and resolve the resulting warnings. These include marking unused parameters as unused, adding fallthrough comments switch statement cases that fall through and resolving sign comparison differences. Signed-off-by: Mark Rustad --- makefile | 2 +- safeclib/abort_handler_s.c | 2 +- safeclib/ignore_handler_s.c | 9 +- safeclib/mem_primitives_lib.c | 270 ++++++++++++++++----------------- safeclib/safe_str_constraint.h | 2 + safeclib/safeclib_private.h | 6 + unittests/test_strcspn_s.c | 8 +- unittests/test_strspn_s.c | 12 +- unittests/test_wmemmove_s.c | 4 +- 9 files changed, 165 insertions(+), 150 deletions(-) diff --git a/makefile b/makefile index 380e9fd..264f3f1 100644 --- a/makefile +++ b/makefile @@ -2,7 +2,7 @@ IDIR = include MKDIR_P = mkdir -p CC=gcc CFLAGS := -I${IDIR} -fstack-protector-strong -fPIE -fPIC -O2 -MMD -CFLAGS += -D_FORTIFY_SOURCE=2 -Wall -Wformat -Wformat-security +CFLAGS += -D_FORTIFY_SOURCE=2 -Wall -Wextra -Wformat -Wformat-security LDFLAGS=-z noexecstack -z now ODIR=obj diff --git a/safeclib/abort_handler_s.c b/safeclib/abort_handler_s.c index d956bfd..97e11bd 100644 --- a/safeclib/abort_handler_s.c +++ b/safeclib/abort_handler_s.c @@ -65,7 +65,7 @@ * */ -void abort_handler_s(const char *msg, void *ptr, errno_t error) +void abort_handler_s(const char *msg, _UNUSED void *ptr, errno_t error) { slprintf("ABORT CONSTRAINT HANDLER: (%u) %s\n", error, (msg) ? msg : "Null message"); diff --git a/safeclib/ignore_handler_s.c b/safeclib/ignore_handler_s.c index 412e0cb..3e9b8c1 100644 --- a/safeclib/ignore_handler_s.c +++ b/safeclib/ignore_handler_s.c @@ -31,6 +31,12 @@ #include "safeclib_private.h" +#ifdef DEBUG +#define _DUNUSED +#else +#define _DUNUSED _UNUSED +#endif /* DEBUG */ + /** * NAME * ignore_handler_s @@ -62,7 +68,8 @@ * */ -void ignore_handler_s(const char *msg, void *ptr, errno_t error) +void ignore_handler_s(_DUNUSED const char *msg, _UNUSED void *ptr, + _DUNUSED errno_t error) { sldebug_printf("IGNORE CONSTRAINT HANDLER: (%u) %s\n", error, diff --git a/safeclib/mem_primitives_lib.c b/safeclib/mem_primitives_lib.c index cc189e5..285ae91 100644 --- a/safeclib/mem_primitives_lib.c +++ b/safeclib/mem_primitives_lib.c @@ -108,21 +108,21 @@ mem_prim_set (void *dest, uint32_t len, uint8_t value) lcount -= 16; break; - case 15: *lp++ = value32; - case 14: *lp++ = value32; - case 13: *lp++ = value32; - case 12: *lp++ = value32; - case 11: *lp++ = value32; - case 10: *lp++ = value32; - case 9: *lp++ = value32; - case 8: *lp++ = value32; - - case 7: *lp++ = value32; - case 6: *lp++ = value32; - case 5: *lp++ = value32; - case 4: *lp++ = value32; - case 3: *lp++ = value32; - case 2: *lp++ = value32; + case 15: *lp++ = value32; /* Fallthrough */ + case 14: *lp++ = value32; /* Fallthrough */ + case 13: *lp++ = value32; /* Fallthrough */ + case 12: *lp++ = value32; /* Fallthrough */ + case 11: *lp++ = value32; /* Fallthrough */ + case 10: *lp++ = value32; /* Fallthrough */ + case 9: *lp++ = value32; /* Fallthrough */ + case 8: *lp++ = value32; /* Fallthrough */ + + case 7: *lp++ = value32; /* Fallthrough */ + case 6: *lp++ = value32; /* Fallthrough */ + case 5: *lp++ = value32; /* Fallthrough */ + case 4: *lp++ = value32; /* Fallthrough */ + case 3: *lp++ = value32; /* Fallthrough */ + case 2: *lp++ = value32; /* Fallthrough */ case 1: *lp++ = value32; lcount = 0; break; @@ -194,21 +194,21 @@ mem_prim_set16 (uint16_t *dp, uint32_t len, uint16_t value) len -= 16; break; - case 15: *dp++ = value; - case 14: *dp++ = value; - case 13: *dp++ = value; - case 12: *dp++ = value; - case 11: *dp++ = value; - case 10: *dp++ = value; - case 9: *dp++ = value; - case 8: *dp++ = value; - - case 7: *dp++ = value; - case 6: *dp++ = value; - case 5: *dp++ = value; - case 4: *dp++ = value; - case 3: *dp++ = value; - case 2: *dp++ = value; + case 15: *dp++ = value; /* Fallthrough */ + case 14: *dp++ = value; /* Fallthrough */ + case 13: *dp++ = value; /* Fallthrough */ + case 12: *dp++ = value; /* Fallthrough */ + case 11: *dp++ = value; /* Fallthrough */ + case 10: *dp++ = value; /* Fallthrough */ + case 9: *dp++ = value; /* Fallthrough */ + case 8: *dp++ = value; /* Fallthrough */ + + case 7: *dp++ = value; /* Fallthrough */ + case 6: *dp++ = value; /* Fallthrough */ + case 5: *dp++ = value; /* Fallthrough */ + case 4: *dp++ = value; /* Fallthrough */ + case 3: *dp++ = value; /* Fallthrough */ + case 2: *dp++ = value; /* Fallthrough */ case 1: *dp++ = value; len = 0; break; @@ -265,21 +265,21 @@ mem_prim_set32 (uint32_t *dp, uint32_t len, uint32_t value) len -= 16; break; - case 15: *dp++ = value; - case 14: *dp++ = value; - case 13: *dp++ = value; - case 12: *dp++ = value; - case 11: *dp++ = value; - case 10: *dp++ = value; - case 9: *dp++ = value; - case 8: *dp++ = value; - - case 7: *dp++ = value; - case 6: *dp++ = value; - case 5: *dp++ = value; - case 4: *dp++ = value; - case 3: *dp++ = value; - case 2: *dp++ = value; + case 15: *dp++ = value; /* Fallthrough */ + case 14: *dp++ = value; /* Fallthrough */ + case 13: *dp++ = value; /* Fallthrough */ + case 12: *dp++ = value; /* Fallthrough */ + case 11: *dp++ = value; /* Fallthrough */ + case 10: *dp++ = value; /* Fallthrough */ + case 9: *dp++ = value; /* Fallthrough */ + case 8: *dp++ = value; /* Fallthrough */ + + case 7: *dp++ = value; /* Fallthrough */ + case 6: *dp++ = value; /* Fallthrough */ + case 5: *dp++ = value; /* Fallthrough */ + case 4: *dp++ = value; /* Fallthrough */ + case 3: *dp++ = value; /* Fallthrough */ + case 2: *dp++ = value; /* Fallthrough */ case 1: *dp++ = value; len = 0; break; @@ -518,21 +518,21 @@ mem_prim_move8 (uint8_t *dp, const uint8_t *sp, uint32_t len) len -= 16; break; - case 15: *dp++ = *sp++; - case 14: *dp++ = *sp++; - case 13: *dp++ = *sp++; - case 12: *dp++ = *sp++; - case 11: *dp++ = *sp++; - case 10: *dp++ = *sp++; - case 9: *dp++ = *sp++; - case 8: *dp++ = *sp++; - - case 7: *dp++ = *sp++; - case 6: *dp++ = *sp++; - case 5: *dp++ = *sp++; - case 4: *dp++ = *sp++; - case 3: *dp++ = *sp++; - case 2: *dp++ = *sp++; + case 15: *dp++ = *sp++; /* Fallthrough */ + case 14: *dp++ = *sp++; /* Fallthrough */ + case 13: *dp++ = *sp++; /* Fallthrough */ + case 12: *dp++ = *sp++; /* Fallthrough */ + case 11: *dp++ = *sp++; /* Fallthrough */ + case 10: *dp++ = *sp++; /* Fallthrough */ + case 9: *dp++ = *sp++; /* Fallthrough */ + case 8: *dp++ = *sp++; /* Fallthrough */ + + case 7: *dp++ = *sp++; /* Fallthrough */ + case 6: *dp++ = *sp++; /* Fallthrough */ + case 5: *dp++ = *sp++; /* Fallthrough */ + case 4: *dp++ = *sp++; /* Fallthrough */ + case 3: *dp++ = *sp++; /* Fallthrough */ + case 2: *dp++ = *sp++; /* Fallthrough */ case 1: *dp++ = *sp++; len = 0; break; @@ -568,21 +568,21 @@ mem_prim_move8 (uint8_t *dp, const uint8_t *sp, uint32_t len) len -= 16; break; - case 15: *--dp = *--sp; - case 14: *--dp = *--sp; - case 13: *--dp = *--sp; - case 12: *--dp = *--sp; - case 11: *--dp = *--sp; - case 10: *--dp = *--sp; - case 9: *--dp = *--sp; - case 8: *--dp = *--sp; - - case 7: *--dp = *--sp; - case 6: *--dp = *--sp; - case 5: *--dp = *--sp; - case 4: *--dp = *--sp; - case 3: *--dp = *--sp; - case 2: *--dp = *--sp; + case 15: *--dp = *--sp; /* Fallthrough */ + case 14: *--dp = *--sp; /* Fallthrough */ + case 13: *--dp = *--sp; /* Fallthrough */ + case 12: *--dp = *--sp; /* Fallthrough */ + case 11: *--dp = *--sp; /* Fallthrough */ + case 10: *--dp = *--sp; /* Fallthrough */ + case 9: *--dp = *--sp; /* Fallthrough */ + case 8: *--dp = *--sp; /* Fallthrough */ + + case 7: *--dp = *--sp; /* Fallthrough */ + case 6: *--dp = *--sp; /* Fallthrough */ + case 5: *--dp = *--sp; /* Fallthrough */ + case 4: *--dp = *--sp; /* Fallthrough */ + case 3: *--dp = *--sp; /* Fallthrough */ + case 2: *--dp = *--sp; /* Fallthrough */ case 1: *--dp = *--sp; len = 0; break; @@ -649,21 +649,21 @@ mem_prim_move16 (uint16_t *dp, const uint16_t *sp, uint32_t len) len -= 16; break; - case 15: *dp++ = *sp++; - case 14: *dp++ = *sp++; - case 13: *dp++ = *sp++; - case 12: *dp++ = *sp++; - case 11: *dp++ = *sp++; - case 10: *dp++ = *sp++; - case 9: *dp++ = *sp++; - case 8: *dp++ = *sp++; - - case 7: *dp++ = *sp++; - case 6: *dp++ = *sp++; - case 5: *dp++ = *sp++; - case 4: *dp++ = *sp++; - case 3: *dp++ = *sp++; - case 2: *dp++ = *sp++; + case 15: *dp++ = *sp++; /* Fallthrough */ + case 14: *dp++ = *sp++; /* Fallthrough */ + case 13: *dp++ = *sp++; /* Fallthrough */ + case 12: *dp++ = *sp++; /* Fallthrough */ + case 11: *dp++ = *sp++; /* Fallthrough */ + case 10: *dp++ = *sp++; /* Fallthrough */ + case 9: *dp++ = *sp++; /* Fallthrough */ + case 8: *dp++ = *sp++; /* Fallthrough */ + + case 7: *dp++ = *sp++; /* Fallthrough */ + case 6: *dp++ = *sp++; /* Fallthrough */ + case 5: *dp++ = *sp++; /* Fallthrough */ + case 4: *dp++ = *sp++; /* Fallthrough */ + case 3: *dp++ = *sp++; /* Fallthrough */ + case 2: *dp++ = *sp++; /* Fallthrough */ case 1: *dp++ = *sp++; len = 0; break; @@ -698,21 +698,21 @@ mem_prim_move16 (uint16_t *dp, const uint16_t *sp, uint32_t len) len -= 16; break; - case 15: *--dp = *--sp; - case 14: *--dp = *--sp; - case 13: *--dp = *--sp; - case 12: *--dp = *--sp; - case 11: *--dp = *--sp; - case 10: *--dp = *--sp; - case 9: *--dp = *--sp; - case 8: *--dp = *--sp; - - case 7: *--dp = *--sp; - case 6: *--dp = *--sp; - case 5: *--dp = *--sp; - case 4: *--dp = *--sp; - case 3: *--dp = *--sp; - case 2: *--dp = *--sp; + case 15: *--dp = *--sp; /* Fallthrough */ + case 14: *--dp = *--sp; /* Fallthrough */ + case 13: *--dp = *--sp; /* Fallthrough */ + case 12: *--dp = *--sp; /* Fallthrough */ + case 11: *--dp = *--sp; /* Fallthrough */ + case 10: *--dp = *--sp; /* Fallthrough */ + case 9: *--dp = *--sp; /* Fallthrough */ + case 8: *--dp = *--sp; /* Fallthrough */ + + case 7: *--dp = *--sp; /* Fallthrough */ + case 6: *--dp = *--sp; /* Fallthrough */ + case 5: *--dp = *--sp; /* Fallthrough */ + case 4: *--dp = *--sp; /* Fallthrough */ + case 3: *--dp = *--sp; /* Fallthrough */ + case 2: *--dp = *--sp; /* Fallthrough */ case 1: *--dp = *--sp; len = 0; break; @@ -779,21 +779,21 @@ mem_prim_move32 (uint32_t *dp, const uint32_t *sp, uint32_t len) len -= 16; break; - case 15: *dp++ = *sp++; - case 14: *dp++ = *sp++; - case 13: *dp++ = *sp++; - case 12: *dp++ = *sp++; - case 11: *dp++ = *sp++; - case 10: *dp++ = *sp++; - case 9: *dp++ = *sp++; - case 8: *dp++ = *sp++; - - case 7: *dp++ = *sp++; - case 6: *dp++ = *sp++; - case 5: *dp++ = *sp++; - case 4: *dp++ = *sp++; - case 3: *dp++ = *sp++; - case 2: *dp++ = *sp++; + case 15: *dp++ = *sp++; /* Fallthrough */ + case 14: *dp++ = *sp++; /* Fallthrough */ + case 13: *dp++ = *sp++; /* Fallthrough */ + case 12: *dp++ = *sp++; /* Fallthrough */ + case 11: *dp++ = *sp++; /* Fallthrough */ + case 10: *dp++ = *sp++; /* Fallthrough */ + case 9: *dp++ = *sp++; /* Fallthrough */ + case 8: *dp++ = *sp++; /* Fallthrough */ + + case 7: *dp++ = *sp++; /* Fallthrough */ + case 6: *dp++ = *sp++; /* Fallthrough */ + case 5: *dp++ = *sp++; /* Fallthrough */ + case 4: *dp++ = *sp++; /* Fallthrough */ + case 3: *dp++ = *sp++; /* Fallthrough */ + case 2: *dp++ = *sp++; /* Fallthrough */ case 1: *dp++ = *sp++; len = 0; break; @@ -827,21 +827,21 @@ mem_prim_move32 (uint32_t *dp, const uint32_t *sp, uint32_t len) len -= 16; break; - case 15: *--dp = *--sp; - case 14: *--dp = *--sp; - case 13: *--dp = *--sp; - case 12: *--dp = *--sp; - case 11: *--dp = *--sp; - case 10: *--dp = *--sp; - case 9: *--dp = *--sp; - case 8: *--dp = *--sp; - - case 7: *--dp = *--sp; - case 6: *--dp = *--sp; - case 5: *--dp = *--sp; - case 4: *--dp = *--sp; - case 3: *--dp = *--sp; - case 2: *--dp = *--sp; + case 15: *--dp = *--sp; /* Fallthrough */ + case 14: *--dp = *--sp; /* Fallthrough */ + case 13: *--dp = *--sp; /* Fallthrough */ + case 12: *--dp = *--sp; /* Fallthrough */ + case 11: *--dp = *--sp; /* Fallthrough */ + case 10: *--dp = *--sp; /* Fallthrough */ + case 9: *--dp = *--sp; /* Fallthrough */ + case 8: *--dp = *--sp; /* Fallthrough */ + + case 7: *--dp = *--sp; /* Fallthrough */ + case 6: *--dp = *--sp; /* Fallthrough */ + case 5: *--dp = *--sp; /* Fallthrough */ + case 4: *--dp = *--sp; /* Fallthrough */ + case 3: *--dp = *--sp; /* Fallthrough */ + case 2: *--dp = *--sp; /* Fallthrough */ case 1: *--dp = *--sp; len = 0; break; diff --git a/safeclib/safe_str_constraint.h b/safeclib/safe_str_constraint.h index a1fba3e..134401e 100644 --- a/safeclib/safe_str_constraint.h +++ b/safeclib/safe_str_constraint.h @@ -54,6 +54,7 @@ static inline void handle_error(char *orig_dest, rsize_t orig_dmax, /* null string to eliminate partial copy */ while (orig_dmax) { *orig_dest = '\0'; orig_dmax--; orig_dest++; } #else + orig_dmax = orig_dmax; *orig_dest = '\0'; #endif @@ -68,6 +69,7 @@ static inline void handle_wc_error(wchar_t *orig_dest, rsize_t orig_dmax, /* null string to eliminate partial copy */ while (orig_dmax) { *orig_dest = L'\0'; orig_dmax--; orig_dest++; } #else + orig_dmax = orig_dmax; *orig_dest = L'\0'; #endif diff --git a/safeclib/safeclib_private.h b/safeclib/safeclib_private.h index 3967d55..cc4f160 100644 --- a/safeclib/safeclib_private.h +++ b/safeclib/safeclib_private.h @@ -32,6 +32,12 @@ #ifndef __SAFECLIB_PRIVATE_H__ #define __SAFECLIB_PRIVATE_H__ +#ifdef __GNUC__ +#define _UNUSED __attribute__((__unused__)) +#else +#define _UNUSED +#endif /* __GNUC__ */ + #ifdef __KERNEL__ /* linux kernel environment */ diff --git a/unittests/test_strcspn_s.c b/unittests/test_strcspn_s.c index 87fc064..283dc46 100644 --- a/unittests/test_strcspn_s.c +++ b/unittests/test_strcspn_s.c @@ -158,7 +158,7 @@ printf("Test #%d: NULL Scan String Check\n", ++testno); } std_count = strcspn(str1, str2); - if (count != std_count) { + if (count != (rsize_t)std_count) { printf("%s %u count=%zu std_count=%d rc=%d \n", __FUNCTION__, __LINE__, count, std_count, rc); ++errs; @@ -286,7 +286,7 @@ printf("Test #%d: NULL Scan String Check\n", ++testno); } std_count = strcspn(str1, str2); - if (count != std_count) { + if (count != (rsize_t)std_count) { printf("%s %u count=%zu std_count=%d rc=%d \n", __FUNCTION__, __LINE__, count, std_count, rc); ++errs; @@ -308,7 +308,7 @@ printf("Test #%d: NULL Scan String Check\n", ++testno); } std_count = strcspn(str1, str2); - if (count != std_count) { + if (count != (rsize_t)std_count) { printf("%s %u count=%zu std_count=%d rc=%d \n", __FUNCTION__, __LINE__, count, std_count, rc); ++errs; @@ -330,7 +330,7 @@ printf("Test #%d: NULL Scan String Check\n", ++testno); } std_count = strcspn(str1, str2); - if (count != std_count) { + if (count != (rsize_t)std_count) { printf("%s %u count=%zu std_count=%d rc=%d \n", __FUNCTION__, __LINE__, count, std_count, rc); ++errs; diff --git a/unittests/test_strspn_s.c b/unittests/test_strspn_s.c index 2bef05c..05098d3 100644 --- a/unittests/test_strspn_s.c +++ b/unittests/test_strspn_s.c @@ -206,7 +206,7 @@ int test_strspn_s (void) } std_count = strspn(str1, str2); - if (count != std_count) { + if (count != (rsize_t)std_count) { printf("%s %u count=%zu std_count=%d rc=%d \n", __FUNCTION__, __LINE__, count, std_count, rc); ++errs; @@ -225,7 +225,7 @@ int test_strspn_s (void) } std_count = strspn(str1, str2); - if (count != std_count) { + if (count != (rsize_t)std_count) { printf("%s %u count=%zu std_count=%d rc=%d \n", __FUNCTION__, __LINE__, count, std_count, rc); ++errs; @@ -245,7 +245,7 @@ int test_strspn_s (void) } std_count = strspn(str1, str2); - if (count != std_count) { + if (count != (rsize_t)std_count) { printf("%s %u count=%zu std_count=%d rc=%d \n", __FUNCTION__, __LINE__, count, std_count, rc); ++errs; @@ -264,7 +264,7 @@ int test_strspn_s (void) } std_count = strspn(str1, str2); - if (count != std_count) { + if (count != (rsize_t)std_count) { printf("%s %u count=%zu std_count=%d rc=%d \n", __FUNCTION__, __LINE__, count, std_count, rc); ++errs; @@ -283,7 +283,7 @@ int test_strspn_s (void) } std_count = strspn(str1, str2); - if (count != std_count) { + if (count != (rsize_t)std_count) { printf("%s %u count=%zu std_count=%d rc=%d \n", __FUNCTION__, __LINE__, count, std_count, rc); ++errs; @@ -302,7 +302,7 @@ int test_strspn_s (void) } std_count = strspn(str1, str2); - if (count != std_count) { + if (count != (rsize_t)std_count) { printf("%s %u count=%zu std_count=%d rc=%d \n", __FUNCTION__, __LINE__, count, std_count, rc); ++errs; diff --git a/unittests/test_wmemmove_s.c b/unittests/test_wmemmove_s.c index d4567be..345ee6b 100644 --- a/unittests/test_wmemmove_s.c +++ b/unittests/test_wmemmove_s.c @@ -340,14 +340,14 @@ int test_wmemmove_s (void) /* verify mem1 was copied properly */ for (i=0; i<10; i++) { - if (mem1[i] != i) { + if ((uint32_t)mem1[i] != i) { printf("%d - %d m1=%d m2=%d \n", __LINE__, i, mem1[i], mem2[i]); ++errs; } } for (i=10; i Date: Sat, 5 May 2018 21:27:23 -0700 Subject: [PATCH 14/18] safestringlib: Function prototypes belong in a common header Function prototypes belong in a common header so that there is some check that the caller and callee are compatible. Move extern declarations out of the .c and into the private .h. Make them proper prototypes at the same time. Signed-off-by: Mark Rustad --- unittests/Safe_String_UnitTestMain.c | 67 +--------------------------- unittests/test_private.h | 66 +++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 66 deletions(-) diff --git a/unittests/Safe_String_UnitTestMain.c b/unittests/Safe_String_UnitTestMain.c index 50428fd..e40f0cc 100644 --- a/unittests/Safe_String_UnitTestMain.c +++ b/unittests/Safe_String_UnitTestMain.c @@ -12,72 +12,7 @@ #include #include #include - -extern int test_memcmp_s(); -extern int test_memcmp16_s(); -extern int test_memcmp32_s(); -extern int test_memcpy_s(); -extern int test_memcpy16_s(); -extern int test_memcpy32_s(); -extern int test_memmove_s(); -extern int test_memmove16_s(); -extern int test_memmove32_s(); -extern int test_memset_s(); -extern int test_memset16_s(); -extern int test_memset32_s(); -extern int test_memzero_s(); -extern int test_memzero16_s(); -extern int test_memzero32_s(); -extern int test_strcasecmp_s(); -extern int test_strcasestr_s(); -extern int test_strcat_s(); -extern int test_strcmp_s(); -extern int test_strcmpfld_s(); -extern int test_strcpy_s(); -extern int test_strcpyfld_s(); -extern int test_strcpyfldin_s(); -extern int test_strcpyfldout_s(); -extern int test_strcspn_s (); -extern int test_strfirstchar_s(); -extern int test_strfirstdiff_s(); -extern int test_strfirstsame_s(); -extern int test_strisalphanumeric_s(); -extern int test_strisascii_s(); -extern int test_strisdigit_s(); -extern int test_strishex_s(); -extern int test_strislowercase_s(); -extern int test_strismixed_s(); -extern int test_strispassword_s(); -extern int test_strisuppercase_s(); -extern int test_strlastchar_s(); -extern int test_strlastdiff_s(); -extern int test_strlastsame_s(); -extern int test_strljustify_s(); -extern int test_strncat_s (); -extern int test_strncpy_s (); -extern int test_strnlen_s (); -extern int test_strnterminate_s(); -extern int test_strpbrk_s (); -extern int test_strprefix_s(); -extern int test_strremovews_s(); -extern int test_strspn_s (); -extern int test_strstr_s (); -extern int test_strtok_s(); -extern int test_strtolowercase_s(); -extern int test_strtouppercase_s(); -extern int test_strzero_s(); -extern int test_stpncpy_s(); -extern int test_stpcpy_s(); -extern int test_wcpcpy_s(); -extern int test_wcscat_s(); -extern int test_wcscpy_s(); -extern int test_wcsncat_s(); -extern int test_wcsncpy_s (void); -extern int test_wcsnlen_s (void); -extern int test_wmemcpy_s(void); -extern int test_wmemmove_s(void); -extern int test_wmemset_s(void); -extern int test_wmemcmp_s(void); +#include "test_private.h" #define TEST_FUNC(f) { #f, f } diff --git a/unittests/test_private.h b/unittests/test_private.h index ca2aa5a..294f59c 100644 --- a/unittests/test_private.h +++ b/unittests/test_private.h @@ -59,4 +59,70 @@ #define debug_printf(...) #endif +int test_memcmp_s(void); +int test_memcmp16_s(void); +int test_memcmp32_s(void); +int test_memcpy_s(void); +int test_memcpy16_s(void); +int test_memcpy32_s(void); +int test_memmove_s(void); +int test_memmove16_s(void); +int test_memmove32_s(void); +int test_memset_s(void); +int test_memset16_s(void); +int test_memset32_s(void); +int test_memzero_s(void); +int test_memzero16_s(void); +int test_memzero32_s(void); +int test_strcasecmp_s(void); +int test_strcasestr_s(void); +int test_strcat_s(void); +int test_strcmp_s(void); +int test_strcmpfld_s(void); +int test_strcpy_s(void); +int test_strcpyfld_s(void); +int test_strcpyfldin_s(void); +int test_strcpyfldout_s(void); +int test_strcspn_s(void); +int test_strfirstchar_s(void); +int test_strfirstdiff_s(void); +int test_strfirstsame_s(void); +int test_strisalphanumeric_s(void); +int test_strisascii_s(void); +int test_strisdigit_s(void); +int test_strishex_s(void); +int test_strislowercase_s(void); +int test_strismixed_s(void); +int test_strispassword_s(void); +int test_strisuppercase_s(void); +int test_strlastchar_s(void); +int test_strlastdiff_s(void); +int test_strlastsame_s(void); +int test_strljustify_s(void); +int test_strncat_s(void); +int test_strncpy_s(void); +int test_strnlen_s(void); +int test_strnterminate_s(void); +int test_strpbrk_s(void); +int test_strprefix_s(void); +int test_strremovews_s(void); +int test_strspn_s(void); +int test_strstr_s(void); +int test_strtok_s(void); +int test_strtolowercase_s(void); +int test_strtouppercase_s(void); +int test_strzero_s(void); +int test_stpncpy_s(void); +int test_stpcpy_s(void); +int test_wcpcpy_s(void); +int test_wcscat_s(void); +int test_wcscpy_s(void); +int test_wcsncat_s(void); +int test_wcsncpy_s(void); +int test_wcsnlen_s(void); +int test_wmemcpy_s(void); +int test_wmemmove_s(void); +int test_wmemset_s(void); +int test_wmemcmp_s(void); + #endif /* __TEST_PRIVATE_H__ */ From 3422505052d061d966f41b7fd01b3e66efd959bb Mon Sep 17 00:00:00 2001 From: Mark Rustad Date: Sat, 5 May 2018 21:33:54 -0700 Subject: [PATCH 15/18] safestringlib: Add -Wstrict-prototypes Add -W strict-prototypes and resolve the resulting warnings. Add static where necessary, and convert non-prototypes into prototypes. Signed-off-by: Mark Rustad --- makefile | 1 + safeclib/snprintf_support.c | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/makefile b/makefile index 264f3f1..ce32cd1 100644 --- a/makefile +++ b/makefile @@ -3,6 +3,7 @@ MKDIR_P = mkdir -p CC=gcc CFLAGS := -I${IDIR} -fstack-protector-strong -fPIE -fPIC -O2 -MMD CFLAGS += -D_FORTIFY_SOURCE=2 -Wall -Wextra -Wformat -Wformat-security +CFLAGS += -Wmissing-prototypes -Wstrict-prototypes LDFLAGS=-z noexecstack -z now ODIR=obj diff --git a/safeclib/snprintf_support.c b/safeclib/snprintf_support.c index 52a0739..a632a9c 100644 --- a/safeclib/snprintf_support.c +++ b/safeclib/snprintf_support.c @@ -55,7 +55,7 @@ #define CHK_FORMAT(X,Y) (((X)==(Y))?1:0) -unsigned int +static unsigned int parse_format(const char *format, char pformatList[], unsigned int maxFormats) { unsigned int numFormats = 0; @@ -202,7 +202,7 @@ parse_format(const char *format, char pformatList[], unsigned int maxFormats) return numFormats; } -unsigned int +static unsigned int check_integer_format(const char format) { unsigned int retValue = 0; // default failure From 5e032ec98d27ebc080561bdacbfed80c1caf68ef Mon Sep 17 00:00:00 2001 From: Mark Rustad Date: Mon, 28 May 2018 22:03:26 -0700 Subject: [PATCH 16/18] safestringlib: Fix documentation to match implementation Although the documentation describes destination buffers being filled with 0, that is only done when SAFECLIB_STR_NULL_SLACK is defined. Make the source documentation conditional on the same variable. Documentation outside the source is perpetually at risk of being different than how any particular library is built. A such, most documentation should assume that the clearing is not done, as that is the least risk assumption. Signed-off-by: Mark Rustad --- safeclib/stpcpy_s.c | 9 +++++++++ safeclib/stpncpy_s.c | 7 +++++++ safeclib/strcat_s.c | 7 +++++++ safeclib/strcpy_s.c | 9 +++++++++ safeclib/strncpy_s.c | 6 ++++++ safeclib/wcpcpy_s.c | 9 +++++++++ safeclib/wcscat_s.c | 7 +++++++ safeclib/wcscpy_s.c | 9 +++++++++ safeclib/wcsncpy_s.c | 6 ++++++ 9 files changed, 69 insertions(+) diff --git a/safeclib/stpcpy_s.c b/safeclib/stpcpy_s.c index e15908f..8e70f04 100644 --- a/safeclib/stpcpy_s.c +++ b/safeclib/stpcpy_s.c @@ -46,10 +46,14 @@ * DESCRIPTION * The stpcpy_s function copies the string pointed to by src * (including the terminating null character) into the array +#ifdef SAFECLIB_STR_NULL_SLACK * pointed to by dest. All elements following the terminating * null character (if any) written by stpcpy_s in the array * of dmax characters pointed to by dest are nulled when * strcpy_s returns. The function returns a pointer to the +#else + * pointed to by dest. The function returns a pointer to the +#endif // SAFECLIB_STR_NULL_SLACK * end of the string in dest - that is to the null terminator * of dest - upon return. If an error occurs, NULL is returned * and err is set to the error encountered. @@ -88,7 +92,12 @@ * Copying shall not take place between objects that overlap. * If there is a runtime-constraint violation, then if dest * is not a null pointer and destmax is greater than zero and +#ifdef SAFECLIB_STR_NULL_SLACK * not greater than RSIZE_MAX_STR, then stpcpy_s nulls dest. +#else + * not greater than RSIZE_MAX_STR, then stpcpy_s stores a 0 + * terminator in dest. +#endif // SAFECLIB_STR_NULL_SLACK * * RETURN VALUE * a char pointer to the terminating null at the end of dest diff --git a/safeclib/stpncpy_s.c b/safeclib/stpncpy_s.c index 890002d..212d116 100644 --- a/safeclib/stpncpy_s.c +++ b/safeclib/stpncpy_s.c @@ -47,8 +47,10 @@ * The stpncpy_s function copies at most smax characters from the string * pointed to by src, including the terminating null byte ('\0'), to the * array pointed to by dest. Exactly smax characters are written at dest. +#ifdef SAFECLIB_STR_NULL_SLACK * If the length strlen_s(src) is smaller than smax, the remaining smax * characters in the array pointed to by dest are filled with null bytes. +#endif // SAFECLIB_STR_NULL_SLACK * If the length strlen_s(src) is greater than or equal to smax, the string * pointed to by dest will contain smax characters from src plus a null * characters (dest will be null-terminated). @@ -97,8 +99,13 @@ * If src and dest overlap, copying shall be stopped; destruction of src may have occurred. * If there is a runtime-constraint violation, then: * if dest is not a null pointer and dmax is greater than zero and +#ifdef SAFECLIB_STR_NULL_SLACK * not greater than RSIZE_MAX_STR, then stpncpy_s shall fill dest with nulls, * if library was compiled with SAFECLIB_STR_NULL_SLACK. +#else + * not greater than RSIZE_MAX_STR, then stpncpy_s shall write a terminator + * to the dest buffer. +#endif // SAFECLIB_STR_NULL_SLACK * * RETURN VALUE * a char pointer to the terminating null at the end of dest diff --git a/safeclib/strcat_s.c b/safeclib/strcat_s.c index 16db8a7..2eaaf64 100644 --- a/safeclib/strcat_s.c +++ b/safeclib/strcat_s.c @@ -62,8 +62,10 @@ * INPUT PARAMETERS * dest pointer to string that will be extended by src * if dmax allows. The string is null terminated. +#ifdef SAFECLIB_STR_NULL_SLACK * If the resulting concatenated string is less * than dmax, the remaining slack space is nulled. +#endif // SAFECLIB_STR_NULL_SLACK * * dmax restricted maximum length of the resulting dest, * including the null @@ -82,7 +84,12 @@ * Copying shall not takeplace between objects that overlap * If there is a runtime-constraint violation, then if dest is * not a null pointer and dmax is greater than zero and not +#ifdef SAFECLIB_STR_NULL_SLACK * greater than RSIZE_MAX_STR, then strcat_s nulls dest. +#else + * greater than RSIZE_MAX_STR, then strcat_s writes a terminator + * to dest. +#endif // SAFECLIB_STR_NULL_SLACK * * RETURN VALUE * EOK successful operation, all the characters from src diff --git a/safeclib/strcpy_s.c b/safeclib/strcpy_s.c index 67f6f03..874c4c3 100644 --- a/safeclib/strcpy_s.c +++ b/safeclib/strcpy_s.c @@ -46,10 +46,14 @@ * DESCRIPTION * The strcpy_s function copies the string pointed to by src * (including the terminating null character) into the array +#ifdef SAFECLIB_STR_NULL_SLACK * pointed to by dest. All elements following the terminating * null character (if any) written by strcpy_s in the array * of dmax characters pointed to by dest are nulled when * strcpy_s returns. +#else + * pointed to by dest. +#endif // SAFECLIB_STR_NULL_SLACK * * SPECIFIED IN * ISO/IEC TR 24731, Programming languages, environments @@ -75,7 +79,12 @@ * Copying shall not take place between objects that overlap. * If there is a runtime-constraint violation, then if dest * is not a null pointer and destmax is greater than zero and +#ifdef SAFECLIB_STR_NULL_SLACK * not greater than RSIZE_MAX_STR, then strcpy_s nulls dest. +#else + * not greater than RSIZE_MAX_STR, then strcpy_s writes a + * terminator to dest. +#endif // SAFECLIB_STR_NULL_SLACK * * RETURN VALUE * EOK successful operation, the characters in src were diff --git a/safeclib/strncpy_s.c b/safeclib/strncpy_s.c index 1fcdf6f..bc9d286 100644 --- a/safeclib/strncpy_s.c +++ b/safeclib/strncpy_s.c @@ -48,10 +48,12 @@ * (characters that follow a null character are not copied) from the * array pointed to by src to the array pointed to by dest. If no null * character was copied from src, then dest[n] is set to a null character. +#ifdef SAFECLIB_STR_NULL_SLACK * * All elements following the terminating null character (if any) * written by strncpy_s in the array of dmax characters pointed to * by dest take on the null value when strncpy_s returns. +#endif // SAFECLIB_STR_NULL_SLACK * * Specicified in: * ISO/IEC TR 24731-1, Programming languages, environments @@ -82,7 +84,11 @@ * Copying shall not take place between objects that overlap. * If there is a runtime-constraint violation, then if dest * is not a null pointer and dmax greater than RSIZE_MAX_STR, +#ifdef SAFECLIB_STR_NULL_SLACK * then strncpy_s nulls dest. +#else + * the strncpy_s writes a terminator to dest. +#endif // SAFECLIB_STR_NULL_SLACK * * RETURN VALUE * EOK successful operation, the characters in src were copied diff --git a/safeclib/wcpcpy_s.c b/safeclib/wcpcpy_s.c index d388dc6..e4cc9f6 100644 --- a/safeclib/wcpcpy_s.c +++ b/safeclib/wcpcpy_s.c @@ -47,10 +47,14 @@ * The wcpcpy_s function copies the wide character string pointed * to by src (including the terminating null character) into the * array pointed to by dest, and returns a pointer to the end of +#ifdef SAFECLIB_STR_NULL_SLACK * the wide character string. All elements following the terminating * null character (if any) written by wcpcpy_s in the array of * dmax characters pointed to by dest are nulled when * wcpcpy_s returns. +#else + * the wide character string. +#endif // SAFECLIB_STR_NULL_SLACK * * SPECIFIED IN * ISO/IEC TR 24731, Programming languages, environments @@ -86,7 +90,12 @@ * Copying shall not take place between objects that overlap. * If there is a runtime-constraint violation, then if dest * is not a null pointer and destmax is greater than zero and +#ifdef SAFECLIB_STR_NULL_SLACK * not greater than RSIZE_MAX_STR, then strcpy_s nulls dest. +#else + * not greater than RSIZE_MAX_STR, then strcpy_s stores a + * terminator at dest. +#endif // SAFECLIB_STR_NULL_SLACK * * RETURN VALUE * a wchar_t pointer to the terminating null at the end of dest diff --git a/safeclib/wcscat_s.c b/safeclib/wcscat_s.c index 19363df..c1102b1 100644 --- a/safeclib/wcscat_s.c +++ b/safeclib/wcscat_s.c @@ -48,11 +48,13 @@ * to by src (including the terminating null character) to the * end of the string pointed to by dest. The initial wide character * from src overwrites the null character at the end of dest. +#ifdef SAFECLIB_STR_NULL_SLACK * * All elements following the terminating null character (if * any) written by strcat_s in the array of dmax characters * pointed to by dest take unspecified values when strcat_s * returns. +#endif // SAFECLIB_STR_NULL_SLACK * * SPECIFIED IN * ISO/IEC TR 24731, Programming languages, environments @@ -82,7 +84,12 @@ * Copying shall not takeplace between objects that overlap * If there is a runtime-constraint violation, then if dest is * not a null pointer and dmax is greater than zero and not +#ifdef SAFECLIB_STR_NULL_SLACK * greater than RSIZE_MAX_STR, then strcat_s nulls dest. +#else + * greater than RSIZE_MAX_STR, then strcat_s stores a + * terminator at dest. +#endif // SAFECLIB_STR_NULL_SLACK * * RETURN VALUE * EOK successful operation, all the characters from src diff --git a/safeclib/wcscpy_s.c b/safeclib/wcscpy_s.c index 64aad61..e620c33 100644 --- a/safeclib/wcscpy_s.c +++ b/safeclib/wcscpy_s.c @@ -46,10 +46,14 @@ * DESCRIPTION * The wcscpy_s function copies the wide character string pointed * to by src (including the terminating null character) into the +#ifdef SAFECLIB_STR_NULL_SLACK * array pointed to by dest. All elements following the terminating * null character (if any) written by strcpy_s in the array of * dmax characters pointed to by dest are nulled when * wcscpy_s returns. +#else + * array pointed to by dest. +#endif // SAFECLIB_STR_NULL_SLACK * * SPECIFIED IN * ISO/IEC TR 24731, Programming languages, environments @@ -75,7 +79,12 @@ * Copying shall not take place between objects that overlap. * If there is a runtime-constraint violation, then if dest * is not a null pointer and destmax is greater than zero and +#ifdef SAFECLIB_STR_NULL_SLACK * not greater than RSIZE_MAX_STR, then strcpy_s nulls dest. +#else + * not greater than RSIZE_MAX_STR, then strcpy_s stores a + * terminator at dest. +#endif // SAFECLIB_STR_NULL_SLACK * * RETURN VALUE * EOK successful operation, the characters in src were diff --git a/safeclib/wcsncpy_s.c b/safeclib/wcsncpy_s.c index 67ddda1..cfa2d7b 100644 --- a/safeclib/wcsncpy_s.c +++ b/safeclib/wcsncpy_s.c @@ -48,6 +48,7 @@ * (characters that follow a null character are not copied) from the * array pointed to by src to the array pointed to by dest. If no null * character was copied from src, then dest[slen] is set to a null character. +#ifdef SAFECLIB_STR_NULL_SLACK * * All elements following the terminating null character (if any) * written by wcsncpy_s in the array of dmax characters pointed to @@ -60,6 +61,7 @@ * array is used multiple times to string manipulation routines in this * library. If this extra security is not required, ensure that the * library is compiled without #DEFINE SAFECLIB_STR_NULL_SLACK. +#endif // SAFECLIB_STR_NULL_SLACK * * Specicified in: * ISO/IEC TR 24731-1, Programming languages, environments @@ -90,7 +92,11 @@ * Copying shall not take place between objects that overlap. * If there is a runtime-constraint violation, then if dest * is not a null pointer and dmax greater than RSIZE_MAX_STR, +#ifdef SAFECLIB_STR_NULL_SLACK * then strncpy_s nulls dest. +#else + * then strncpy_s stores a terminator at dest. +#endif // SAFECLIB_STR_NULL_SLACK * * RETURN VALUE * EOK successful operation, the characters in src were copied From 8bf75b0574ed8cd01dd3958a4bbe468ab40ac683 Mon Sep 17 00:00:00 2001 From: Mark Rustad Date: Sun, 3 Jun 2018 00:18:21 -0700 Subject: [PATCH 17/18] safestringlib: Fix unit test failures with SAFECLIB_STR_NULL_SLACK When SAFECLIB_STR_NULL_SLACK is defined, several unit tests fail. Some fail because the functions misbehave, others fail because the tests are expecting different values in the slack buffer. Fix them all. Signed-off-by: Mark Rustad --- safeclib/stpcpy_s.c | 12 +++++++++--- safeclib/stpncpy_s.c | 4 ++-- safeclib/wcpcpy_s.c | 8 ++++++-- unittests/test_stpncpy_s.c | 28 ++++++++++++++++++++++++---- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/safeclib/stpcpy_s.c b/safeclib/stpcpy_s.c index 8e70f04..c30b78c 100644 --- a/safeclib/stpcpy_s.c +++ b/safeclib/stpcpy_s.c @@ -157,7 +157,9 @@ stpcpy_s(char *dest, rsize_t dmax, const char *src, errno_t *err) if (*dest == '\0') { #ifdef SAFECLIB_STR_NULL_SLACK /* null slack to clear any data */ - while (dmax) { *dest = '\0'; dmax--; dest++; } + char *filler = dest; + + while (dmax) { *filler = '\0'; dmax--; filler++; } #endif *err = RCNEGATE(EOK); return dest; @@ -191,7 +193,9 @@ stpcpy_s(char *dest, rsize_t dmax, const char *src, errno_t *err) if (*dest == '\0') { #ifdef SAFECLIB_STR_NULL_SLACK /* null slack to clear any data */ - while (dmax) { *dest = '\0'; dmax--; dest++; } + char *filler = dest; + + while (dmax) { *filler = '\0'; dmax--; filler++; } #endif *err = RCNEGATE(EOK); return dest; @@ -219,7 +223,9 @@ stpcpy_s(char *dest, rsize_t dmax, const char *src, errno_t *err) if (*dest == '\0') { #ifdef SAFECLIB_STR_NULL_SLACK /* null slack to clear any data */ - while (dmax) { *dest = '\0'; dmax--; dest++; } + char *filler = dest; + + while (dmax) { *filler = '\0'; dmax--; filler++; } #endif *err = RCNEGATE(EOK); return dest; diff --git a/safeclib/stpncpy_s.c b/safeclib/stpncpy_s.c index 212d116..a52b9d2 100644 --- a/safeclib/stpncpy_s.c +++ b/safeclib/stpncpy_s.c @@ -185,7 +185,7 @@ stpncpy_s(char *dest, rsize_t dmax, const char *src, rsize_t smax, errno_t *err) #ifdef SAFECLIB_STR_NULL_SLACK /* dmwheel1: Add check to prevent destruction of overlap into destination */ - if ((src < dest) && ((src+dmax) >= dest)) { + if ((src < dest) && ((src + smax) > dest)) { invoke_safe_str_constraint_handler("stpncpy_s: src+dmax overlaps into dest", NULL, ESOVRLP); *err = RCNEGATE(ESOVRLP); @@ -193,7 +193,7 @@ stpncpy_s(char *dest, rsize_t dmax, const char *src, rsize_t smax, errno_t *err) } /* dmwheel1: Add check to prevent destruction of overlap into source */ - if ((dest < src) && ((dest+dmax) >= src)) { + if ((dest < src) && ((dest + dmax) > src)) { invoke_safe_str_constraint_handler("stpncpy_s: dest+dmax overlaps into src", NULL, ESOVRLP); *err = RCNEGATE(ESOVRLP); diff --git a/safeclib/wcpcpy_s.c b/safeclib/wcpcpy_s.c index e4cc9f6..aea6555 100644 --- a/safeclib/wcpcpy_s.c +++ b/safeclib/wcpcpy_s.c @@ -178,7 +178,9 @@ wcpcpy_s(wchar_t* dest, rsize_t dmax, const wchar_t* src, errno_t *err) if (*dest == L'\0') { #ifdef SAFECLIB_STR_NULL_SLACK /* null slack to clear any data */ - while (dmax) { *dest = L'\0'; dmax--; dest++; } + wchar_t *filler = dest; + + while (dmax) { *filler = L'\0'; dmax--; filler++; } #endif *err = RCNEGATE(EOK); return dest; /* successful return */ @@ -204,7 +206,9 @@ wcpcpy_s(wchar_t* dest, rsize_t dmax, const wchar_t* src, errno_t *err) if (*dest == L'\0') { #ifdef SAFECLIB_STR_NULL_SLACK /* null slack to clear any data */ - while (dmax) { *dest = L'\0'; dmax--; dest++; } + wchar_t *filler = dest; + + while (dmax) { *filler = L'\0'; dmax--; filler++; } #endif *err = RCNEGATE(EOK); return dest; /* successful return */ diff --git a/unittests/test_stpncpy_s.c b/unittests/test_stpncpy_s.c index 77684b9..29ce345 100644 --- a/unittests/test_stpncpy_s.c +++ b/unittests/test_stpncpy_s.c @@ -428,7 +428,12 @@ printf("Test #%d:\n", ++testno); } /* be sure that the slack is correct */ for (i=1; i<6; i++) { - if (ret[i] != 'x') { +#ifdef SAFECLIB_STR_NULL_SLACK + const char slack = '\0'; +#else + const char slack = 'x'; +#endif // SAFECLIB_STR_NULL_SLACK + if (ret[i] != slack) { printf("%s %u Incorrect Slack at returned ptr index %d Error rc=%u \n", __FUNCTION__, __LINE__, i, rc ); ++errs; @@ -473,7 +478,12 @@ printf("Test #%d:\n", ++testno); } /* be sure that the slack is correct */ for (; i<15; i++) { - if (ret[i] != 'x') { +#ifdef SAFECLIB_STR_NULL_SLACK + const char slack = '\0'; +#else + const char slack = 'x'; +#endif // SAFECLIB_STR_NULL_SLACK + if (ret[i] != slack) { printf("%s %u Incorrect Slack at returned ptr index %d Error rc=%u \n", __FUNCTION__, __LINE__, i, rc ); ++errs; @@ -511,7 +521,12 @@ printf("Test #%d:\n", ++testno); } else { /* be sure that the slack is correct */ for (i=1; i<5; i++) { - if (ret[i] != 'x') { +#ifdef SAFECLIB_STR_NULL_SLACK + const char slack = '\0'; +#else + const char slack = 'x'; +#endif // SAFECLIB_STR_NULL_SLACK + if (ret[i] != slack) { printf("%s %u Incorrect Slack at returned ptr index %d Error rc=%u \n", __FUNCTION__, __LINE__, i, rc ); ++errs; @@ -549,7 +564,12 @@ printf("Test #%d:\n", ++testno); } else { /* be sure that the slack is correct */ for (i=1; i<5; i++) { - if (ret[i] != 'x') { +#ifdef SAFECLIB_STR_NULL_SLACK + const char slack = '\0'; +#else + const char slack = 'x'; +#endif // SAFECLIB_STR_NULL_SLACK + if (ret[i] != slack) { printf("%s %u Incorrect Slack at returned ptr index %d Error rc=%u \n", __FUNCTION__, __LINE__, i, rc ); ++errs; From 30fda210bf2449ad7588cd9f073b87b38a4cb716 Mon Sep 17 00:00:00 2001 From: Mark Rustad Date: Sat, 2 Jun 2018 12:11:59 -0700 Subject: [PATCH 18/18] safestringlib: Use order-only dependencies to create directories Order-only dependencies work well for creating object directories, so use them. Also, remove those directories when doing a clean. Adds a missing .PHONY for the all rule. Signed-off-by: Mark Rustad > --- makefile | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/makefile b/makefile index ce32cd1..d931f15 100644 --- a/makefile +++ b/makefile @@ -11,7 +11,8 @@ OTDIR=objtest SRCDIR=safeclib TESTDIR=unittests -all: directories libsafestring.a safestringtest +.PHONY: all +all: libsafestring.a safestringtest _CLIB := abort_handler_s.c stpcpy_s.c strlastsame_s.c ignore_handler_s.c _CLIB += stpncpy_s.c strljustify_s.c memcmp16_s.c strcasecmp_s.c strncat_s.c @@ -36,7 +37,7 @@ OBJ = $(patsubst %.c,%.o,$(_TLIST)) CLIB =$(addprefix $(SRCDIR)/,$(_CLIB)) -${ODIR}/%.o: ${SRCDIR}/%.c +${ODIR}/%.o: ${SRCDIR}/%.c | ${ODIR} $(CC) $(LDFLAGS) -c -o $@ $< $(CFLAGS) libsafestring.a: $(OBJ) @@ -70,18 +71,13 @@ TOBJ = $(patsubst %.c,%.o,$(_TLIST2)) TCLIB =$(addprefix $(TESTDIR)/,$(_TESTFUNCS)) -$(OTDIR)/%.o: $(TESTDIR)/%.c $(TESTDIR)/test_private.h +${OTDIR}/%.o: ${TESTDIR}/%.c ${TESTDIR}/test_private.h | ${OTDIR} $(CC) -c -o $@ $< $(CFLAGS) -safestringtest: directories libsafestring.a $(TOBJ) +safestringtest: libsafestring.a $(TOBJ) | ${OTDIR} $(CC) $(LDFLAGS) -o $@ $(TOBJ) libsafestring.a - -.PHONY: directories - -directories: ${ODIR} ${OTDIR} - ${ODIR}: ${MKDIR_P} ${ODIR} @@ -91,7 +87,8 @@ ${OTDIR}: .PHONY: clean clean: - rm -f ${ODIR}/* *~ core ${INCDIR}/*~ ${OTDIR}/* + rm -f *~ core ${INCDIR}/*~ + rm -rf ${ODIR} ${OTDIR} rm -f libsafestring.a rm -f safestringtest