All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] lib/colors: fix double free on error path
@ 2014-11-19 11:03 Andreas Henriksson
  2014-11-19 13:19 ` Karel Zak
  0 siblings, 1 reply; 2+ messages in thread
From: Andreas Henriksson @ 2014-11-19 11:03 UTC (permalink / raw)
  To: util-linux; +Cc: Andreas Henriksson

The error path was a bit unclear on the semantics. The seq argument
would get freed but not the name argument. Then the caller frees
them both when the function returns an error.
This fixes the problem my making the function not touch the arguments
unless we're going to return success. Also improve the description
for this function to make it more obvious what the callers should expect.

Addresses: CID#77487 (on scan.coverity.com for JIghtuse/util-linux)
Signed-off-by: Andreas Henriksson <andreas@fatal.se>
---
 lib/colors.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/lib/colors.c b/lib/colors.c
index fb7d4b2..ab62c56 100644
--- a/lib/colors.c
+++ b/lib/colors.c
@@ -438,8 +438,9 @@ static int cn_sequence(const char *str, char **seq)
 
 
 /*
- * Adds one color sequence to array with color scheme,
- * @seq and @name have to be allocated strings
+ * Adds one color sequence to array with color scheme.
+ * When returning success (0) this function takes ownership of
+ * @seq and @name, which have to be allocated strings.
  */
 static int colors_add_scheme(struct ul_color_ctl *cc,
 			     char *name,
@@ -455,7 +456,6 @@ static int colors_add_scheme(struct ul_color_ctl *cc,
 	DBG(SCHEME, ul_debug("add '%s'", name));
 
 	rc = cn_sequence(seq0, &seq);
-	free(seq0);
 	if (rc)
 		return rc;
 
@@ -485,6 +485,8 @@ static int colors_add_scheme(struct ul_color_ctl *cc,
 		cc->schemes_sz = cc->nschemes + 10;
 	}
 
+	free(seq0);
+
 	/* add a new item */
 	cs = &cc->schemes[cc->nschemes++];
 	cs->name = name;
-- 
2.1.3


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] lib/colors: fix double free on error path
  2014-11-19 11:03 [PATCH] lib/colors: fix double free on error path Andreas Henriksson
@ 2014-11-19 13:19 ` Karel Zak
  0 siblings, 0 replies; 2+ messages in thread
From: Karel Zak @ 2014-11-19 13:19 UTC (permalink / raw)
  To: Andreas Henriksson; +Cc: util-linux

On Wed, Nov 19, 2014 at 12:03:35PM +0100, Andreas Henriksson wrote:
>  lib/colors.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)

 Applied, thanks!

    Karel
-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-11-19 13:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-19 11:03 [PATCH] lib/colors: fix double free on error path Andreas Henriksson
2014-11-19 13:19 ` Karel Zak

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.