From 577dc345653947a31b2841167dc1a9be0e44e043 Mon Sep 17 00:00:00 2001
From: Pavel Raiskup <praiskup@redhat.com>
Date: Tue, 31 Jul 2018 12:06:09 +0300
Subject: [PATCH 20/58] Avoid some resource leaks

* src/incremen.c (store_rename): Free temp_name, leaked before for
each renamed directory with --listed-incremental.
* src/transform.c (add_literal_segment): Tighten arguments by
const.
(parse_transform_expr): Free 'str', leaked storage for each
--transform option before.
* src/utf8.c (utf8_convert): Deallocate buffer for failed iconv()
call so callers don't have to.
---
 src/incremen.c  | 1 +
 src/transform.c | 3 ++-
 src/utf8.c      | 9 ++++++---
 3 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/incremen.c b/src/incremen.c
index ca611f6..7c26eb8 100644
--- a/src/incremen.c
+++ b/src/incremen.c
@@ -915,6 +915,7 @@ store_rename (struct directory *dir, struct obstack *stk)
 	    obstack_code_rename (stk, p->orig->name, p->name);
 
 	  obstack_code_rename (stk, "", prev->name);
+	  free (temp_name);
 	}
     }
 }
diff --git a/src/transform.c b/src/transform.c
index 3fae3c0..6ef0da6 100644
--- a/src/transform.c
+++ b/src/transform.c
@@ -101,7 +101,7 @@ add_segment (struct transform *tf)
 }
 
 static void
-add_literal_segment (struct transform *tf, char *str, char *end)
+add_literal_segment (struct transform *tf, const char *str, const char *end)
 {
   size_t len = end - str;
   if (len)
@@ -403,6 +403,7 @@ parse_transform_expr (const char *expr)
 	cur++;
     }
   add_literal_segment (tf, beg, cur);
+  free(str);
 
   return p;
 }
diff --git a/src/utf8.c b/src/utf8.c
index a018ce0..168d636 100644
--- a/src/utf8.c
+++ b/src/utf8.c
@@ -68,7 +68,6 @@ utf8_convert (bool to_utf, char const *input, char **output)
   char *ob;
   size_t inlen;
   size_t outlen;
-  size_t rc;
   iconv_t cd = utf8_init (to_utf);
 
   if (cd == 0)
@@ -83,9 +82,13 @@ utf8_convert (bool to_utf, char const *input, char **output)
   outlen = inlen * MB_LEN_MAX + 1;
   ob = *output = xmalloc (outlen);
   ib = (char ICONV_CONST *) input;
-  rc = iconv (cd, &ib, &inlen, &ob, &outlen);
+  if (iconv (cd, &ib, &inlen, &ob, &outlen) == -1)
+    {
+      free (*output);
+      return false;
+    }
   *ob = 0;
-  return rc != -1;
+  return true;
 }
 
 
-- 
2.19.1

