Rewrite the wrappers of strncpy & strncat:

Add one argument about the allocation of the dest buffer
Theses wrappers are internal (so no impact to API)
and only used in a few place in m-string.
This make the wrappers able to use the function
strncpy_s & strncat_s for MSVC rather than using
reimplementation of the original functions.
This commit is contained in:
Patrick Pelissier committed 2021-01-13 08:34:47 +01:00
1 parent 561f67b3b9
commit cd357c5eb7
2 files changed
+16 -42

No files matched your search

+9 -35
View File
@@ -1756,41 +1756,15 @@ m_core_fopen(const char filename[], const char opt[])
if (err) return NULL;
return f;
}
/* Rewrite strncpy (Cannot use strncpy_s as its semantic is too bothersome) */
static inline void m_core_strncpy(char s1[], const char s2[], size_t size)
{
while (size > 0) {
*s1 = *s2;
s1 ++;
s2 += (*s2 != 0);
size --;
}
// No final null char (it may not appear in case of truncation)
}
/* Rewrite strncat (Cannot use strncat_s as its semantic is too bothersome) */
static inline void m_core_strncat(char s1[], const char s2[], size_t size)
{
// Go to the end of s1
while (*s1 != 0) {
s1++;
}
// Copy at most size bytes of s2 in s1.
while (size > 0)
{
*s1 = *s2;
if (*s2 == 0) {
break;
}
s1 ++;
s2 ++;
size --;
}
// Always a final null char.
*s1 = 0;
}
/* Wrapper around strncpy */
#define m_core_strncpy(s1, alloc, s2, size) strncpy_s(s1, alloc, s2, size)
/* Wrapper around strncat */
#define m_core_strncat(s1, alloc, s2, size) strncat_s(s1, alloc, s2, size)
/* Wrapper around fscanf_s */
#define m_core_fscanf(...) fscanf_s(__VA_ARGS__)
/* Macro to be used in m_core_fscanf for argument associated
* to the format %c, %s or %[
* in order to specify the size of the argument */
@@ -1801,9 +1775,9 @@ static inline void m_core_strncat(char s1[], const char s2[], size_t size)
/* Wrapper around fopen */
#define m_core_fopen(...) fopen(__VA_ARGS__)
/* Wrapper around strncpy */
#define m_core_strncpy(...) strncpy(__VA_ARGS__)
#define m_core_strncpy(s1, alloc, s2, size) strncpy(s1, s2, size)
/* Wrapper around strncat */
#define m_core_strncat(s1, s2, size) strncat(s1, s2, size)
#define m_core_strncat(s1, alloc, s2, size) strncat(s1, s2, size)
/* Wrapper around fscanf */
#define m_core_fscanf(...) fscanf(__VA_ARGS__)
+7 -7
View File
@@ -2035,7 +2035,7 @@ namespace m_lib {
M_C(name, _set_str)(bounded_t s, const char str[]) \
{ \
BOUNDED_STRINGI_CONTRACT(s, max_size); \
m_core_strncpy(s->s, str, max_size); \
m_core_strncpy(s->s, max_size+1, str, max_size); \
BOUNDED_STRINGI_CONTRACT(s, max_size); \
} \
\
@@ -2045,7 +2045,7 @@ namespace m_lib {
BOUNDED_STRINGI_CONTRACT(s, max_size); \
M_ASSERT(str != NULL); \
size_t len = M_MIN(max_size, n); \
m_core_strncpy(s->s, str, len); \
m_core_strncpy(s->s, max_size+1, str, len); \
s->s[len] = 0; \
BOUNDED_STRINGI_CONTRACT(s, max_size); \
} \
@@ -2095,7 +2095,7 @@ namespace m_lib {
BOUNDED_STRINGI_CONTRACT(s, max_size); \
M_ASSERT (str != NULL); \
M_ASSERT_INDEX (strlen(s->s), max_size+1); \
m_core_strncat(s->s, str, max_size-strlen(s->s)); \
m_core_strncat(s->s, max_size+1, str, max_size-strlen(s->s)); \
} \
\
static inline void \
@@ -2256,7 +2256,7 @@ namespace m_lib {
string_t v2; \
string_init(v2); \
bool ret = string_in_str(v2, f); \
m_core_strncpy(v->s, string_get_cstr(v2), max_size); \
m_core_strncpy(v->s, max_size+1, string_get_cstr(v2), max_size); \
string_clear(v2); \
return ret; \
} \
@@ -2269,7 +2269,7 @@ namespace m_lib {
string_t v2; \
string_init(v2); \
bool ret = string_parse_str(v2, str, endptr); \
m_core_strncpy(v->s, string_get_cstr(v2), max_size); \
m_core_strncpy(v->s, max_size+1, string_get_cstr(v2), max_size); \
string_clear(v2); \
return ret; \
} \
@@ -2291,7 +2291,7 @@ namespace m_lib {
/* TODO: Not optimum */ \
string_init(tmp); \
m_serial_return_code_t r = serial->m_interface->read_string(serial, tmp); \
m_core_strncpy(v->s, string_get_cstr(tmp), max_size); \
m_core_strncpy(v->s, max_size+1, string_get_cstr(tmp), max_size); \
string_clear(tmp); \
BOUNDED_STRINGI_CONTRACT(v, max_size); \
return r; \
@@ -2314,7 +2314,7 @@ namespace m_lib {
inline m_bounded_string(const char lstr[])
{
memset(this->s, 0, N);
m_core_strncpy(this->s, lstr, N-1);
m_core_strncpy(this->s, N, lstr, N-1);
}
};
}