From 663991897bbd2ae20098747fd79c57e0d42c70c0 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Mon, 11 Oct 2021 16:02:46 -0400 Subject: [PATCH] fixed bug #5138 --- include/rtthread.h | 15 +++++++++----- src/kservice.c | 49 +++++++++++++++++++++++++--------------------- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/include/rtthread.h b/include/rtthread.h index 1be5fda82d..cee58e402b 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -564,6 +564,7 @@ int __rt_ffs(int value); void *rt_memset(void *src, int c, rt_ubase_t n); void *rt_memcpy(void *dest, const void *src, rt_ubase_t n); +char *rt_strdup(const char *s); #ifndef RT_KSERVICE_USING_STDLIB void *rt_memmove(void *dest, const void *src, rt_ubase_t n); @@ -573,7 +574,6 @@ rt_int32_t rt_strcasecmp(const char *a, const char *b); char *rt_strncpy(char *dest, const char *src, rt_ubase_t n); rt_int32_t rt_strncmp(const char *cs, const char *ct, rt_ubase_t count); rt_int32_t rt_strcmp(const char *cs, const char *ct); -rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen); rt_size_t rt_strlen(const char *src); #else #include @@ -584,15 +584,20 @@ rt_size_t rt_strlen(const char *src); #define rt_strncpy(dest, src, n) strncpy(dest, src, n) #define rt_strncmp(cs, ct, count) strncmp(cs, ct, count) #define rt_strcmp(cs, ct) strcmp(cs, ct) -#define rt_strnlen(s, maxlen) strnlen(s, maxlen) #define rt_strlen(src) strlen(src) #endif /*RT_KSERVICE_USING_STDLIB*/ -char *rt_strdup(const char *s); +#if !defined(RT_KSERVICE_USING_STDLIB) || defined(__ARMCC_VERSION) +rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen); +#else +#define rt_strnlen(s, maxlen) strnlen(s, maxlen) +#endif /* !defined(RT_KSERVICE_USING_STDLIB) || defined(__ARMCC_VERSION) */ + #ifdef __ARMCC_VERSION -/* lack strdup interface */ +/* MDK doesn't have these APIs */ char* strdup(const char* str); -#endif +size_t strnlen(const char *s, size_t maxlen); +#endif /* __ARMCC_VERSION */ void rt_show_version(void); diff --git a/src/kservice.c b/src/kservice.c index 41d81d8677..cc1dc6e22c 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -499,6 +499,28 @@ rt_int32_t rt_strcmp(const char *cs, const char *ct) } RTM_EXPORT(rt_strcmp); +/** + * This function will return the length of a string, which terminate will + * null character. + * + * @param s is the string + * + * @return The length of string. + */ +rt_size_t rt_strlen(const char *s) +{ + const char *sc; + + for (sc = s; *sc != '\0'; ++sc) /* nothing */ + ; + + return sc - s; +} +RTM_EXPORT(rt_strlen); + +#endif /* RT_KSERVICE_USING_STDLIB */ + +#if !defined(RT_KSERVICE_USING_STDLIB) || defined(__ARMCC_VERSION) /** * The strnlen() function returns the number of characters in the * string pointed to by s, excluding the terminating null byte ('\0'), @@ -522,27 +544,10 @@ rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen) return sc - s; } RTM_EXPORT(rt_strnlen); - -/** - * This function will return the length of a string, which terminate will - * null character. - * - * @param s is the string - * - * @return The length of string. - */ -rt_size_t rt_strlen(const char *s) -{ - const char *sc; - - for (sc = s; *sc != '\0'; ++sc) /* nothing */ - ; - - return sc - s; -} -RTM_EXPORT(rt_strlen); - -#endif /* RT_KSERVICE_USING_STDLIB */ +#ifdef __ARMCC_VERSION +size_t strnlen(const char *s, size_t maxlen) __attribute__((alias("rt_strnlen"))); +#endif /* __ARMCC_VERSION */ +#endif /* !defined(RT_KSERVICE_USING_STDLIB) || defined(__ARMCC_VERSION) */ #ifdef RT_USING_HEAP /** @@ -567,7 +572,7 @@ char *rt_strdup(const char *s) RTM_EXPORT(rt_strdup); #ifdef __ARMCC_VERSION char *strdup(const char *s) __attribute__((alias("rt_strdup"))); -#endif +#endif /* __ARMCC_VERSION */ #endif /* RT_USING_HEAP */ /**