Skip to content

Commit cd357c5

Browse files
committed
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.
1 parent 561f67b commit cd357c5

File tree

2 files changed

+16
-42
lines changed

2 files changed

+16
-42
lines changed

m-core.h

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,41 +1756,15 @@ m_core_fopen(const char filename[], const char opt[])
17561756
if (err) return NULL;
17571757
return f;
17581758
}
1759-
/* Rewrite strncpy (Cannot use strncpy_s as its semantic is too bothersome) */
1760-
static inline void m_core_strncpy(char s1[], const char s2[], size_t size)
1761-
{
1762-
while (size > 0) {
1763-
*s1 = *s2;
1764-
s1 ++;
1765-
s2 += (*s2 != 0);
1766-
size --;
1767-
}
1768-
// No final null char (it may not appear in case of truncation)
1769-
}
1770-
/* Rewrite strncat (Cannot use strncat_s as its semantic is too bothersome) */
1771-
static inline void m_core_strncat(char s1[], const char s2[], size_t size)
1772-
{
1773-
// Go to the end of s1
1774-
while (*s1 != 0) {
1775-
s1++;
1776-
}
1777-
// Copy at most size bytes of s2 in s1.
1778-
while (size > 0)
1779-
{
1780-
*s1 = *s2;
1781-
if (*s2 == 0) {
1782-
break;
1783-
}
1784-
s1 ++;
1785-
s2 ++;
1786-
size --;
1787-
}
1788-
// Always a final null char.
1789-
*s1 = 0;
1790-
}
1759+
1760+
/* Wrapper around strncpy */
1761+
#define m_core_strncpy(s1, alloc, s2, size) strncpy_s(s1, alloc, s2, size)
1762+
1763+
/* Wrapper around strncat */
1764+
#define m_core_strncat(s1, alloc, s2, size) strncat_s(s1, alloc, s2, size)
1765+
17911766
/* Wrapper around fscanf_s */
17921767
#define m_core_fscanf(...) fscanf_s(__VA_ARGS__)
1793-
17941768
/* Macro to be used in m_core_fscanf for argument associated
17951769
* to the format %c, %s or %[
17961770
* 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)
18011775
/* Wrapper around fopen */
18021776
#define m_core_fopen(...) fopen(__VA_ARGS__)
18031777
/* Wrapper around strncpy */
1804-
#define m_core_strncpy(...) strncpy(__VA_ARGS__)
1778+
#define m_core_strncpy(s1, alloc, s2, size) strncpy(s1, s2, size)
18051779
/* Wrapper around strncat */
1806-
#define m_core_strncat(s1, s2, size) strncat(s1, s2, size)
1780+
#define m_core_strncat(s1, alloc, s2, size) strncat(s1, s2, size)
18071781
/* Wrapper around fscanf */
18081782
#define m_core_fscanf(...) fscanf(__VA_ARGS__)
18091783

m-string.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,7 +2035,7 @@ namespace m_lib {
20352035
M_C(name, _set_str)(bounded_t s, const char str[]) \
20362036
{ \
20372037
BOUNDED_STRINGI_CONTRACT(s, max_size); \
2038-
m_core_strncpy(s->s, str, max_size); \
2038+
m_core_strncpy(s->s, max_size+1, str, max_size); \
20392039
BOUNDED_STRINGI_CONTRACT(s, max_size); \
20402040
} \
20412041
\
@@ -2045,7 +2045,7 @@ namespace m_lib {
20452045
BOUNDED_STRINGI_CONTRACT(s, max_size); \
20462046
M_ASSERT(str != NULL); \
20472047
size_t len = M_MIN(max_size, n); \
2048-
m_core_strncpy(s->s, str, len); \
2048+
m_core_strncpy(s->s, max_size+1, str, len); \
20492049
s->s[len] = 0; \
20502050
BOUNDED_STRINGI_CONTRACT(s, max_size); \
20512051
} \
@@ -2095,7 +2095,7 @@ namespace m_lib {
20952095
BOUNDED_STRINGI_CONTRACT(s, max_size); \
20962096
M_ASSERT (str != NULL); \
20972097
M_ASSERT_INDEX (strlen(s->s), max_size+1); \
2098-
m_core_strncat(s->s, str, max_size-strlen(s->s)); \
2098+
m_core_strncat(s->s, max_size+1, str, max_size-strlen(s->s)); \
20992099
} \
21002100
\
21012101
static inline void \
@@ -2256,7 +2256,7 @@ namespace m_lib {
22562256
string_t v2; \
22572257
string_init(v2); \
22582258
bool ret = string_in_str(v2, f); \
2259-
m_core_strncpy(v->s, string_get_cstr(v2), max_size); \
2259+
m_core_strncpy(v->s, max_size+1, string_get_cstr(v2), max_size); \
22602260
string_clear(v2); \
22612261
return ret; \
22622262
} \
@@ -2269,7 +2269,7 @@ namespace m_lib {
22692269
string_t v2; \
22702270
string_init(v2); \
22712271
bool ret = string_parse_str(v2, str, endptr); \
2272-
m_core_strncpy(v->s, string_get_cstr(v2), max_size); \
2272+
m_core_strncpy(v->s, max_size+1, string_get_cstr(v2), max_size); \
22732273
string_clear(v2); \
22742274
return ret; \
22752275
} \
@@ -2291,7 +2291,7 @@ namespace m_lib {
22912291
/* TODO: Not optimum */ \
22922292
string_init(tmp); \
22932293
m_serial_return_code_t r = serial->m_interface->read_string(serial, tmp); \
2294-
m_core_strncpy(v->s, string_get_cstr(tmp), max_size); \
2294+
m_core_strncpy(v->s, max_size+1, string_get_cstr(tmp), max_size); \
22952295
string_clear(tmp); \
22962296
BOUNDED_STRINGI_CONTRACT(v, max_size); \
22972297
return r; \
@@ -2314,7 +2314,7 @@ namespace m_lib {
23142314
inline m_bounded_string(const char lstr[])
23152315
{
23162316
memset(this->s, 0, N);
2317-
m_core_strncpy(this->s, lstr, N-1);
2317+
m_core_strncpy(this->s, N, lstr, N-1);
23182318
}
23192319
};
23202320
}

0 commit comments

Comments
 (0)