From 79e6312397661e1ece181d9ff0729dde125ac4ed Mon Sep 17 00:00:00 2001 From: Thomas Schmitt Date: Thu, 15 Oct 2015 08:38:56 +0200 Subject: [PATCH] Refactored error handling while encoding AAIP information. Again Coverity CID 12564. --- libisofs/aaip_0_2.c | 28 ++++++++++++++++++---------- libisofs/aaip_0_2.h | 10 +++++----- libisofs/fs_local.c | 9 +++++---- libisofs/node.c | 34 +++++++++++++++++++--------------- 4 files changed, 47 insertions(+), 34 deletions(-) diff --git a/libisofs/aaip_0_2.c b/libisofs/aaip_0_2.c index fc15e3d..1fc84e5 100644 --- a/libisofs/aaip_0_2.c +++ b/libisofs/aaip_0_2.c @@ -7,7 +7,7 @@ See libisofs/aaip_0_2.h http://libburnia-project.org/wiki/AAIP - Copyright (c) 2009 - 2014 Thomas Schmitt, libburnia project, GPLv2+ + Copyright (c) 2009 - 2015 Thomas Schmitt, libburnia project, GPLv2+ */ @@ -88,26 +88,30 @@ static int aaip_encode_pair(char *name, size_t attr_length, char *attr, no longer needed @param flag Bitfield for control purposes bit0= set CONTINUE bit of last AAIP field to 1 - @return >0 is the number of SUSP fields generated, - 0 means error + @return >= 0 is the number of SUSP fields generated, + < 0 means error */ -size_t aaip_encode(size_t num_attrs, char **names, - size_t *value_lengths, char **values, - size_t *result_len, unsigned char **result, int flag) +ssize_t aaip_encode(size_t num_attrs, char **names, + size_t *value_lengths, char **values, + size_t *result_len, unsigned char **result, int flag) { - size_t mem_size= 0, comp_size, ret; + size_t mem_size= 0, comp_size; + ssize_t ret; unsigned int number_of_fields, i, num_recs; /* Predict memory needs, number of SUSP fields and component records */ + *result = NULL; *result_len= 0; for(i= 0; i < num_attrs; i++) { ret= aaip_encode_pair(names[i], value_lengths[i], values[i], &num_recs, &comp_size, NULL, (size_t) 0, 1); - if(ret <= 0) + if(ret < 0) return(ret); mem_size+= comp_size; } number_of_fields= mem_size / 250 + !!(mem_size % 250); + if(number_of_fields == 0) + return(0); mem_size+= number_of_fields * 5; #ifdef Aaip_encode_debuG @@ -118,14 +122,18 @@ size_t aaip_encode(size_t num_attrs, char **names, #endif if(*result == NULL) - return 0; + return ISO_OUT_OF_MEM; /* Encode pairs into result */ for(i= 0; i < num_attrs; i++) { ret= aaip_encode_pair(names[i], value_lengths[i], values[i], &num_recs, &comp_size, *result, *result_len, 0); - if(ret <= 0) + if(ret < 0) { + free(*result); + *result = NULL; + *result_len = 0; return(ret); + } (*result_len)+= comp_size; } diff --git a/libisofs/aaip_0_2.h b/libisofs/aaip_0_2.h index 2c458c7..c4f0a98 100644 --- a/libisofs/aaip_0_2.h +++ b/libisofs/aaip_0_2.h @@ -30,12 +30,12 @@ no longer needed @param flag Bitfield for control purposes bit0= set CONTINUE bit of last AAIP field to 1 - @return >0 is the number of SUSP fields generated, - 0 means error + @return >= 0 is the number of SUSP fields generated, + < 0 means error */ -size_t aaip_encode(size_t num_attrs, char **names, - size_t *value_lengths, char **values, - size_t *result_len, unsigned char **result, int flag); +ssize_t aaip_encode(size_t num_attrs, char **names, + size_t *value_lengths, char **values, + size_t *result_len, unsigned char **result, int flag); /* ------ ACL representation ------ */ diff --git a/libisofs/fs_local.c b/libisofs/fs_local.c index da40f00..8bb3c57 100644 --- a/libisofs/fs_local.c +++ b/libisofs/fs_local.c @@ -500,7 +500,8 @@ static int lfs_get_aa_string(IsoFileSource *src, unsigned char **aa_string, int flag) { int ret; - size_t num_attrs = 0, *value_lengths = NULL, result_len, sret; + size_t num_attrs = 0, *value_lengths = NULL, result_len; + ssize_t sret; char *path = NULL, **names = NULL, **values = NULL; unsigned char *result = NULL; @@ -533,10 +534,10 @@ int lfs_get_aa_string(IsoFileSource *src, unsigned char **aa_string, int flag) else { sret = aaip_encode(num_attrs, names, value_lengths, values, &result_len, &result, 0); - if (sret == 0) { - ret = ISO_OUT_OF_MEM; + if (sret < 0) { + ret = sret; goto ex; - } + } } *aa_string = result; ret = 1; diff --git a/libisofs/node.c b/libisofs/node.c index f377454..b8ca5d1 100644 --- a/libisofs/node.c +++ b/libisofs/node.c @@ -1959,7 +1959,8 @@ int iso_node_set_attrs(IsoNode *node, size_t num_attrs, char **names, size_t *value_lengths, char **values, int flag) { int ret, acl_saved = 0; - size_t sret, result_len, m_num = 0, *m_value_lengths = NULL, i; + ssize_t sret; + size_t result_len, m_num = 0, *m_value_lengths = NULL, i; unsigned char *result = NULL; char *a_acl = NULL, *d_acl = NULL, **m_names = NULL, **m_values = NULL; @@ -1999,8 +2000,8 @@ int iso_node_set_attrs(IsoNode *node, size_t num_attrs, char **names, } sret = aaip_encode(num_attrs, names, value_lengths, values, &result_len, &result, 0); - if (sret == 0) { - ret = ISO_OUT_OF_MEM; + if (sret < 0) { + ret = sret; goto ex; } @@ -2010,20 +2011,23 @@ int iso_node_set_attrs(IsoNode *node, size_t num_attrs, char **names, free(result); goto ex; } - ret = iso_node_add_xinfo(node, aaip_xinfo_func, result); - if (ret < 0) - goto ex; - if (ret == 0) { - - /* >>> something is messed up with xinfo: an aa_string still exists */; - - ret = ISO_ERROR; - goto ex; - } - if (acl_saved) { - ret = iso_node_set_acl_text(node, a_acl, d_acl, 0); + if (sret > 0) { + ret = iso_node_add_xinfo(node, aaip_xinfo_func, result); if (ret < 0) goto ex; + if (ret == 0) { + + /* >>> something is messed up with xinfo: + an aa_string still exists */; + + ret = ISO_ERROR; + goto ex; + } + if (acl_saved) { + ret = iso_node_set_acl_text(node, a_acl, d_acl, 0); + if (ret < 0) + goto ex; + } } ret = 1; ex:;