Add null parameter checking for public functions
authorJonathan Dieter <jdieter@gmail.com>
Wed, 7 Mar 2018 11:31:14 +0000 (13:31 +0200)
committerJonathan Dieter <jdieter@gmail.com>
Wed, 7 Mar 2018 11:31:14 +0000 (13:31 +0200)
Signed-off-by: Jonathan Dieter <jdieter@gmail.com>
include/zck.h
src/lib/zck.c

index 8845308241bd303457e65c430ecafc5109e4ccd4..9ea488d360971c67e3561df4ec2e644405b8d30c 100644 (file)
@@ -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);
index f04b9e55afacd519b3de54adb958e24af09e86dc..0f76f308e5d56b400a1f78ba83d07e185a5833cb 100644 (file)
 #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;
 }