From 14cda04725d9f93643c17c8e6bf091e84f122b00 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 11 Apr 2014 10:45:48 +1000 Subject: [PATCH] Add basic logging warnings to libckpool functions --- src/libckpool.c | 16 ++++++++++++---- src/libckpool.h | 9 +++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/libckpool.c b/src/libckpool.c index b7855824..9bc612fb 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -279,7 +279,9 @@ void *bin2hex(const uchar *p, size_t len) slen = len * 2 + 1; align_len(&slen); s = calloc(slen, 1); - if (likely(s)) + if (unlikely(!s)) { + LOGERR("Failed to calloc s in bin2hex"); + } else __bin2hex(s, p, len); /* Returns NULL if calloc failed. */ @@ -313,16 +315,20 @@ bool hex2bin(uchar *p, const uchar *hexstr, size_t len) bool ret = false; while (*hexstr && len) { - if (unlikely(!hexstr[1])) + if (unlikely(!hexstr[1])) { + LOGWARNING("Early end of string in hex2bin"); return ret; + } idx = *hexstr++; nibble1 = hex2bin_tbl[idx]; idx = *hexstr++; nibble2 = hex2bin_tbl[idx]; - if (unlikely((nibble1 < 0) || (nibble2 < 0))) + if (unlikely((nibble1 < 0) || (nibble2 < 0))) { + LOGWARNING("Invalid binary encoding in hex2bin"); return ret; + } *p++ = (((uchar)nibble1) << 4) | ((uchar)nibble2); --len; @@ -330,6 +336,8 @@ bool hex2bin(uchar *p, const uchar *hexstr, size_t len) if (likely(len == 0 && *hexstr == 0)) ret = true; + if (!ret) + LOGWARNING("Failed hex2bin decode"); return ret; } @@ -384,7 +392,7 @@ uchar *http_base64(const uchar *src) align_len(&hlen); str = malloc(hlen); if (unlikely(!str)) - return str; + quit(1, "Failed to malloc string of length %d in http_base64", (int)hlen); dst = str; r = 0; diff --git a/src/libckpool.h b/src/libckpool.h index 51f5cffb..639e233f 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -68,6 +68,15 @@ exit(status); \ } while (0) +#define quit(status, fmt, ...) do { \ + if (fmt) { \ + fprintf(stderr, fmt, ##__VA_ARGS__); \ + fprintf(stderr, "\n"); \ + fflush(stderr); \ + } \ + exit(status); \ +} while (0) + /* cgminer locks, a write biased variant of rwlocks */ struct cklock { pthread_mutex_t mutex;