Remove NULL checks after zmalloc and zrealloc.
Signed-off-by: Jonathan Dieter <jdieter@gmail.com>
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);
/* 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);
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);
* POSSIBILITY OF SUCH DAMAGE.
*/
-#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
ALLOCD_INT(zck, comp);
*dst = zmalloc(src_size);
- assert(*dst);
memcpy(*dst, src, src_size);
*dst_size = src_size;
* POSSIBILITY OF SUCH DAMAGE.
*/
-#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
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;
}
*dst = zmalloc(max_size);
- assert(*dst);
/* Currently, compression isn't deterministic when using contexts in
* zstd 1.3.5, so this works around it */
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) {
/* 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;
}
void PUBLIC zck_dl_reset(zckDL *dl) {
if(!dl)
return;
+
reset_mp(dl->mp);
dl->dl_chunk_data = 0;
clear_dl_regex(dl);
* POSSIBILITY OF SUCH DAMAGE.
*/
-#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
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);
/* 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
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;
}
/* 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);
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) {
}
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;
}
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;
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;
}
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;
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 &&
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;
}
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;
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;
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 */
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;
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);
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;
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;
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);
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) {
/* 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;
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;
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;
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;
}
}
/* 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);
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;
}
} 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;
* POSSIBILITY OF SUCH DAMAGE.
*/
-#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
}
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;
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;
* POSSIBILITY OF SUCH DAMAGE.
*/
+#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
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; i<cl; i++) {
return raw_checksum;
}
+void *zmalloc(size_t size) {
+ void *ret = calloc(1, size);
+ assert(ret);
+ return ret;
+}
+
+void *zrealloc(void *ptr, size_t size) {
+ void *ret = realloc(ptr, size);
+ assert(ret);
+ return ret;
+}
+
int get_tmp_fd(zckCtx *zck) {
VALIDATE_BOOL(zck);
tmpdir = "/tmp/";
}
fname = zmalloc(strlen(template) + strlen(tmpdir) + 2);
- if(fname == NULL) {
- set_error(zck, "Unable to allocate %lu bytes",
- strlen(template) + strlen(tmpdir) + 2);
- return -1;
- }
strncpy(fname, tmpdir, strlen(tmpdir));
strncpy(fname+strlen(tmpdir), "/", 2);
strncpy(fname+strlen(tmpdir)+1, template, strlen(template));
zck_log(ZCK_LOG_DEBUG, "Reading compression dict");
char *data = zmalloc(size);
- if(data == NULL) {
- set_error(zck, "Unable to allocate %lu bytes", size);
- return false;
- }
if(comp_read(zck, data, size, 0) != size) {
set_error(zck, "Error reading compressed dict");
return false;
size_t length) {
VALIDATE_BOOL(zck);
char *data = zmalloc(length);
- if(data == NULL) {
- set_error(zck, "Unable to allocate %lu bytes", length);
- return false;
- }
memcpy(data, value, length);
/* Validation options */
zckCtx PUBLIC *zck_create() {
zckCtx *zck = zmalloc(sizeof(zckCtx));
- if(zck == NULL) {
- set_error(NULL, "Unable to allocate %lu bytes", sizeof(zckCtx));
- return NULL;
- }
zck_clear_error(NULL);
zck->prep_hash_type = -1;
zck->prep_hdr_size = -1;
#ifndef ZCK_PRIVATE_H
#define ZCK_PRIVATE_H
+
#include <stdarg.h>
#include <stdint.h>
#include <stdbool.h>
#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__)
"zckCtx not opened for writing"); \
return NULL; \
}
+
typedef struct zckComp zckComp;
typedef zckCtx zckCtx;
__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 */