Skip to content

Commit

Permalink
Remove memory leak when cram_encode_container fails during a close.
Browse files Browse the repository at this point in the history
We now continue the close process, freeing up everything else as we go.
  • Loading branch information
jkbonfield committed Nov 16, 2023
1 parent 20510eb commit 8557b64
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions cram/cram_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -5533,7 +5533,7 @@ int cram_write_eof_block(cram_fd *fd) {
*/
int cram_close(cram_fd *fd) {
spare_bams *bl, *next;
int i;
int i, ret = 0;

if (!fd)
return -1;
Expand All @@ -5543,7 +5543,7 @@ int cram_close(cram_fd *fd) {
cram_update_curr_slice(fd->ctr, fd->version);

if (-1 == cram_flush_container_mt(fd, fd->ctr))
return -1;
ret = -1;
}

if (fd->mode != 'w')
Expand All @@ -5553,7 +5553,7 @@ int cram_close(cram_fd *fd) {
hts_tpool_process_flush(fd->rqueue);

if (0 != cram_flush_result(fd))
return -1;
ret = -1;

if (fd->mode == 'w')
fd->ctr = NULL; // prevent double freeing
Expand All @@ -5571,7 +5571,7 @@ int cram_close(cram_fd *fd) {
if (fd->mode == 'w') {
/* Write EOF block */
if (0 != cram_write_eof_block(fd))
return -1;
ret = -1;
}

for (bl = fd->bl; bl; bl = next) {
Expand All @@ -5587,7 +5587,7 @@ int cram_close(cram_fd *fd) {
}

if (hclose(fd->fp) != 0)
return -1;
ret = -1;

if (fd->file_def)
cram_free_file_def(fd->file_def);
Expand Down Expand Up @@ -5631,10 +5631,11 @@ int cram_close(cram_fd *fd) {

if (fd->idxfp)
if (bgzf_close(fd->idxfp) < 0)
return -1;
ret = -1;

free(fd);
return 0;

return ret;
}

/*
Expand Down

0 comments on commit 8557b64

Please sign in to comment.