On 3/10/19 2:20 PM, Bruno Haible wrote:
> 2019-03-10  Bruno Haible  <br...@clisp.org>
> 
>       di-set: Fix memory leak.
>       * lib/di-set.c (di_set_free): Free the ino_map through ino_map_free(),
>       not free().
> 
> diff --git a/lib/di-set.c b/lib/di-set.c
> index e8f69db..2c0601e 100644
> --- a/lib/di-set.c
> +++ b/lib/di-set.c
> @@ -136,7 +136,7 @@ void
>  di_set_free (struct di_set *dis)
>  {
>    hash_free (dis->dev_map);
> -  free (dis->ino_map);
> +  ino_map_free (dis->ino_map);
>    free (dis->probe);
>    free (dis);
>  }

This commit (3703dbbe88dd) makes coreutils' du(1) crash.

Reproducer:
---------------------------------------------------------
#!/bin/sh
set -x
git clone --depth=1 git://git.sv.gnu.org/gnulib.git \
  && cd gnulib \
  && rm -rf /tmp/testdir/ \
  && ./gnulib-tool --create-testdir --with-tests --dir=/tmp/testdir di-set \
  && cd /tmp/testdir \
  && ./configure --quiet \
  && make \
  && patch -p0 --fuzz=0 <<<'\
--- gltests/test-di-set.c.ORIG  2019-04-10 08:23:09.663882510 +0200
+++ gltests/test-di-set.c       2019-04-18 22:27:57.778264454 +0200
@@ -27,6 +27,9 @@
 {
   struct di_set *dis = di_set_alloc ();
   ASSERT (dis);
+  di_set_free (dis);
+  dis = di_set_alloc ();
+  ASSERT (dis);

   ASSERT (di_set_lookup (dis, 2, 5) == 0); /* initial lookup fails */
   ASSERT (di_set_insert (dis, 2, 5) == 1); /* first insertion succeeds */
' \
&& make check
---------------------------------------------------------

The attached allows for 'ino-map' to free on NULL.

Have a nice day,
Berny


>From c6f24d39f25cbedfba6f728da48e9666d7240894 Mon Sep 17 00:00:00 2001
From: Bernhard Voelker <m...@bernhard-voelker.de>
Date: Thu, 18 Apr 2019 23:04:26 +0200
Subject: [PATCH] ino-map: allow free on NULL

* lib/ino-map.c (ino_map_free): Do nothing when MAP is NULL.
Since commit 3703dbbe88dd, di_set_free calls ino_map_free which may
be NULL.
---
 ChangeLog     | 7 +++++++
 lib/ino-map.c | 9 ++++++---
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 398c33968..1d96402d8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2019-04-18  Bernhard Voelker  <m...@bernhard-voelker.de>
+
+	ino-map: allow free on NULL.
+	* lib/ino-map.c (ino_map_free): Do nothing when MAP is NULL.
+	Since commit 3703dbbe88dd, di_set_free calls ino_map_free which may
+	be NULL.
+
 2019-04-14  Paul Eggert  <egg...@cs.ucla.edu>
 
 	* lib/str-two-way.h: Fix comment typo.
diff --git a/lib/ino-map.c b/lib/ino-map.c
index c48defa65..9d6bd0894 100644
--- a/lib/ino-map.c
+++ b/lib/ino-map.c
@@ -105,9 +105,12 @@ ino_map_alloc (size_t next_mapped_ino)
 void
 ino_map_free (struct ino_map *map)
 {
-  hash_free (map->map);
-  free (map->probe);
-  free (map);
+  if (map)
+    {
+      hash_free (map->map);
+      free (map->probe);
+      free (map);
+    }
 }
 
 
-- 
2.21.0

Reply via email to