From 744642c286a0f33f8bf0b4f4e1bf2efdbfb08ff2 Mon Sep 17 00:00:00 2001
From: Richard Mudgett <rmudgett@digium.com>
Date: Fri, 7 Sep 2012 23:07:49 +0000
Subject: [PATCH] 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


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/11@372657 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 main/astmm.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

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;
 }
-- 
GitLab