From: Richard Mudgett Date: Fri, 7 Sep 2012 23:06:38 +0000 (+0000) Subject: Fix MALLOC_DEBUG version of ast_strndup(). X-Git-Tag: 10.9.0-rc1~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=12a2affd6fdb906c18dab9c09f7f43c494699466;p=thirdparty%2Fasterisk.git Fix MALLOC_DEBUG version of ast_strndup(). (closes issue ASTERISK-20349) Reported by: Brent Eagles ........ Merged revisions 372655 from http://svn.asterisk.org/svn/asterisk/branches/1.8 git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/10@372656 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- diff --git a/main/astmm.c b/main/astmm.c index 64ba167411..ad7046dc75 100644 --- a/main/astmm.c +++ b/main/astmm.c @@ -272,16 +272,17 @@ char *__ast_strdup(const char *s, const char *file, int lineno, const char *func char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func) { size_t len; - void *ptr; + char *ptr; - if (!s) + if (!s) { return NULL; + } - len = strlen(s) + 1; - if (len > n) - len = n; - if ((ptr = __ast_alloc_region(len, FUNC_STRNDUP, file, lineno, func, 0))) - strcpy(ptr, s); + len = strnlen(s, n); + if ((ptr = __ast_alloc_region(len + 1, FUNC_STRNDUP, file, lineno, func, 0))) { + memcpy(ptr, s, len); + ptr[len] = '\0'; + } return ptr; }