devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Maxim Zhukov <mussitantesmortem@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Michal Marek <mmarek@suse.com>, Rob Herring <robh+dt@kernel.org>,
	Frank Rowand <frowand.list@gmail.com>,
	Grant Likely <grant.likely@linaro.org>,
	Maxim Zhukov <mussitantesmortem@gmail.com>,
	devicetree-compiler@vger.kernel.org
Subject: [PATCH V2 2/2] scripts: dtc: fix memory leak after realloc
Date: Tue, 12 Apr 2016 23:55:00 +0300	[thread overview]
Message-ID: <1460494500-26922-3-git-send-email-mussitantesmortem@gmail.com> (raw)
In-Reply-To: <1460494500-26922-1-git-send-email-mussitantesmortem@gmail.com>

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


       reply	other threads:[~2016-04-12 20:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1460494500-26922-1-git-send-email-mussitantesmortem@gmail.com>
2016-04-12 20:55 ` Maxim Zhukov [this message]
     [not found]   ` <1460494500-26922-3-git-send-email-mussitantesmortem-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2016-04-12 21:11     ` [PATCH V2 2/2] scripts: dtc: fix memory leak after realloc Rob Herring
     [not found]       ` <CAL_JsqKqbj+FmD2SwSsmdBW6SPoJzsP5an1o_XZAozo86ssPCg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-04-13  4:17         ` David Gibson
2016-04-12 21:29     ` (unknown), Maxim Zhukov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1460494500-26922-3-git-send-email-mussitantesmortem@gmail.com \
    --to=mussitantesmortem@gmail.com \
    --cc=devicetree-compiler@vger.kernel.org \
    --cc=frowand.list@gmail.com \
    --cc=grant.likely@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mmarek@suse.com \
    --cc=robh+dt@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).