Skip to content

Commit 90fb6fb

Browse files
jmarshallpd3
authored andcommitted
Fix error reporting after closing files
Calling bgzf_close() (and hts_close()) frees the file pointer, so the errcode field cannot be accessed afterwards. Report errors as per errno instead as is done in vcfconvert.c. Also report the right file pointer's errcode after bgzf_write() failure.
1 parent aff7d42 commit 90fb6fb

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

reheader.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ static void reheader_vcf_gz(args_t *args)
418418
// Output all remaining data read with the header block
419419
if ( fp->block_length - skip_until > 0 )
420420
{
421-
if ( bgzf_write(bgzf_out, buffer+skip_until, fp->block_length-skip_until)<0 ) error("Error: %d\n",fp->errcode);
421+
if ( bgzf_write(bgzf_out, buffer+skip_until, fp->block_length-skip_until)<0 ) error("Error: %d\n",bgzf_out->errcode);
422422
}
423423
if ( bgzf_flush(bgzf_out)<0 ) error("Error: %d\n",bgzf_out->errcode);
424424

@@ -434,8 +434,8 @@ static void reheader_vcf_gz(args_t *args)
434434
int count = bgzf_raw_write(bgzf_out, buf, nread);
435435
if (count != nread) error("Write failed, wrote %d instead of %d bytes.\n", count,(int)nread);
436436
}
437-
if (bgzf_close(bgzf_out) < 0) error("Error closing %s: %d\n",args->output_fname ? args->output_fname : "-",bgzf_out->errcode);
438-
if (hts_close(args->fp)) error("Error closing %s: %d\n",args->fname,fp->errcode);
437+
if (bgzf_close(bgzf_out) < 0) error("Error closing %s: %s\n",args->output_fname ? args->output_fname : "-",strerror(errno));
438+
if (hts_close(args->fp)) error("Error closing %s: %s\n",args->fname,strerror(errno));
439439
free(buf);
440440
}
441441
static void reheader_vcf(args_t *args)

vcfconcat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ static void naive_concat(args_t *args)
919919
// Output all non-header data that were read together with the header block
920920
if ( fp->block_length - nskip > 0 )
921921
{
922-
if ( bgzf_write(bgzf_out, (char *)fp->uncompressed_block+nskip, fp->block_length-nskip)<0 ) error("\nError: %d\n",fp->errcode);
922+
if ( bgzf_write(bgzf_out, (char *)fp->uncompressed_block+nskip, fp->block_length-nskip)<0 ) error("\nError: %d\n",bgzf_out->errcode);
923923
}
924924
if ( bgzf_flush(bgzf_out)<0 ) error("\nError: %d\n",bgzf_out->errcode);
925925

@@ -952,7 +952,7 @@ static void naive_concat(args_t *args)
952952
}
953953
free(buf);
954954
free(tmp.s);
955-
if (bgzf_close(bgzf_out) < 0) error("Error: %d\n",bgzf_out->errcode);
955+
if (bgzf_close(bgzf_out) < 0) error("Error: %s\n",strerror(errno));
956956
}
957957

958958
static void usage(args_t *args)

0 commit comments

Comments
 (0)