From: Henry Amrhein Date: Thu, 3 Jan 2013 06:53:51 +0000 (-0800) Subject: razf.c X-Git-Url: http://woldlab.caltech.edu/gitweb/?p=samtools.git;a=commitdiff_plain;h=0db649c80dc40ee36264024a7d35a0fd808263cb razf.c wrapped calls to read() and write() with error-checking started wrapping malloc() and calloc() calls with error-checking --- diff --git a/razf.c b/razf.c index e7499f9..94fa854 100644 --- a/razf.c +++ b/razf.c @@ -90,21 +90,39 @@ static void add_zindex(RAZF *rz, int64_t in, int64_t out){ } static void save_zindex(RAZF *rz, int fd){ + size_t count; int32_t i, v32; int is_be; is_be = is_big_endian(); - if(is_be) write(fd, &rz->index->size, sizeof(int)); + if(is_be) { + if (write(fd, &rz->index->size, sizeof(int)) < 0) { + fprintf(stderr, "[%s] failure to write zindex size.\n", __func__); + abort(); + } + } else { v32 = byte_swap_4((uint32_t)rz->index->size); - write(fd, &v32, sizeof(uint32_t)); + if (write(fd, &v32, sizeof(uint32_t)) < 0) { + fprintf(stderr, "[%s] failure to write zindex size.\n", __func__); + abort(); + } } v32 = rz->index->size / RZ_BIN_SIZE + 1; if(!is_be){ for(i=0;iindex->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]); for(i=0;iindex->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]); } - write(fd, rz->index->bin_offsets, sizeof(int64_t) * v32); - write(fd, rz->index->cell_offsets, sizeof(int32_t) * rz->index->size); + count = sizeof(int64_t) * v32; + if (write(fd, rz->index->bin_offsets, count) < 0) { + fprintf(stderr, "[%s] failure to write zindex bin_offsets.\n", __func__); + abort(); + } + + count = sizeof(int32_t) * rz->index->size; + if (write(fd, rz->index->cell_offsets, count) < 0) { + fprintf(stderr, "[%s] failure to write zindex cell_offsets.\n", __func__); + abort(); + } } #endif @@ -115,29 +133,51 @@ static void load_zindex(RAZF *rz, int fd){ #endif int32_t i, v32; int is_be; + size_t count; if(!rz->load_index) return; if(rz->index == NULL) rz->index = malloc(sizeof(ZBlockIndex)); + if(rz->index == NULL) { + fprintf(stderr, "[%s] failure to allocate index.\n", __func__); + abort(); + } is_be = is_big_endian(); #ifdef _USE_KNETFILE - knet_read(fp, &rz->index->size, sizeof(int)); + if (knet_read(fp, &rz->index->size, sizeof(int)) < 0) { #else - read(fd, &rz->index->size, sizeof(int)); + if (read(fd, &rz->index->size, sizeof(int)) < 0) { #endif + fprintf(stderr, "[%s] failure to read zindex size.\n", __func__); + abort(); + } if(!is_be) rz->index->size = byte_swap_4((uint32_t)rz->index->size); rz->index->cap = rz->index->size; v32 = rz->index->size / RZ_BIN_SIZE + 1; - rz->index->bin_offsets = malloc(sizeof(int64_t) * v32); + count = sizeof(int64_t) * v32; + if ((rz->index->bin_offsets = malloc(count)) == NULL) { + fprintf(stderr, "[%s] failure to allocate bin_offsets array.\n", __func__); + abort(); + } #ifdef _USE_KNETFILE - knet_read(fp, rz->index->bin_offsets, sizeof(int64_t) * v32); + if (knet_read(fp, rz->index->bin_offsets, count) < 0) { #else - read(fd, rz->index->bin_offsets, sizeof(int64_t) * v32); + if (read(fd, rz->index->bin_offsets, count) < 0) { #endif - rz->index->cell_offsets = malloc(sizeof(int) * rz->index->size); + fprintf(stderr, "[%s] failure to read bin_offsets.\n", __func__); + abort(); + } + count = sizeof(int) * rz->index->size; + if ((rz->index->cell_offsets = malloc(count)) == NULL) { + fprintf(stderr, "[%s] failure to allocate cell_offsets array.\n", __func__); + abort(); + } #ifdef _USE_KNETFILE - knet_read(fp, rz->index->cell_offsets, sizeof(int) * rz->index->size); + if (knet_read(fp, rz->index->cell_offsets, count) < count) { #else - read(fd, rz->index->cell_offsets, sizeof(int) * rz->index->size); + if (read(fd, rz->index->cell_offsets, count) < count) { #endif + fprintf(stderr, "[%s] failure to read cell_offsets.\n", __func__); + abort(); + } if(!is_be){ for(i=0;iindex->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]); for(i=0;iindex->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]); @@ -190,6 +230,7 @@ static RAZF* razf_open_w(int fd){ static void _razf_write(RAZF* rz, const void *data, int size){ int tout; + size_t count; rz->stream->avail_in = size; rz->stream->next_in = (void*)data; while(1){ @@ -197,11 +238,15 @@ static void _razf_write(RAZF* rz, const void *data, int size){ deflate(rz->stream, Z_NO_FLUSH); rz->out += tout - rz->stream->avail_out; if(rz->stream->avail_out) break; + count = RZ_BUFFER_SIZE - rz->stream->avail_out; #ifdef _USE_KNETFILE - write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out); + if (write(rz->x.fpw, rz->outbuf, count) < 0) { #else - write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out); + if (write(rz->filedes, rz->outbuf, count) < 0) { #endif + fprintf(stderr, "[%s] failed to write output buffer.\n", __func__); + abort(); + } rz->stream->avail_out = RZ_BUFFER_SIZE; rz->stream->next_out = rz->outbuf; if(rz->stream->avail_in == 0) break; @@ -211,17 +256,22 @@ static void _razf_write(RAZF* rz, const void *data, int size){ } static void razf_flush(RAZF *rz){ + size_t count; uint32_t tout; if(rz->buf_len){ _razf_write(rz, rz->inbuf, rz->buf_len); rz->buf_off = rz->buf_len = 0; } if(rz->stream->avail_out){ + count = RZ_BUFFER_SIZE - rz->stream->avail_out; #ifdef _USE_KNETFILE - write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out); + if (write(rz->x.fpw, rz->outbuf, count) < 0) { #else - write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out); + if (write(rz->filedes, rz->outbuf, count) < 0) { #endif + fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__); + abort(); + } rz->stream->avail_out = RZ_BUFFER_SIZE; rz->stream->next_out = rz->outbuf; } @@ -230,11 +280,15 @@ static void razf_flush(RAZF *rz){ deflate(rz->stream, Z_FULL_FLUSH); rz->out += tout - rz->stream->avail_out; if(rz->stream->avail_out == 0){ + count = RZ_BUFFER_SIZE - rz->stream->avail_out; #ifdef _USE_KNETFILE - write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out); + if (write(rz->x.fpw, rz->outbuf, count) < 0) { #else - write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out); + if (write(rz->filedes, rz->outbuf, count) < 0) { #endif + fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__); + abort(); + } rz->stream->avail_out = RZ_BUFFER_SIZE; rz->stream->next_out = rz->outbuf; } else break; @@ -244,6 +298,7 @@ static void razf_flush(RAZF *rz){ } static void razf_end_flush(RAZF *rz){ + size_t count; uint32_t tout; if(rz->buf_len){ _razf_write(rz, rz->inbuf, rz->buf_len); @@ -254,11 +309,15 @@ static void razf_end_flush(RAZF *rz){ deflate(rz->stream, Z_FINISH); rz->out += tout - rz->stream->avail_out; if(rz->stream->avail_out < RZ_BUFFER_SIZE){ + count = RZ_BUFFER_SIZE - rz->stream->avail_out; #ifdef _USE_KNETFILE - write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out); + if (write(rz->x.fpw, rz->outbuf, count) < 0) { #else - write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out); + if (write(rz->filedes, rz->outbuf, count) < 0) { #endif + fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__); + abort(); + } rz->stream->avail_out = RZ_BUFFER_SIZE; rz->stream->next_out = rz->outbuf; } else break; @@ -355,7 +414,10 @@ static RAZF* razf_open_r(int fd, int _load_index){ int n, is_be, ret; int64_t end; unsigned char c[] = "RAZF"; - rz = calloc(1, sizeof(RAZF)); + if ((rz = calloc(1, sizeof(RAZF))) == NULL) { + fprintf(stderr, "[%s] failure to allocate RAZF structure.\n", __func__); + return NULL; + } rz->mode = 'r'; #ifdef _USE_KNETFILE rz->x.fpr = fp; @@ -365,9 +427,24 @@ static RAZF* razf_open_r(int fd, int _load_index){ #endif rz->filedes = fd; #endif - rz->stream = calloc(sizeof(z_stream), 1); - rz->inbuf = malloc(RZ_BUFFER_SIZE); - rz->outbuf = malloc(RZ_BUFFER_SIZE); + if ((rz->stream = calloc(sizeof(z_stream), 1)) == NULL) { + fprintf(stderr, "[%s] failure to allocate z_stream.\n", __func__); + free(rz); + return NULL; + } + if ((rz->inbuf = malloc(RZ_BUFFER_SIZE)) == NULL) { + fprintf(stderr, "[%s] failure to allocate input buffer.\n", __func__); + free(rz->stream); + free(rz); + return NULL; + } + if ((rz->outbuf = malloc(RZ_BUFFER_SIZE)) == NULL) { + fprintf(stderr, "[%s] failure to allocate output buffer.\n", __func__); + free(rz->inbuf); + free(rz->stream); + free(rz); + return NULL; + } rz->end = rz->src_end = 0x7FFFFFFFFFFFFFFFLL; #ifdef _USE_KNETFILE n = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE); @@ -801,24 +878,48 @@ void razf_close(RAZF *rz){ #ifdef _USE_KNETFILE save_zindex(rz, rz->x.fpw); if(is_big_endian()){ - write(rz->x.fpw, &rz->in, sizeof(int64_t)); - write(rz->x.fpw, &rz->out, sizeof(int64_t)); + if (write(rz->x.fpw, &rz->in, sizeof(int64_t)) < 0) { + fprintf(stderr, "[%s] failed to write rz.in.\n", __func__); + abort(); + } + if (write(rz->x.fpw, &rz->out, sizeof(int64_t)) < 0) { + fprintf(stderr, "[%s] failed to write rz.out.\n", __func__); + abort(); + } } else { uint64_t v64 = byte_swap_8((uint64_t)rz->in); - write(rz->x.fpw, &v64, sizeof(int64_t)); + if (write(rz->x.fpw, &v64, sizeof(int64_t)) < 0) { + fprintf(stderr, "[%s] failed to write rz.in.\n", __func__); + abort(); + } v64 = byte_swap_8((uint64_t)rz->out); - write(rz->x.fpw, &v64, sizeof(int64_t)); + if (write(rz->x.fpw, &v64, sizeof(int64_t)) < 0) { + fprintf(stderr, "[%s] failed to write rz.out.\n", __func__); + abort(); + } } #else save_zindex(rz, rz->filedes); if(is_big_endian()){ - write(rz->filedes, &rz->in, sizeof(int64_t)); - write(rz->filedes, &rz->out, sizeof(int64_t)); + if (write(rz->filedes, &rz->in, sizeof(int64_t)) < 0) { + fprintf(stderr, "[%s] failed to write rz.in.\n", __func__); + abort(); + } + if (write(rz->filedes, &rz->out, sizeof(int64_t)) < 0) { + fprintf(stderr, "[%s] failed to write rz.out.\n", __func__); + abort(); + } } else { uint64_t v64 = byte_swap_8((uint64_t)rz->in); - write(rz->filedes, &v64, sizeof(int64_t)); + if (write(rz->filedes, &v64, sizeof(int64_t)) < 0) { + fprintf(stderr, "[%s] failed to write rz.in.\n", __func__); + abort(); + } v64 = byte_swap_8((uint64_t)rz->out); - write(rz->filedes, &v64, sizeof(int64_t)); + if (write(rz->filedes, &v64, sizeof(int64_t)) < 0) { + fprintf(stderr, "[%s] failed to write rz.out.\n", __func__); + abort(); + } } #endif #endif