From: Pavel Filipenský Date: Fri, 7 Jan 2022 10:57:08 +0000 (+0100) Subject: ctdb:utils: Improve error handling of hex_decode() X-Git-Tag: tdb-1.4.6~174 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5ac8762256830f1c7e48dcc9684802f00fc3b5c2;p=thirdparty%2Fsamba.git ctdb:utils: Improve error handling of hex_decode() This has been found by covscan and make analyzers happy. Pair-programmed-with: Andreas Schneider Signed-off-by: Pavel Filipenský Signed-off-by: Andreas Schneider Reviewed-by: Jeremy Allison --- diff --git a/ctdb/utils/tdb/tdb_mutex_check.c b/ctdb/utils/tdb/tdb_mutex_check.c index da794b8dab5..4da0c40d41b 100644 --- a/ctdb/utils/tdb/tdb_mutex_check.c +++ b/ctdb/utils/tdb/tdb_mutex_check.c @@ -30,30 +30,42 @@ #include "lib/tdb/common/tdb_private.h" #include "lib/tdb/common/mutex.c" -static uint8_t *hex_decode(const char *hex_in, size_t *len) +static uint8_t *hex_decode(const char *hex_in, size_t *plen) { size_t i; int num; uint8_t *buffer; + size_t len; - *len = strlen(hex_in) / 2; - buffer = malloc(*len); + len = strlen(hex_in) / 2; + if (len == 0) { + return NULL; + } + + buffer = malloc(len); + if (buffer == NULL) { + return NULL; + } - for (i=0; i<*len; i++) { + for (i = 0; i < len; i++) { sscanf(&hex_in[i*2], "%02X", &num); buffer[i] = (uint8_t)num; } + *plen = len; + return buffer; } static int get_hash_chain(struct tdb_context *tdb, const char *hex_key) { - TDB_DATA key; + TDB_DATA key = { + .dsize = 0, + }; unsigned int hash; key.dptr = hex_decode(hex_key, &key.dsize); - if (key.dsize == 0) { + if (key.dptr == NULL || key.dsize == 0) { return -1; } hash = tdb_jenkins_hash(&key);