From 0e8c19b115797ff934e84c105ef4357a899bc973 Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Wed, 12 Feb 2020 10:54:27 -0600 Subject: DiskMan: Fix all memory leaks --- diskman/disk.cc | 14 +++++++++----- diskman/diskman.cc | 9 +++++++-- diskman/partition.cc | 17 ++++++++++++----- 3 files changed, 28 insertions(+), 12 deletions(-) (limited to 'diskman') diff --git a/diskman/disk.cc b/diskman/disk.cc index 8006052..329edaa 100644 --- a/diskman/disk.cc +++ b/diskman/disk.cc @@ -111,6 +111,7 @@ Disk::Disk(void *creation, int type, bool partition) { fdisk_table_get_partition(parts, next); _partitions.push_back(Partition(*this, part, 0)); } + fdisk_unref_table(parts); } } else if(type == 0) { /* fallback to udev, if available */ @@ -120,8 +121,7 @@ Disk::Disk(void *creation, int type, bool partition) { struct udev *udev = udev_device_get_udev(device); struct udev_enumerate *part_enum = udev_enumerate_new(udev); if(part_enum != NULL) { - struct udev_list_entry *first, *item; - struct udev_device *part_device = NULL; + struct udev_list_entry *first; udev_enumerate_add_match_subsystem(part_enum, "block"); udev_enumerate_add_match_property(part_enum, "DEVTYPE", @@ -131,13 +131,17 @@ Disk::Disk(void *creation, int type, bool partition) { first = udev_enumerate_get_list_entry(part_enum); if(first != NULL) { + struct udev_list_entry *item; udev_list_entry_foreach(item, first) { const char *path = udev_list_entry_get_name(item); - if(part_device != NULL) udev_device_unref(part_device); - part_device = udev_device_new_from_syspath(udev, path); - _partitions.push_back(Partition(*this, part_device, 1)); + struct udev_device *part_device = udev_device_new_from_syspath(udev, path); + if(part_device != nullptr) { + _partitions.push_back(Partition(*this, part_device, 1)); + udev_device_unref(part_device); + } } } + udev_enumerate_unref(part_enum); } } else { std::cerr << "Cannot load partitions for " << _name << std::endl; diff --git a/diskman/diskman.cc b/diskman/diskman.cc index a7d4a26..ee04911 100644 --- a/diskman/diskman.cc +++ b/diskman/diskman.cc @@ -52,14 +52,14 @@ std::vector DiskMan::find_disks(bool include_part, bool include_vg, udev_enumerate_add_match_property(disk_enum, "DEVTYPE", "disk"); udev_enumerate_scan_devices(disk_enum); first = udev_enumerate_get_list_entry(disk_enum); - if(first == NULL) { + if(first == nullptr) { std::cerr << "No block devices found" << std::endl; return {}; } udev_list_entry_foreach(item, first) { const char *path = udev_list_entry_get_name(item); - if(device != NULL) udev_device_unref(device); + if(device != nullptr) udev_device_unref(device); device = udev_device_new_from_syspath(pImpl->udev, path); std::string name(udev_device_get_sysname(device)); if(name.compare(0, 4, "loop") == 0 @@ -78,6 +78,11 @@ std::vector DiskMan::find_disks(bool include_part, bool include_vg, disks.push_back(Disk(device, 0, include_part)); } + if(device != nullptr) { + udev_device_unref(device); + } + udev_enumerate_unref(disk_enum); + return disks; } diff --git a/diskman/partition.cc b/diskman/partition.cc index 308d002..ba94258 100644 --- a/diskman/partition.cc +++ b/diskman/partition.cc @@ -32,13 +32,20 @@ Partition::Partition(Disk &d, void *creation, int type) { } else { this->_size = 0; } - const char *name = fdisk_partname(d.node().c_str(), - fdisk_partition_get_partno(part) + 1); - const char *value; + char *name = fdisk_partname(d.node().c_str(), + fdisk_partition_get_partno(part) + 1); + char *value; value = blkid_get_tag_value(nullptr, "TYPE", name); - if(value != nullptr) this->_fs_type = std::string(value); + if(value != nullptr) { + this->_fs_type = std::string(value); + free(value); + } value = blkid_get_tag_value(nullptr, "LABEL", name); - if(value != nullptr) this->_label = std::string(value); + if(value != nullptr) { + this->_label = std::string(value); + free(value); + } + free(name); break; } case 1: { /* udev */ -- cgit v1.2.3-60-g2f50