diff options
| author | Peter Jones <pjones@redhat.com> | 2025-03-12 13:58:51 -0400 |
|---|---|---|
| committer | Peter Jones <pjones@redhat.com> | 2025-03-14 11:05:05 -0400 |
| commit | 6dadb70a2a522640db9025c77ef61d8949734c37 (patch) | |
| tree | 7bad6468fd92a0024d4ff0ea115a7b8067b434f7 /generate_sbat_var_defs.c | |
| parent | 76fab7bd7e61e080ac8ae28450cae6f533159086 (diff) | |
| download | efi-boot-shim-6dadb70a2a522640db9025c77ef61d8949734c37.tar.gz efi-boot-shim-6dadb70a2a522640db9025c77ef61d8949734c37.zip | |
generate_sbat_var_defs: Fix memory leak on realloc failure and fd leak.
Resolves: Coverity CID 457502
Signed-off-by: Peter Jones <pjones@redhat.com>
Diffstat (limited to 'generate_sbat_var_defs.c')
| -rw-r--r-- | generate_sbat_var_defs.c | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/generate_sbat_var_defs.c b/generate_sbat_var_defs.c index 3ae62eba..2d8f76de 100644 --- a/generate_sbat_var_defs.c +++ b/generate_sbat_var_defs.c @@ -27,8 +27,9 @@ readfile(char *SbatLevel_Variable) FILE *varfilep; char line[1024]; int date; + int ret = -1; - int revocationsp = 0; + unsigned int revocationsp = 0; sbat_revocation *revlistlast = NULL; sbat_revocation *revlistentry = NULL; @@ -44,7 +45,7 @@ readfile(char *SbatLevel_Variable) revlistentry = (sbat_revocation *)malloc( sizeof(sbat_revocation)); if (revlistentry == NULL) - return -1; + goto err; if (revlisthead == NULL) revlisthead = revlistentry; else @@ -55,11 +56,14 @@ readfile(char *SbatLevel_Variable) revlistentry->date = date; while (line[0] != '\n' && fgets(line, sizeof(line), varfilep) != NULL) { - revlistentry->revocations = (char *)realloc( - revlistentry->revocations, - revocationsp + strlen(line) + 1); - if (revlistentry->revocations == NULL) - return -1; + char *new = NULL; + new = realloc(revlistentry->revocations, + revocationsp + strlen(line) + 1); + if (new == NULL) { + ret = -1; + goto err; + } + revlistentry->revocations = new; if (strlen(line) > 1) { line[strlen(line) - 1] = 0; sprintf(revlistentry->revocations + @@ -73,7 +77,21 @@ readfile(char *SbatLevel_Variable) } } - return 1; + ret = 1; +err: + if (ret < 0 && revlisthead) { + sbat_revocation *rle = revlisthead; + while (rle) { + sbat_revocation *next = rle->next; + if (rle->revocations) + free(rle->revocations); + free(rle); + rle = next; + } + revlisthead = NULL; + } + fclose(varfilep); + return ret; } int |
