From aae85b2c17517714e0b9dcd3953972b74e6a7e5e Mon Sep 17 00:00:00 2001 From: Alexander Potashev Date: Fri, 27 May 2011 04:19:40 +0400 Subject: [PATCH 5/5] Fix memory leak: string_list_append duplicates the string string_list_append works like before. string_list_append_nodup works almost like string_list_append, but takes ownership of the string passed into it, i.e. it does not run xstrdup() unlike string_list_append. --- po-gram-gen.y | 8 ++++---- str-list.c | 11 +++++++++-- str-list.h | 2 ++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/po-gram-gen.y b/po-gram-gen.y index e87d530..d29a8d5 100644 --- a/po-gram-gen.y +++ b/po-gram-gen.y @@ -411,7 +411,7 @@ string_list : STRING { string_list_init (&$$.stringlist); - string_list_append (&$$.stringlist, $1.string); + string_list_append_nodup (&$$.stringlist, $1.string); $$.pos = $1.pos; $$.obsolete = $1.obsolete; } @@ -419,7 +419,7 @@ string_list { check_obsolete ($1, $2); $$.stringlist = $1.stringlist; - string_list_append (&$$.stringlist, $2.string); + string_list_append_nodup (&$$.stringlist, $2.string); $$.pos = $1.pos; $$.obsolete = $1.obsolete; } @@ -429,7 +429,7 @@ prev_string_list : PREV_STRING { string_list_init (&$$.stringlist); - string_list_append (&$$.stringlist, $1.string); + string_list_append_nodup (&$$.stringlist, $1.string); $$.pos = $1.pos; $$.obsolete = $1.obsolete; } @@ -437,7 +437,7 @@ prev_string_list { check_obsolete ($1, $2); $$.stringlist = $1.stringlist; - string_list_append (&$$.stringlist, $2.string); + string_list_append_nodup (&$$.stringlist, $2.string); $$.pos = $1.pos; $$.obsolete = $1.obsolete; } diff --git a/str-list.c b/str-list.c index 3f929c2..f3c5b07 100644 --- a/str-list.c +++ b/str-list.c @@ -57,7 +57,7 @@ string_list_alloc () /* Append a single string to the end of a list of strings. */ void -string_list_append (string_list_ty *slp, const char *s) +string_list_append_nodup (string_list_ty *slp, const char *s) { /* Grow the list. */ if (slp->nitems >= slp->nitems_max) @@ -70,7 +70,14 @@ string_list_append (string_list_ty *slp, const char *s) } /* Add a copy of the string to the end of the list. */ - slp->item[slp->nitems++] = xstrdup (s); + slp->item[slp->nitems++] = s; +} + + +void +string_list_append (string_list_ty *slp, const char *s) +{ + string_list_append_nodup (slp, xstrdup (s)); } diff --git a/str-list.h b/str-list.h index 5b7b75d..1d40dbb 100644 --- a/str-list.h +++ b/str-list.h @@ -48,6 +48,8 @@ extern void string_list_init (string_list_ty *slp); extern string_list_ty *string_list_alloc (void); /* Append a single string to the end of a list of strings. */ +extern void string_list_append_nodup (string_list_ty *slp, const char *s); + extern void string_list_append (string_list_ty *slp, const char *s); /* Append a single string to the end of a list of strings, unless it is -- 1.7.5.rc3