Skip to content

Commit 577bd1c

Browse files
authored
Merge pull request #5149 from mysterywolf/strnlen
fixed bug #5138
2 parents 9840418 + 6639918 commit 577bd1c

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

include/rtthread.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ int __rt_ffs(int value);
564564

565565
void *rt_memset(void *src, int c, rt_ubase_t n);
566566
void *rt_memcpy(void *dest, const void *src, rt_ubase_t n);
567+
char *rt_strdup(const char *s);
567568

568569
#ifndef RT_KSERVICE_USING_STDLIB
569570
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);
573574
char *rt_strncpy(char *dest, const char *src, rt_ubase_t n);
574575
rt_int32_t rt_strncmp(const char *cs, const char *ct, rt_ubase_t count);
575576
rt_int32_t rt_strcmp(const char *cs, const char *ct);
576-
rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen);
577577
rt_size_t rt_strlen(const char *src);
578578
#else
579579
#include <string.h>
@@ -584,15 +584,20 @@ rt_size_t rt_strlen(const char *src);
584584
#define rt_strncpy(dest, src, n) strncpy(dest, src, n)
585585
#define rt_strncmp(cs, ct, count) strncmp(cs, ct, count)
586586
#define rt_strcmp(cs, ct) strcmp(cs, ct)
587-
#define rt_strnlen(s, maxlen) strnlen(s, maxlen)
588587
#define rt_strlen(src) strlen(src)
589588
#endif /*RT_KSERVICE_USING_STDLIB*/
590589

591-
char *rt_strdup(const char *s);
590+
#if !defined(RT_KSERVICE_USING_STDLIB) || defined(__ARMCC_VERSION)
591+
rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen);
592+
#else
593+
#define rt_strnlen(s, maxlen) strnlen(s, maxlen)
594+
#endif /* !defined(RT_KSERVICE_USING_STDLIB) || defined(__ARMCC_VERSION) */
595+
592596
#ifdef __ARMCC_VERSION
593-
/* lack strdup interface */
597+
/* MDK doesn't have these APIs */
594598
char* strdup(const char* str);
595-
#endif
599+
size_t strnlen(const char *s, size_t maxlen);
600+
#endif /* __ARMCC_VERSION */
596601

597602
void rt_show_version(void);
598603

src/kservice.c

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -500,49 +500,54 @@ rt_int32_t rt_strcmp(const char *cs, const char *ct)
500500
RTM_EXPORT(rt_strcmp);
501501

502502
/**
503-
* The strnlen() function returns the number of characters in the
504-
* string pointed to by s, excluding the terminating null byte ('\0'),
505-
* but at most maxlen. In doing this, strnlen() looks only at the
506-
* first maxlen characters in the string pointed to by s and never
507-
* beyond s+maxlen.
508-
*
509-
* @param s is the string.
503+
* This function will return the length of a string, which terminate will
504+
* null character.
510505
*
511-
* @param maxlen is the max size.
506+
* @param s is the string
512507
*
513508
* @return The length of string.
514509
*/
515-
rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen)
510+
rt_size_t rt_strlen(const char *s)
516511
{
517512
const char *sc;
518513

519-
for (sc = s; *sc != '\0' && (rt_ubase_t)(sc - s) < maxlen; ++sc) /* nothing */
514+
for (sc = s; *sc != '\0'; ++sc) /* nothing */
520515
;
521516

522517
return sc - s;
523518
}
524-
RTM_EXPORT(rt_strnlen);
519+
RTM_EXPORT(rt_strlen);
525520

521+
#endif /* RT_KSERVICE_USING_STDLIB */
522+
523+
#if !defined(RT_KSERVICE_USING_STDLIB) || defined(__ARMCC_VERSION)
526524
/**
527-
* This function will return the length of a string, which terminate will
528-
* null character.
525+
* The strnlen() function returns the number of characters in the
526+
* string pointed to by s, excluding the terminating null byte ('\0'),
527+
* but at most maxlen. In doing this, strnlen() looks only at the
528+
* first maxlen characters in the string pointed to by s and never
529+
* beyond s+maxlen.
529530
*
530-
* @param s is the string
531+
* @param s is the string.
532+
*
533+
* @param maxlen is the max size.
531534
*
532535
* @return The length of string.
533536
*/
534-
rt_size_t rt_strlen(const char *s)
537+
rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen)
535538
{
536539
const char *sc;
537540

538-
for (sc = s; *sc != '\0'; ++sc) /* nothing */
541+
for (sc = s; *sc != '\0' && (rt_ubase_t)(sc - s) < maxlen; ++sc) /* nothing */
539542
;
540543

541544
return sc - s;
542545
}
543-
RTM_EXPORT(rt_strlen);
544-
545-
#endif /* RT_KSERVICE_USING_STDLIB */
546+
RTM_EXPORT(rt_strnlen);
547+
#ifdef __ARMCC_VERSION
548+
size_t strnlen(const char *s, size_t maxlen) __attribute__((alias("rt_strnlen")));
549+
#endif /* __ARMCC_VERSION */
550+
#endif /* !defined(RT_KSERVICE_USING_STDLIB) || defined(__ARMCC_VERSION) */
546551

547552
#ifdef RT_USING_HEAP
548553
/**
@@ -567,7 +572,7 @@ char *rt_strdup(const char *s)
567572
RTM_EXPORT(rt_strdup);
568573
#ifdef __ARMCC_VERSION
569574
char *strdup(const char *s) __attribute__((alias("rt_strdup")));
570-
#endif
575+
#endif /* __ARMCC_VERSION */
571576
#endif /* RT_USING_HEAP */
572577

573578
/**

0 commit comments

Comments
 (0)