Merge pull request #2839 from signalwire/fctstr_safe_cpy

[Unit-tests] Fix test framework error on newer compiler: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Werror=stringop-truncation]
This commit is contained in:
Andrey Volk 2025-07-10 21:39:36 +03:00 committed by GitHub
commit f76b504d6d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 2 deletions

View File

@ -254,10 +254,18 @@ fctstr_safe_cpy(char *dst, char const *src, size_t num)
FCT_ASSERT( num > 0 ); FCT_ASSERT( num > 0 );
#if defined(WIN32) && _MSC_VER >= 1400 #if defined(WIN32) && _MSC_VER >= 1400
strncpy_s(dst, num, src, _TRUNCATE); strncpy_s(dst, num, src, _TRUNCATE);
dst[num - 1] = '\0';
#else #else
strncpy(dst, src, num - 1); {
size_t i;
for (i = 0; (i < num - 1) && src[i] != '\0'; ++i) {
dst[i] = src[i];
}
dst[i] = '\0';
}
#endif #endif
dst[num-1] = '\0';
} }
/* Isolate the vsnprintf implementation */ /* Isolate the vsnprintf implementation */

View File

@ -53,6 +53,17 @@ FST_CORE_BEGIN("./conf")
} }
FST_TEARDOWN_END() FST_TEARDOWN_END()
FST_TEST_BEGIN(test_fctstr_safe_cpy)
{
char *dst;
const char *src = "1234567890";
dst = fctstr_clone(src);
fst_check_string_equals(dst, src);
free(dst);
}
FST_TEST_END()
FST_TEST_BEGIN(test_switch_rand) FST_TEST_BEGIN(test_switch_rand)
{ {
int i, c = 0; int i, c = 0;