From 65eb29503b7cfe0f509c5b4d6146dd7961d1f8ba Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Sat, 1 Dec 2018 00:35:41 +0100 Subject: [PATCH] libdm: optimize dm_pool_strndup --- libdm/mm/pool.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libdm/mm/pool.c b/libdm/mm/pool.c index a8fe73228..26f43a787 100644 --- a/libdm/mm/pool.c +++ b/libdm/mm/pool.c @@ -59,11 +59,13 @@ char *dm_pool_strdup(struct dm_pool *p, const char *str) char *dm_pool_strndup(struct dm_pool *p, const char *str, size_t n) { + size_t slen = strlen(str); + size_t len = (slen < n) ? slen : n; char *ret = dm_pool_alloc(p, n + 1); if (ret) { - strncpy(ret, str, n); - ret[n] = '\0'; + ret[len] = '\0'; + memcpy(ret, str, len); } return ret;