From: Richard Mudgett Date: Fri, 7 Sep 2012 23:10:05 +0000 (+0000) Subject: Fix MALLOC_DEBUG version of ast_strndup(). X-Git-Tag: 13.0.0-beta1~2559 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e3156572878976bf2607d374449ca5c7dbbfb488;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 ........ Merged revisions 372656 from http://svn.asterisk.org/svn/asterisk/branches/10 ........ Merged revisions 372657 from http://svn.asterisk.org/svn/asterisk/branches/11 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@372658 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- diff --git a/main/astmm.c b/main/astmm.c index 741358205e..e469d60ad1 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; }