[parted-devel] [PATCH] Avoid a leak.

Jim Meyering jim at meyering.net
Tue May 29 19:33:31 UTC 2007


I noticed a leak, fixed it, and added a test.
The test must be run via valgrind to verify that there is no leak.
I plan to enable running the entire test suite with valgrind enabled,
but this is enough for now:

	* parted/parted.c (do_print): Use separate variables for the header
	and for individual rows.  Free each when done.
	* parted/table.c (table_add_row_from_strlist): Insert a newly
	allocated copy of each string, so that the caller can free
	the argument corresponding to the "list" parameter.
	* tests/t0100-print.sh: New test for this.
	* tests/Makefile.am (TESTS): Add t0100-print.sh.

Signed-off-by: Jim Meyering <jim at meyering.net>
---
 parted/parted.c      |   23 ++++++++++---------
 parted/table.c       |    5 +++-
 tests/Makefile.am    |    1 +
 tests/t0100-print.sh |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 75 insertions(+), 12 deletions(-)
 create mode 100755 tests/t0100-print.sh

diff --git a/parted/parted.c b/parted/parted.c
index 202558a..92e820d 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -1258,7 +1258,6 @@ do_print (PedDevice** dev)
         PedUnit         default_unit;
         PedDisk*        disk;
         Table*          table;
-        StrList*        row;
         int             has_extended;
         int             has_name;
         int             has_devices_arg = 0;
@@ -1404,30 +1403,30 @@ do_print (PedDevice** dev)
         
         PedPartition* part;
         if (!opt_machine_mode) {
+            StrList *row1;
 
             if (ped_unit_get_default() == PED_UNIT_CHS) {
-                    row = str_list_create (_("Number"), _("Start"),
+                    row1 = str_list_create (_("Number"), _("Start"),
                                                _("End"), NULL);
             } else {
-                    row = str_list_create (_("Number"), _("Start"),
+                    row1 = str_list_create (_("Number"), _("Start"),
                                                _("End"), _("Size"), NULL);
             }
 
             if (has_extended)
-                    str_list_append (row, _("Type"));
+                    str_list_append (row1, _("Type"));
 
-            str_list_append (row, _("File system"));
+            str_list_append (row1, _("File system"));
 
             if (has_name)
-                    str_list_append (row, _("Name"));
+                    str_list_append (row1, _("Name"));
 
-            str_list_append (row, _("Flags"));
+            str_list_append (row1, _("Flags"));
 
 
-            table = table_new (str_list_length(row));
-
-            table_add_row_from_strlist (table, row);
+            table = table_new (str_list_length(row1));
 
+            table_add_row_from_strlist (table, row1);
 
             for (part = ped_disk_next_partition (disk, NULL); part;
                  part = ped_disk_next_partition (disk, part)) {
@@ -1443,7 +1442,7 @@ do_print (PedDevice** dev)
                     else
                             sprintf (tmp, "%2s ", "");
 
-                    row = str_list_create (tmp, NULL);
+                    StrList *row = str_list_create (tmp, NULL);
 
                     start = ped_unit_format (*dev, part->geom.start);
                     end = ped_unit_format_byte (
@@ -1485,6 +1484,7 @@ do_print (PedDevice** dev)
 
                     //PED_ASSERT (row.cols == caption.cols)
                     table_add_row_from_strlist (table, row);
+                    str_list_destroy (row);
             }
 
             table_rendered = table_render (table); 
@@ -1495,6 +1495,7 @@ do_print (PedDevice** dev)
 #endif
             ped_free (table_rendered);
             table_destroy (table);
+            str_list_destroy (row1);
 
         } else {
     
diff --git a/parted/table.c b/parted/table.c
index 23976a9..befb045 100644
--- a/parted/table.c
+++ b/parted/table.c
@@ -164,7 +164,10 @@ void table_add_row_from_strlist (Table* t, StrList* list)
 
         while (list)
         {
-                row[i] = (wchar_t*)list->str;
+                row[i] = wcsdup (list->str);
+                if (row[i] == NULL)
+                        xalloc_die ();
+
 
                 list = list->next;
                 ++i;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 27689c9..ecce727 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,5 +1,6 @@
 TESTS = \
   t0000-basic.sh \
+  t0100-print.sh \
   t1000-mkpartfs.sh \
   t1100-busy-label.sh \
   t1500-small-ext2.sh \
diff --git a/tests/t0100-print.sh b/tests/t0100-print.sh
new file mode 100755
index 0000000..74c478e
--- /dev/null
+++ b/tests/t0100-print.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+# Copyright (C) 2007 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+test_description="the most basic 'print' test"
+
+. ./init.sh
+
+dev=loop-file
+
+msdos_magic='\x55\xaa'
+
+test_expect_success \
+    "setup: create the most basic partition table, manually" \
+    '{ dd if=/dev/zero bs=510 count=1; printf "$msdos_magic"; } > $dev'
+
+test_expect_success \
+    'print the empty table' \
+    'parted -s $dev print >out 2>&1'
+
+pwd=`pwd`
+
+fail=0
+{
+  cat <<EOF
+Model:  (file)
+Disk .../$dev: 512B
+Sector size (logical/physical): 512B/512B
+Partition Table: msdos
+
+Number  Start  End  Size  Type  File system  Flags
+
+EOF
+} > exp || fail=1
+
+test_expect_success \
+    'prepare actual and expected output' \
+    'test $fail = 0 &&
+     mv out o2 && sed "s,^Disk .*/$dev:,Disk .../$dev:," o2 > out'
+
+test_expect_success 'check for expected output' '$compare out exp'
+
+test_done
-- 
1.5.2.73.g18bece




More information about the parted-devel mailing list