From patchwork Tue May 15 19:02:52 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: pci: aspm: free memory from lists Date: Tue, 15 May 2012 09:02:52 -0000 From: Colin King X-Patchwork-Id: 159414 Message-Id: <1337108572-4455-1-git-send-email-colin.king@canonical.com> To: fwts-devel@lists.ubuntu.com From: Colin Ian King free lspci text lists and pci device lists correctly. valgrind found a bunch of memory allocations that were not being freed. Signed-off-by: Colin Ian King Acked-by: Alex Hung Acked-by: Keng-Yu Lin Acked-by: Ivan Hu --- src/pci/aspm/aspm.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/pci/aspm/aspm.c b/src/pci/aspm/aspm.c index c461ce8..fe6cb60 100644 --- a/src/pci/aspm/aspm.c +++ b/src/pci/aspm/aspm.c @@ -188,6 +188,17 @@ int pcie_compare_rp_dev_aspm_registers(fwts_framework *fw, return ret; } +static void pci_device_list_free(struct pci_device *head) +{ + struct pci_device *next; + + while (head) { + next = head->next; + free(head); + head = next; + } +} + int pcie_check_aspm_registers(fwts_framework *fw, const fwts_log_level level) { @@ -212,8 +223,11 @@ int pcie_check_aspm_registers(fwts_framework *fw, char *pEnd; device = (struct pci_device *) calloc(1, sizeof(*device)); - if (device == NULL) + if (device == NULL) { + pci_device_list_free(head); + fwts_text_list_free(lspci_output); return FWTS_ERROR; + } device->bus = strtol(line, &pEnd, 16); device->dev = strtol(pEnd + 1, &pEnd, 16); @@ -226,6 +240,7 @@ int pcie_check_aspm_registers(fwts_framework *fw, cur = device; } + fwts_text_list_free(lspci_output); for (cur = head; cur; cur = cur->next) { int reg_loc = 0, reg_val = 0; @@ -235,10 +250,13 @@ int pcie_check_aspm_registers(fwts_framework *fw, fw->lspci, cur->bus, cur->dev, cur->func); if (fwts_pipe_exec(command, &lspci_output) == FWTS_EXEC_ERROR) { fwts_log_warning(fw, "Could not execute %s", command); + pci_device_list_free(head); return FWTS_ERROR; } - if (lspci_output == NULL) + if (lspci_output == NULL) { + pci_device_list_free(head); return FWTS_ERROR; + } fwts_list_foreach(item, lspci_output) { char *line = fwts_text_list_text(item); @@ -253,6 +271,7 @@ int pcie_check_aspm_registers(fwts_framework *fw, } } } + fwts_text_list_free(lspci_output); } /* Check aspm registers from the list of pci devices */ @@ -275,14 +294,7 @@ int pcie_check_aspm_registers(fwts_framework *fw, } } - cur = head; - while (cur != NULL) { - device = cur->next; - free(cur); - cur = device; - } - - fwts_text_list_free(lspci_output); + pci_device_list_free(head); return ret; }