From: David Ahern <dsahern@gmail.com>
To: acme@ghostprotocols.net, linux-kernel@vger.kernel.org
Cc: David Ahern <dsahern@gmail.com>, Andi Kleen <ak@linux.intel.com>
Subject: [PATCH] perf: free original memory on realloc failure in str_append
Date: Sun, 7 Jul 2013 14:43:53 -0600 [thread overview]
Message-ID: <1373229833-6157-1-git-send-email-dsahern@gmail.com> (raw)
str_append was added in 578d9c6. If realloc fails orignal block is
not freed. Need to save the original pointer to handle on failure.
Signed-off-by: David Ahern <dsahern@gmail.com>
Cc: Andi Kleen <ak@linux.intel.com>
---
tools/perf/util/string.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/tools/perf/util/string.c b/tools/perf/util/string.c
index f0b0c00..53bca50 100644
--- a/tools/perf/util/string.c
+++ b/tools/perf/util/string.c
@@ -398,13 +398,20 @@ int str_append(char **s, int *len, const char *a)
{
int olen = *s ? strlen(*s) : 0;
int nlen = olen + strlen(a) + 1;
+ char *p;
+
if (*len < nlen) {
*len = *len * 2;
if (*len < nlen)
*len = nlen;
+
+ p = *s;
*s = realloc(*s, *len);
- if (!*s)
+ if (!*s) {
+ free(p);
return -ENOMEM;
+ }
+
if (olen == 0)
**s = 0;
}
--
1.7.10.1
reply other threads:[~2013-07-07 20:44 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=1373229833-6157-1-git-send-email-dsahern@gmail.com \
--to=dsahern@gmail.com \
--cc=acme@ghostprotocols.net \
--cc=ak@linux.intel.com \
--cc=linux-kernel@vger.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