Hi all,

Here a cleanup patch for the chunk_dup function.
hope it can be useful.

Regards.
From 3d904193dc041bad266fd04f69b50a66b8429f54 Mon Sep 17 00:00:00 2001
From: David Carlier <devne...@gmail.com>
Date: Wed, 23 Mar 2016 17:50:57 +0000
Subject: [PATCH] CLEANUP: chunk: adding NULL check to chunk_dup allocation.

Avoiding harmful memcpy call if the allocation failed.
Resetting the size which avoids further harmful freeing
invalid pointer. Closer to the comment behavior description.
---
 include/common/chunk.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/include/common/chunk.h b/include/common/chunk.h
index b74c767..aac5282 100644
--- a/include/common/chunk.h
+++ b/include/common/chunk.h
@@ -177,6 +177,12 @@ static inline char *chunk_dup(struct chunk *dst, const struct chunk *src)
 		dst->size++;
 
 	dst->str = (char *)malloc(dst->size);
+	if (!dst->str) {
+		dst->len = 0;
+		dst->size = 0;
+		return NULL;
+	}
+
 	memcpy(dst->str, src->str, dst->len);
 	if (dst->len < dst->size)
 		dst->str[dst->len] = 0;
-- 
2.7.4

Reply via email to