From d69cb33b4658985ba53a2d03fd5557da07cd91fe Mon Sep 17 00:00:00 2001 From: Heng Li Date: Tue, 5 May 2009 14:10:30 +0000 Subject: [PATCH] * samtools-0.1.3-13 (r260) * report error for file I/O error --- bam_plcmd.c | 4 ++-- bamtk.c | 2 +- bgzf.c | 8 ++++++-- sam.c | 8 ++++++++ sam_view.c | 12 +++++++++--- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/bam_plcmd.c b/bam_plcmd.c index 23e0dd0..7bc422f 100644 --- a/bam_plcmd.c +++ b/bam_plcmd.c @@ -304,8 +304,8 @@ int bam_pileup(int argc, char *argv[]) { samfile_t *fp; fp = fn_list? samopen(argv[optind], "r", fn_list) : samopen(argv[optind], "rb", 0); - if (fp->header == 0) { - fprintf(stderr, "[bam_pileup] fail to read the BAM header. Abort!\n"); + if (fp == 0 || fp->header == 0) { + fprintf(stderr, "[bam_pileup] fail to read the header: non-exisiting file or wrong format.\n"); return 1; } d->h = fp->header; diff --git a/bamtk.c b/bamtk.c index 2125ee4..a8972f9 100644 --- a/bamtk.c +++ b/bamtk.c @@ -3,7 +3,7 @@ #include "bam.h" #ifndef PACKAGE_VERSION -#define PACKAGE_VERSION "0.1.3-12 (r259)" +#define PACKAGE_VERSION "0.1.3-13 (r260)" #endif int bam_taf2baf(int argc, char *argv[]); diff --git a/bgzf.c b/bgzf.c index 4314c70..1b5cebe 100644 --- a/bgzf.c +++ b/bgzf.c @@ -86,7 +86,9 @@ BGZF* open_read(int fd) { FILE* file = fdopen(fd, "r"); - BGZF* fp = malloc(sizeof(BGZF)); + BGZF* fp; + if (file == 0) return 0; + fp = malloc(sizeof(BGZF)); fp->file_descriptor = fd; fp->open_mode = 'r'; fp->owned_file = 0; @@ -107,7 +109,9 @@ BGZF* open_write(int fd) { FILE* file = fdopen(fd, "w"); - BGZF* fp = malloc(sizeof(BGZF)); + BGZF* fp; + if (file == 0) return 0; + fp = malloc(sizeof(BGZF)); fp->file_descriptor = fd; fp->open_mode = 'w'; fp->owned_file = 0; diff --git a/sam.c b/sam.c index 8534339..dd6cd3a 100644 --- a/sam.c +++ b/sam.c @@ -79,9 +79,11 @@ samfile_t *samopen(const char *fn, const char *mode, const void *aux) if (mode[1] == 'b') { // binary fp->type |= TYPE_BAM; fp->x.bam = strcmp(fn, "-")? bam_open(fn, "r") : bam_dopen(fileno(stdin), "r"); + if (fp->x.bam == 0) goto open_err_ret; fp->header = bam_header_read(fp->x.bam); } else { fp->x.tamr = sam_open(fn); + if (fp->x.tamr == 0) goto open_err_ret; fp->header = sam_header_read2(fn_list); } } else if (mode[0] == 'w') { @@ -89,12 +91,14 @@ samfile_t *samopen(const char *fn, const char *mode, const void *aux) if (mode[1] == 'b') { // binary fp->type |= TYPE_BAM; fp->x.bam = strcmp(fn, "-")? bam_open(fn, "w") : bam_dopen(fileno(stdout), "w"); + if (fp->x.bam == 0) goto open_err_ret; bam_header_write(fp->x.bam, fp->header); } else { int i; bam_header_t *alt = 0; alt = bam_header_parse(fp->header->text); fp->x.tamw = strcmp(fn, "-")? fopen(fn, "w") : stdout; + if (fp->x.tamr == 0) goto open_err_ret; if (alt) { if (alt->n_targets != fp->header->n_targets) fprintf(stderr, "[samopen] inconsistent number of target sequences.\n"); @@ -107,6 +111,10 @@ samfile_t *samopen(const char *fn, const char *mode, const void *aux) } } return fp; + +open_err_ret: + free(fp); + return 0; } void samclose(samfile_t *fp) diff --git a/sam_view.c b/sam_view.c index 2df0530..cefac2f 100644 --- a/sam_view.c +++ b/sam_view.c @@ -21,7 +21,7 @@ static int usage(void); int main_samview(int argc, char *argv[]) { int c, is_header = 0, is_header_only = 0, is_bamin = 1, ret = 0; - samfile_t *in, *out; + samfile_t *in = 0, *out = 0; char in_mode[4], out_mode[4], *fn_out = 0, *fn_list = 0; /* parse command-line options */ @@ -45,8 +45,14 @@ int main_samview(int argc, char *argv[]) if (argc == optind) return usage(); // open file handlers - in = samopen(argv[optind], in_mode, fn_list); - out = samopen(fn_out? fn_out : "-", out_mode, in->header); + if ((in = samopen(argv[optind], in_mode, fn_list)) == 0) { + fprintf(stderr, "[main_samview] fail to open file for reading.\n"); + goto view_end; + } + if ((out = samopen(fn_out? fn_out : "-", out_mode, in->header)) == 0) { + fprintf(stderr, "[main_samview] fail to open file for writing.\n"); + goto view_end; + } if (is_header_only) goto view_end; // no need to print alignments if (argc == optind + 1) { // convert/print the entire file -- 2.39.5