From 22b51c850f98dc31fc3a2f0e2c18d844e7466ab0 Mon Sep 17 00:00:00 2001 From: RichardG867 Date: Thu, 24 Oct 2024 10:32:24 -0300 Subject: [PATCH] pcireg: Properly allocate and deallocate decompression buffers --- pcireg/pcireg.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/pcireg/pcireg.c b/pcireg/pcireg.c index f70820e..a8686ed 100644 --- a/pcireg/pcireg.c +++ b/pcireg/pcireg.c @@ -180,7 +180,7 @@ pciids_open_database(void **ptr, char id) char *filename; char target_filename[13]; unsigned short crc; - uint8_t *buf; + uint8_t *buf = NULL; /* No action is required if the database is already loaded. */ if (*ptr) @@ -189,10 +189,8 @@ pciids_open_database(void **ptr, char id) /* Open archive, and stop if the open failed. */ f = fopen("PCIIDS.LHA", "r" FOPEN_BINARY); - if (!f) { - f = NULL; + if (!f) return 1; - } /* Generate target filename. */ strcpy(target_filename, "PCIIDS_@.BIN"); @@ -214,15 +212,13 @@ pciids_open_database(void **ptr, char id) crc = !strcmp(filename, target_filename); free(filename); if (crc) { - /* Allocate buffer for reading the compressed data. */ - buf = malloc(packed_size); - if (!buf) - goto fail; - - /* Allocate buffer for the decompressed data. */ + /* Allocate buffers for the compressed and decompressed data. */ *ptr = malloc(original_size); if (!*ptr) goto fail; + buf = malloc(packed_size); + if (!buf) + goto fail; /* Read and decompress data. */ fseek(f, pos + header_size, SEEK_SET); @@ -233,6 +229,7 @@ pciids_open_database(void **ptr, char id) /* All done, close archive. */ fclose(f); + free(buf); return 0; } @@ -244,6 +241,12 @@ pciids_open_database(void **ptr, char id) /* Entry not found or read/decompression failed. */ printf("PCI ID database %c decompression failed\n", id); fclose(f); + if (*ptr) { + free(*ptr); + *ptr = NULL; + } + if (buf) + free(buf); return 1; }