[PATCH] perf: free original memory on realloc failure in str_append

2013-07-07 Thread David Ahern
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 
Cc: Andi Kleen 
---
 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

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH] perf: free original memory on realloc failure in str_append

2013-07-07 Thread David Ahern
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 dsah...@gmail.com
Cc: Andi Kleen a...@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

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/