Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1376822 > unrolled thread

[PATCH 2/2] scripts: dtc: fix memory leak after realloc

Started byMaxim Zhukov <mussitantesmortem@gmail.com>
First post2016-04-12 16:10 +0200
Last post2016-04-12 16:10 +0200
Articles 1 — 1 participant

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH 2/2] scripts: dtc: fix memory leak after realloc Maxim Zhukov <mussitantesmortem@gmail.com> - 2016-04-12 16:10 +0200

#1376822 — [PATCH 2/2] scripts: dtc: fix memory leak after realloc

FromMaxim Zhukov <mussitantesmortem@gmail.com>
Date2016-04-12 16:10 +0200
Subject[PATCH 2/2] scripts: dtc: fix memory leak after realloc
Message-ID<rn7aW-al-9@gated-at.bofh.it>
This commit fixed memory leak after errors realloc.

Signed-off-by: Maxim Zhukov <mussitantesmortem@gmail.com>
---
 scripts/dtc/fdtput.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/scripts/dtc/fdtput.c b/scripts/dtc/fdtput.c
index f2197f5..1042319 100644
--- a/scripts/dtc/fdtput.c
+++ b/scripts/dtc/fdtput.c
@@ -75,8 +75,9 @@ static int encode_value(struct display_info *disp, char **arg, int arg_count,
 	char *ptr;		/* pointer to current value position */
 	int len;		/* length of this cell/string/byte */
 	int ival;
-	int upto;	/* the number of bytes we have written to buf */
+	int upto;		/* the number of bytes we have written to buf */
 	char fmt[3];
+	void *save_ptr = NULL;	/* save pointer to realloc */
 
 	upto = 0;
 
@@ -96,12 +97,15 @@ static int encode_value(struct display_info *disp, char **arg, int arg_count,
 		/* enlarge our value buffer by a suitable margin if needed */
 		if (upto + len > value_size) {
 			value_size = (upto + len) + 500;
-			value = realloc(value, value_size);
-			if (!value) {
+			void *save_ptr = realloc(value, value_size);
+
+			if (!save_ptr) {
+				free(value);
 				fprintf(stderr, "Out of mmory: cannot alloc "
 					"%d bytes\n", value_size);
 				return -1;
 			}
+			value = save_ptr;
 		}
 
 		ptr = value + upto;
-- 
2.7.1.1.g3617aa0

[toc] | [standalone]


Back to top | Article view | linux.kernel


csiph-web