diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/kservice.c | 25 | ||||
| -rw-r--r-- | src/object.c | 10 |
2 files changed, 20 insertions, 15 deletions
diff --git a/src/kservice.c b/src/kservice.c index 76d6f099b0..f4d9e24142 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -355,20 +355,31 @@ rt_uint32_t rt_strcasecmp(const char *a, const char *b) /** * This function will copy string no more than n bytes. * - * @param dest the string to copy + * @param dst the string to copy * @param src the string to be copied * @param n the maximum copied length * * @return the result */ -char *rt_strncpy(char *dest, const char *src, rt_ubase_t n) +char *rt_strncpy(char *dst, const char *src, rt_ubase_t n) { - char *tmp = (char *)dest, *s = (char *)src; - - while (n--) - *tmp++ = *s++; + if (n != 0) + { + char *d = dst; + const char *s = src; - return dest; + do + { + if ((*d++ = *s++) == 0) + { + /* NUL pad the remaining n-1 bytes */ + while (--n != 0) + *d++ = 0; + break; + } + } while (--n != 0); + } + return (dst); } /** diff --git a/src/object.c b/src/object.c index feedc1def8..f4a45713d7 100644 --- a/src/object.c +++ b/src/object.c @@ -204,10 +204,7 @@ void rt_object_init(struct rt_object *object, enum rt_object_class_type type, co object->type = type | RT_Object_Class_Static; /* copy name */ - for (temp = 0; temp < RT_NAME_MAX; temp ++) - { - object->name[temp] = name[temp]; - } + rt_strncpy(object->name, name, RT_NAME_MAX); RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object)); @@ -296,10 +293,7 @@ rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name) #endif /* copy name */ - for (temp = 0; temp < RT_NAME_MAX; temp ++) - { - object->name[temp] = name[temp]; - } + rt_strncpy(object->name, name, RT_NAME_MAX); RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object)); |
