From: Jonathan Dieter Date: Fri, 14 Sep 2018 13:51:17 +0000 (+0100) Subject: Create zrealloc and change zmalloc so they assert that memory is allocated. X-Git-Tag: archive/raspbian/1.1.9+ds1-1+rpi1~1^2~118 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=491e8ab90e6a54ab1728526457a41db1ca864797;p=zchunk.git Create zrealloc and change zmalloc so they assert that memory is allocated. Remove NULL checks after zmalloc and zrealloc. Signed-off-by: Jonathan Dieter --- diff --git a/src/lib/comp/comp.c b/src/lib/comp/comp.c index 5f54046..c3bd09a 100644 --- a/src/lib/comp/comp.c +++ b/src/lib/comp/comp.c @@ -115,12 +115,7 @@ static bool comp_add_to_data(zckCtx *zck, zckComp *comp, const char *src, ALLOCD_BOOL(zck, comp); ALLOCD_BOOL(zck, src); - comp->data = realloc(comp->data, comp->data_size + src_size); - if(comp->data == NULL) { - set_fatal_error(zck, "Unable to reallocate %lu bytes", - comp->data_size + src_size); - return false; - } + comp->data = zrealloc(comp->data, comp->data_size + src_size); zck_log(ZCK_LOG_DEBUG, "Adding %lu bytes to compressed buffer", src_size); memcpy(comp->data + comp->data_size, src, src_size); @@ -390,11 +385,6 @@ bool comp_add_to_dc(zckCtx *zck, zckComp *comp, const char *src, /* Get rid of any already read data and allocate space for new data */ char *temp = zmalloc(comp->dc_data_size - comp->dc_data_loc + src_size); - if(temp == NULL) { - set_fatal_error(zck, "Unable to allocate %lu bytes", - comp->dc_data_size - comp->dc_data_loc + src_size); - return false; - } if(comp->dc_data_loc != 0) zck_log(ZCK_LOG_DEBUG, "Freeing %lu bytes from decompressed buffer", comp->dc_data_loc); @@ -431,10 +421,6 @@ ssize_t comp_read(zckCtx *zck, char *dst, size_t dst_size, bool use_dict) { size_t dc = 0; char *src = zmalloc(dst_size - dc); - if(src == NULL) { - set_fatal_error(zck, "Unable to allocate %lu bytes", dst_size-dc); - return false; - } bool finished_rd = false; bool finished_dc = false; zck_log(ZCK_LOG_DEBUG, "Trying to read %lu bytes", dst_size); diff --git a/src/lib/comp/nocomp/nocomp.c b/src/lib/comp/nocomp/nocomp.c index 90b480d..60ed9e7 100644 --- a/src/lib/comp/nocomp/nocomp.c +++ b/src/lib/comp/nocomp/nocomp.c @@ -24,7 +24,6 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include #include #include #include @@ -50,7 +49,6 @@ static ssize_t compress(zckCtx *zck, zckComp *comp, const char *src, ALLOCD_INT(zck, comp); *dst = zmalloc(src_size); - assert(*dst); memcpy(*dst, src, src_size); *dst_size = src_size; diff --git a/src/lib/comp/zstd/zstd.c b/src/lib/comp/zstd/zstd.c index 939740e..1e98b4f 100644 --- a/src/lib/comp/zstd/zstd.c +++ b/src/lib/comp/zstd/zstd.c @@ -24,7 +24,6 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include #include #include #include @@ -93,8 +92,7 @@ static ssize_t compress(zckCtx *zck, zckComp *comp, const char *src, ALLOCD_INT(zck, dst_size); ALLOCD_INT(zck, comp); - comp->dc_data = realloc(comp->dc_data, comp->dc_data_size + src_size); - assert(comp->dc_data); + comp->dc_data = zrealloc(comp->dc_data, comp->dc_data_size + src_size); memcpy(comp->dc_data + comp->dc_data_size, src, src_size); *dst = NULL; @@ -117,7 +115,6 @@ static bool end_cchunk(zckCtx *zck, zckComp *comp, char **dst, size_t *dst_size, } *dst = zmalloc(max_size); - assert(*dst); /* Currently, compression isn't deterministic when using contexts in * zstd 1.3.5, so this works around it */ @@ -163,9 +160,7 @@ static bool end_dchunk(zckCtx *zck, zckComp *comp, const bool use_dict, comp->data_size = 0; char *dst = zmalloc(fd_size); - assert(dst); - - size_t retval; + size_t retval = 0; zck_log(ZCK_LOG_DEBUG, "Decompressing %lu bytes to %lu bytes", src_size, fd_size); if(use_dict && comp->ddict_ctx) { diff --git a/src/lib/dl/dl.c b/src/lib/dl/dl.c index 44d3c5a..bec3c98 100644 --- a/src/lib/dl/dl.c +++ b/src/lib/dl/dl.c @@ -283,18 +283,10 @@ ssize_t PUBLIC zck_dl_get_bytes_uploaded(zckDL *dl) { /* Initialize zckDL. When finished, zckDL *must* be freed by zck_dl_free() */ zckDL PUBLIC *zck_dl_init(zckCtx *zck) { + VALIDATE_PTR(zck); + zckDL *dl = zmalloc(sizeof(zckDL)); - if(!dl) { - set_fatal_error(zck, "Unable to allocate %lu bytes for zckDL", - sizeof(zckDL)); - return NULL; - } dl->mp = zmalloc(sizeof(zckMP)); - if(!dl->mp) { - set_fatal_error(zck, "Unable to allocate %lu bytes for dl->mp", - sizeof(zckMP)); - return NULL; - } dl->zck = zck; return dl; } @@ -303,6 +295,7 @@ zckDL PUBLIC *zck_dl_init(zckCtx *zck) { void PUBLIC zck_dl_reset(zckDL *dl) { if(!dl) return; + reset_mp(dl->mp); dl->dl_chunk_data = 0; clear_dl_regex(dl); diff --git a/src/lib/dl/multipart.c b/src/lib/dl/multipart.c index 78ebc70..d6cdd04 100644 --- a/src/lib/dl/multipart.c +++ b/src/lib/dl/multipart.c @@ -24,7 +24,6 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include #include #include #include @@ -42,7 +41,6 @@ static char *add_boundary_to_regex(zckCtx *zck, const char *regex, if(regex == NULL || boundary == NULL) return NULL; char *regex_b = zmalloc(strlen(regex) + strlen(boundary) + 1); - assert(regex_b); if(snprintf(regex_b, strlen(regex) + strlen(boundary), regex, boundary) != strlen(regex) + strlen(boundary) - 2) { free(regex_b); @@ -117,12 +115,7 @@ size_t multipart_extract(zckDL *dl, char *b, size_t l) { /* Add new data to stored buffer */ if(mp->buffer) { - buf = realloc(mp->buffer, mp->buffer_len + l); - if(buf == NULL) { - set_fatal_error(dl->zck, "Unable to reallocate %lu bytes for zckDL", - mp->buffer_len + l); - return 0; - } + buf = zrealloc(mp->buffer, mp->buffer_len + l); memcpy(buf + mp->buffer_len, b, l); l = mp->buffer_len + l; mp->buffer = NULL; // No need to free, buf holds realloc'd buffer @@ -163,7 +156,7 @@ size_t multipart_extract(zckDL *dl, char *b, size_t l) { if(i >= end) { size_t size = buf + l - header_start; if(size > 0) { - mp->buffer = malloc(size); + mp->buffer = zmalloc(size); memcpy(mp->buffer, header_start, size); mp->buffer_len = size; } @@ -232,11 +225,6 @@ size_t multipart_get_boundary(zckDL *dl, char *b, size_t size) { /* Copy buffer to null-terminated string because POSIX regex requires null- * terminated string */ char *buf = zmalloc(size+1); - if(buf == NULL) { - set_fatal_error(dl->zck, "Unable to allocate %lu bytes for header", - size+1); - return 0; - } buf[size] = '\0'; memcpy(buf, b, size); diff --git a/src/lib/dl/range.c b/src/lib/dl/range.c index eb25b16..5875620 100644 --- a/src/lib/dl/range.c +++ b/src/lib/dl/range.c @@ -42,11 +42,6 @@ static zckRangeItem *range_insert_new(zckCtx *zck, zckRangeItem *prev, VALIDATE_PTR(zck); zckRangeItem *new = zmalloc(sizeof(zckRangeItem)); - if(!new) { - set_fatal_error(zck, "Unable to allocate %lu bytes", - sizeof(zckRangeItem)); - return NULL; - } new->start = start; new->end = end; if(prev) { @@ -154,13 +149,8 @@ void PUBLIC zck_range_free(zckRange **info) { } char PUBLIC *zck_get_range_char(zckCtx *zck, zckRange *range) { - int buf_size=BUF_SIZE; - char *output=malloc(buf_size); - if(!output) { - set_fatal_error(zck, "Unable to allocate %lu bytes", buf_size); - return NULL; - } - + int buf_size = BUF_SIZE; + char *output = zmalloc(buf_size); int loc = 0; int count = 0; zckRangeItem *ri = range->first; @@ -175,11 +165,7 @@ char PUBLIC *zck_get_range_char(zckCtx *zck, zckRange *range) { } if(length > buf_size-loc) { buf_size = (int)(buf_size * 1.5); - output = realloc(output, buf_size); - if(output == NULL) { - set_fatal_error(zck, "Unable to allocate %lu bytes", buf_size); - return NULL; - } + output = zrealloc(output, buf_size); continue; } loc += length; @@ -187,12 +173,7 @@ char PUBLIC *zck_get_range_char(zckCtx *zck, zckRange *range) { ri = ri->next; } output[loc-1]='\0'; // Remove final comma - output = realloc(output, loc); - if(output == NULL) { - set_fatal_error(zck, "Unable to shrink range to %lu bytes", loc); - free(output); - return NULL; - } + output = zrealloc(output, loc); return output; } @@ -200,10 +181,6 @@ zckRange PUBLIC *zck_get_missing_range(zckCtx *zck, int max_ranges) { VALIDATE_PTR(zck); zckRange *range = zmalloc(sizeof(zckRange)); - if(range == NULL) { - set_fatal_error(zck, "Unable to allocate %lu bytes", sizeof(zckRange)); - return NULL; - } for(zckChunk *chk = zck->index.first; chk; chk = chk->next) { if(chk->valid) continue; diff --git a/src/lib/hash/hash.c b/src/lib/hash/hash.c index 9ce8c86..69d5434 100644 --- a/src/lib/hash/hash.c +++ b/src/lib/hash/hash.c @@ -214,16 +214,12 @@ bool hash_init(zckCtx *zck, zckHash *hash, zckHashType *hash_type) { zck_log(ZCK_LOG_DDEBUG, "Initializing SHA-1 hash"); hash->ctx = zmalloc(sizeof(SHA_CTX)); hash->type = hash_type; - if(hash->ctx == NULL) - return false; SHA1_Init((SHA_CTX *) hash->ctx); return true; } else if(hash_type->type == ZCK_HASH_SHA256) { zck_log(ZCK_LOG_DDEBUG, "Initializing SHA-256 hash"); hash->ctx = zmalloc(sizeof(SHA256_CTX)); hash->type = hash_type; - if(hash->ctx == NULL) - return false; SHA256_Init((SHA256_CTX *) hash->ctx); return true; } else if(hash_type->type >= ZCK_HASH_SHA512 && @@ -231,8 +227,6 @@ bool hash_init(zckCtx *zck, zckHash *hash, zckHashType *hash_type) { zck_log(ZCK_LOG_DDEBUG, "Initializing SHA-512 hash"); hash->ctx = zmalloc(sizeof(SHA512_CTX)); hash->type = hash_type; - if(hash->ctx == NULL) - return false; SHA512_Init((SHA512_CTX *) hash->ctx); return true; } diff --git a/src/lib/header.c b/src/lib/header.c index 20be61a..4cf8be8 100644 --- a/src/lib/header.c +++ b/src/lib/header.c @@ -50,12 +50,7 @@ static bool check_flags(zckCtx *zck, size_t flags) { static bool read_header_from_file(zckCtx *zck) { /* Allocate header and store any extra bytes at beginning of header */ - zck->header = realloc(zck->header, zck->lead_size + zck->header_length); - if(zck->header == NULL) { - set_fatal_error(zck, "Unable to reallocate %lu bytes", - zck->lead_size + zck->header_length); - return false; - } + zck->header = zrealloc(zck->header, zck->lead_size + zck->header_length); zck->lead_string = zck->header; char *header = zck->header + zck->lead_size; size_t loaded = 0; @@ -111,11 +106,6 @@ static bool read_preface(zckCtx *zck) { return false; } zck->full_hash_digest = zmalloc(zck->hash_type.digest_size); - if(!zck->full_hash_digest) { - set_fatal_error(zck, "Unable to allocate %lu bytes", - zck->hash_type.digest_size); - return false; - } memcpy(zck->full_hash_digest, header+length, zck->hash_type.digest_size); length += zck->hash_type.digest_size; @@ -214,10 +204,6 @@ static bool preface_create(zckCtx *zck) { int header_malloc = zck->hash_type.digest_size + 4 + 2*MAX_COMP_SIZE; char *header = zmalloc(header_malloc); - if(header == NULL) { - set_error(zck, "Unable to allocate %lu bytes", header_malloc); - return false; - } size_t length = 0; /* Write out the full data digest */ @@ -238,11 +224,7 @@ static bool preface_create(zckCtx *zck) { compint_from_size(header+length, zck->index_size, &length); /* Shrink header to actual size */ - header = realloc(header, length); - if(header == NULL) { - set_fatal_error(zck, "Unable to reallocate %lu bytes", length); - return false; - } + header = zrealloc(header, length); zck->preface_string = header; zck->preface_size = length; @@ -252,10 +234,6 @@ static bool preface_create(zckCtx *zck) { static bool sig_create(zckCtx *zck) { char *header = zmalloc(MAX_COMP_SIZE); - if(header == NULL) { - set_error(zck, "Unable to allocate %lu bytes", MAX_COMP_SIZE); - return false; - } size_t length = 0; zck_log(ZCK_LOG_DEBUG, "Calculating %i signatures", zck->sigs.count); @@ -277,10 +255,6 @@ static bool sig_create(zckCtx *zck) { static bool lead_create(zckCtx *zck) { int phs = 5 + 2*MAX_COMP_SIZE + zck->hash_type.digest_size; char *header = zmalloc(phs); - if(header == NULL) { - set_error(zck, "Unable to allocate %lu bytes", phs); - return false; - } size_t length = 0; memcpy(header, "\0ZCK1", 5); length += 5; @@ -294,11 +268,7 @@ static bool lead_create(zckCtx *zck) { zck->hdr_digest_loc = length; length += zck->hash_type.digest_size; - header = realloc(header, length); - if(header == NULL) { - set_fatal_error(zck, "Unable to reallocate %lu bytes", length); - return false; - } + header = zrealloc(header, length); zck->lead_string = header; zck->lead_size = length; @@ -339,11 +309,6 @@ bool header_create(zckCtx *zck) { zck_log(ZCK_LOG_DEBUG, "Merging into header: %lu bytes", zck->data_offset); zck->header = zmalloc(zck->data_offset); - if(zck->header == NULL) { - set_fatal_error(zck, "Unable to allocate %lu bytes", - zck->data_offset); - return false; - } size_t offs = 0; memcpy(zck->header + offs, zck->lead_string, zck->lead_size); free(zck->lead_string); @@ -403,10 +368,6 @@ static bool read_lead(zckCtx *zck) { int lead = 5 + 2*MAX_COMP_SIZE; char *header = zmalloc(lead); - if(header == NULL) { - set_error(zck, "Unable to allocate %lu bytes", lead); - return false; - } size_t length = 0; if(read_data(zck, header, lead) < lead) { @@ -455,15 +416,7 @@ static bool read_lead(zckCtx *zck) { /* Read header digest */ zck_log(ZCK_LOG_DEBUG, "Reading header digest"); - header = realloc(header, length + zck->hash_type.digest_size); - if(header == NULL) { - zck->header_length = 0; - zck->hdr_digest_loc = 0; - hash_reset(&(zck->hash_type)); - set_fatal_error(zck, "Unable to re-allocate %lu bytes", - length + zck->hash_type.digest_size); - return false; - } + header = zrealloc(header, length + zck->hash_type.digest_size); size_t to_read = 0; if(lead < length + zck->hash_type.digest_size) to_read = length + zck->hash_type.digest_size - lead; @@ -492,15 +445,6 @@ static bool read_lead(zckCtx *zck) { return false; } zck->header_digest = zmalloc(zck->hash_type.digest_size); - if(zck->header_digest == NULL) { - free(header); - zck->header_length = 0; - zck->hdr_digest_loc = 0; - hash_reset(&(zck->hash_type)); - set_error(zck, "Unable to allocate %lu bytes", - zck->hash_type.digest_size); - return false; - } memcpy(zck->header_digest, header + length, zck->hash_type.digest_size); length += zck->hash_type.digest_size; diff --git a/src/lib/index/index_create.c b/src/lib/index/index_create.c index ec09192..dd6b564 100644 --- a/src/lib/index/index_create.c +++ b/src/lib/index/index_create.c @@ -37,11 +37,6 @@ static bool create_chunk(zckCtx *zck) { clear_work_index(zck); zck->work_index_item = zmalloc(sizeof(zckChunk)); - if(zck->work_index_item == NULL) { - set_error(zck, "Unable to allocate %lu bytes", - sizeof(zckChunk)); - return false; - } if(!hash_init(zck, &(zck->work_index_hash), &(zck->chunk_hash_type))) return false; return true; @@ -54,10 +49,6 @@ static bool finish_chunk(zckIndex *index, zckChunk *item, char *digest, ALLOCD_BOOL(zck, item); item->digest = zmalloc(index->digest_size); - if(item->digest == NULL) { - set_error(zck, "Unable to allocate %lu bytes", index->digest_size); - return false; - } if(digest) { memcpy(item->digest, digest, index->digest_size); item->digest_size = index->digest_size; @@ -123,11 +114,7 @@ bool index_create(zckCtx *zck) { } } /* Shrink index to actual size */ - index = realloc(index, index_size); - if(index == NULL) { - set_fatal_error(zck, "Unable to reallocate %lu bytes", index_size); - return false; - } + index = zrealloc(index, index_size); zck->index_string = index; zck->index_size = index_size; zck_log(ZCK_LOG_DEBUG, "Generated index: %lu bytes", zck->index_size); @@ -147,10 +134,6 @@ bool index_new_chunk(zckCtx *zck, zckIndex *index, char *digest, int digest_size return false; } zckChunk *chk = zmalloc(sizeof(zckChunk)); - if(chk == NULL) { - set_error(zck, "Unable to allocate %lu bytes", sizeof(zckChunk)); - return false; - } index->digest_size = digest_size; chk->comp_length = comp_size; chk->length = orig_size; @@ -195,11 +178,6 @@ bool index_finish_chunk(zckCtx *zck) { } } else { digest = zmalloc(zck->chunk_hash_type.digest_size); - if(digest == NULL) { - set_fatal_error(zck, "Unable to allocate %lu bytes", - zck->chunk_hash_type.digest_size); - return false; - } } if(!finish_chunk(&(zck->index), zck->work_index_item, digest, true, zck)) return false; diff --git a/src/lib/index/index_read.c b/src/lib/index/index_read.c index f308040..a8b7272 100644 --- a/src/lib/index/index_read.c +++ b/src/lib/index/index_read.c @@ -24,7 +24,6 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include #include #include #include @@ -68,12 +67,9 @@ bool index_read(zckCtx *zck, char *data, size_t size, size_t max_length) { } zckChunk *new = zmalloc(sizeof(zckChunk)); - assert(new); /* Read index entry digest */ new->digest = zmalloc(zck->index.digest_size); - assert(new->digest); - memcpy(new->digest, data+length, zck->index.digest_size); new->digest_size = zck->index.digest_size; length += zck->index.digest_size; diff --git a/src/lib/io.c b/src/lib/io.c index 1b3e3b6..1e22e87 100644 --- a/src/lib/io.c +++ b/src/lib/io.c @@ -101,8 +101,6 @@ ssize_t tell_data(zckCtx *zck) { int chunks_from_temp(zckCtx *zck) { int read_count; char *data = zmalloc(BUF_SIZE); - if(data == NULL) - return false; if(lseek(zck->temp_fd, 0, SEEK_SET) == -1) return false; diff --git a/src/lib/zck.c b/src/lib/zck.c index eb58f8a..fd04067 100644 --- a/src/lib/zck.c +++ b/src/lib/zck.c @@ -24,6 +24,7 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include #include @@ -93,10 +94,6 @@ static int hex_to_int (char c) { static char *ascii_checksum_to_bin (zckCtx *zck, char *checksum) { int cl = strlen(checksum); char *raw_checksum = zmalloc(cl/2); - if(raw_checksum == NULL) { - set_error(zck, "Unable to allocate %lu bytes", cl/2); - return NULL; - } char *rp = raw_checksum; int buf = 0; for (int i=0; iprep_hash_type = -1; zck->prep_hdr_size = -1; diff --git a/src/lib/zck_private.h b/src/lib/zck_private.h index 0a3afad..32b4413 100644 --- a/src/lib/zck_private.h +++ b/src/lib/zck_private.h @@ -1,5 +1,6 @@ #ifndef ZCK_PRIVATE_H #define ZCK_PRIVATE_H + #include #include #include @@ -19,8 +20,6 @@ #define CHUNK_DEFAULT_MIN 1 #define CHUNK_DEFAULT_MAX 10485760 // 10MB -#define zmalloc(x) calloc(1, x) - #define PUBLIC __attribute__((visibility("default"))) #define zck_log(...) zck_log_wf(__func__, __VA_ARGS__) @@ -87,6 +86,7 @@ "zckCtx not opened for writing"); \ return NULL; \ } + typedef struct zckComp zckComp; typedef zckCtx zckCtx; @@ -293,6 +293,10 @@ int get_tmp_fd() __attribute__ ((warn_unused_result)); bool import_dict(zckCtx *zck) __attribute__ ((warn_unused_result)); +void *zmalloc(size_t size) + __attribute__ ((warn_unused_result)); +void *zrealloc(void *ptr, size_t size) + __attribute__ ((warn_unused_result)); /* hash/hash.h */