From 1377d7a1c294f256f9c8d2f130758f5aba4e74cd Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Mon, 28 Dec 2020 22:54:16 +0100 Subject: [PATCH] Fix usage of strncpy_s and strncat_s Provides a full wrapper around strncpy & strncat (for MSVS as it complains on using strncpy & strncat). See #86 --- m-core.h | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/m-core.h b/m-core.h index 4ddc1f57..f28587ee 100644 --- a/m-core.h +++ b/m-core.h @@ -1737,10 +1737,38 @@ m_core_fopen(const char filename[], const char opt[]) if (err) return NULL; return f; } -/* Wrapper around strncpy_s */ -#define m_core_strncpy(s1, s2, size) strncpy_s(s1, size, s2, size) -/* Wrapper around strncat_s */ -#define m_core_strncat(s1, s2, size) strncat_s(s1, size, s2, size) +/* 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 fscanf_s */ #define m_core_fscanf(...) fscanf_s(__VA_ARGS__)