Skip to content

Commit

Permalink
pcireg: Properly allocate and deallocate decompression buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
richardg867 committed Oct 24, 2024
1 parent e5c2056 commit 22b51c8
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions pcireg/pcireg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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, "[email protected]");
Expand All @@ -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);
Expand All @@ -233,6 +229,7 @@ pciids_open_database(void **ptr, char id)

/* All done, close archive. */
fclose(f);
free(buf);
return 0;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 22b51c8

Please sign in to comment.