Dear list,

q?copy_file_preserving is leaking a block, each time it is called with a
bogus filename:

#include <config.h>
#include <copy-file.h>

int
main (void)
{
  qcopy_file_preserving ("/tmp/some_non_existent_file", "/tmp/vlevle");
}

will produce in valgrind:

==5703== 32,768 bytes in 1 blocks are definitely lost in loss record 1 of 1
==5703==    at 0x4C28C20: malloc (vg_replace_malloc.c:296)
==5703==    by 0x402C0D: xmalloc (xmalloc.c:41)
==5703==    by 0x400EB4: qcopy_file_preserving (copy-file.c:61)
==5703==    by 0x400E88: main (copy-file.c:7)

Attached a fix that adds the missing call to free.

Thanks,
Simon
From 05e6b06bc957c826f6d424fd56f308a3d545ea93 Mon Sep 17 00:00:00 2001
From: Simon Reinhardt <simon.reinha...@stud.uni-regensburg.de>
Date: Thu, 15 Oct 2015 17:01:14 +0200
Subject: [PATCH] copy-file: On error, add missing call to free.

---
 lib/copy-file.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lib/copy-file.c b/lib/copy-file.c
index da84c6c..b0cd850 100644
--- a/lib/copy-file.c
+++ b/lib/copy-file.c
@@ -101,6 +101,8 @@ qcopy_file_preserving (const char *src_filename, const char *dest_filename)
     }
 
   free (buf);
+  /* Set to NULL to avoid double-free below.  */
+  buf = NULL;
 
 #if !USE_ACL
   if (close (dest_fd) < 0)
@@ -174,6 +176,7 @@ qcopy_file_preserving (const char *src_filename, const char *dest_filename)
  error_src:
   close (src_fd);
  error:
+  free (buf);
   return err;
 }
 
-- 
2.1.4

Reply via email to