From: Jonathan Dieter Date: Wed, 7 Mar 2018 11:31:14 +0000 (+0200) Subject: Add null parameter checking for public functions X-Git-Tag: archive/raspbian/1.1.9+ds1-1+rpi1~1^2~366 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=cb2e41ef8006b5164ce01f9807e7d29a414a4ee2;p=zchunk.git Add null parameter checking for public functions Signed-off-by: Jonathan Dieter --- diff --git a/include/zck.h b/include/zck.h index 8845308..9ea488d 100644 --- a/include/zck.h +++ b/include/zck.h @@ -70,7 +70,7 @@ int zck_decompress(zckCtx *zck, const char *src, const size_t src_size, char **dst, size_t *dst_size); int zck_write_file(zckCtx *zck); int zck_read_header(zckCtx *zck, int src_fd); -uint64_t zck_get_index_count(zckCtx *zck); +int64_t zck_get_index_count(zckCtx *zck); zckIndexInfo *zck_get_index(zckCtx *zck); int zck_decompress_to_file (zckCtx *zck, int src_fd, int dst_fd); int zck_set_full_hash_type(zckCtx *zck, uint8_t hash_type); @@ -79,8 +79,8 @@ char *zck_get_index_digest(zckCtx *zck); char *zck_get_full_digest(zckCtx *zck); int zck_get_full_digest_size(zckCtx *zck); int zck_get_chunk_digest_size(zckCtx *zck); -uint8_t zck_get_full_hash_type(zckCtx *zck); -uint8_t zck_get_chunk_hash_type(zckCtx *zck); +int zck_get_full_hash_type(zckCtx *zck); +int zck_get_chunk_hash_type(zckCtx *zck); const char *zck_hash_name_from_type(uint8_t hash_type); const char *zck_comp_name_from_type(uint8_t comp_type); int zck_range_calc_segments(zckRangeInfo *info, unsigned int max_ranges); diff --git a/src/lib/zck.c b/src/lib/zck.c index f04b9e5..0f76f30 100644 --- a/src/lib/zck.c +++ b/src/lib/zck.c @@ -35,6 +35,10 @@ #include "zck_private.h" int zck_write_file(zckCtx *zck) { + if(zck == NULL) { + zck_log(ZCK_LOG_ERROR, "zckCtx not initialized\n"); + return False; + } zck_index_finalize(zck); zck_log(ZCK_LOG_DEBUG, "Writing header\n"); if(!zck_write_header(zck)) @@ -57,6 +61,8 @@ int zck_write_file(zckCtx *zck) { } void zck_free(zckCtx *zck) { + if(zck == NULL) + return; zck_index_free(zck); zck_comp_close(zck); zck_hash_close(&(zck->full_hash)); @@ -79,10 +85,19 @@ void zck_free(zckCtx *zck) { zckCtx *zck_create() { zckCtx *zck = zmalloc(sizeof(zckCtx)); + if(zck->index.hash_type == NULL) { + zck_log(ZCK_LOG_ERROR, "Unable to allocate %lu bytes\n", + sizeof(zckCtx)); + return False; + } return zck; } int zck_set_full_hash_type(zckCtx *zck, uint8_t hash_type) { + if(zck == NULL) { + zck_log(ZCK_LOG_ERROR, "zckCtx not initialized\n"); + return False; + } zck_log(ZCK_LOG_INFO, "Setting full hash to %s\n", zck_hash_name_from_type(hash_type)); if(!zck_hash_setup(&(zck->hash_type), hash_type)) { @@ -99,8 +114,20 @@ int zck_set_full_hash_type(zckCtx *zck, uint8_t hash_type) { } int zck_set_chunk_hash_type(zckCtx *zck, uint8_t hash_type) { + if(zck == NULL) { + zck_log(ZCK_LOG_ERROR, "zckCtx not initialized\n"); + return False; + } zck_log(ZCK_LOG_INFO, "Setting chunk hash to %s\n", zck_hash_name_from_type(hash_type)); + if(zck->index.hash_type == NULL) { + zck->index.hash_type = zmalloc(sizeof(zckHashType)); + if(zck->index.hash_type == NULL) { + zck_log(ZCK_LOG_ERROR, "Unable to allocate %lu bytes\n", + sizeof(zckHashType)); + return False; + } + } if(!zck_hash_setup(zck->index.hash_type, hash_type)) { zck_log(ZCK_LOG_ERROR, "Unable to set chunk hash to %s\n", zck_hash_name_from_type(hash_type)); @@ -110,34 +137,50 @@ int zck_set_chunk_hash_type(zckCtx *zck, uint8_t hash_type) { } int zck_get_full_digest_size(zckCtx *zck) { + if(zck == NULL) + return -1; return zck->hash_type.digest_size; } int zck_get_chunk_digest_size(zckCtx *zck) { + if(zck == NULL) + return -1; return zck->index.hash_type->digest_size; } -uint8_t zck_get_full_hash_type(zckCtx *zck) { +int zck_get_full_hash_type(zckCtx *zck) { + if(zck == NULL) + return -1; return zck->hash_type.type; } -uint8_t zck_get_chunk_hash_type(zckCtx *zck) { +int zck_get_chunk_hash_type(zckCtx *zck) { + if(zck == NULL) + return -1; return zck->index.hash_type->type; } -uint64_t zck_get_index_count(zckCtx *zck) { +int64_t zck_get_index_count(zckCtx *zck) { + if(zck == NULL) + return -1; return zck->index.count; } zckIndexInfo *zck_get_index(zckCtx *zck) { + if(zck == NULL) + return NULL; return &(zck->index); } char *zck_get_index_digest(zckCtx *zck) { + if(zck == NULL) + return NULL; return zck->index_digest; } char *zck_get_full_digest(zckCtx *zck) { + if(zck == NULL) + return NULL; return zck->full_hash_digest; }