Failure to calloc in bin2hex is a fatal failure always so just check for that failure within the function and abort, simplifying the rest of the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
diff --git a/cgminer.c b/cgminer.c
index ca0a92b..542c813 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -379,25 +379,8 @@ static void sharelog(const char*disposition, const struct work*work)
pool = work->pool;
t = (unsigned long int)(work->tv_work_found.tv_sec);
target = bin2hex(work->target, sizeof(work->target));
- if (unlikely(!target)) {
- applog(LOG_ERR, "sharelog target OOM");
- return;
- }
-
hash = bin2hex(work->hash, sizeof(work->hash));
- if (unlikely(!hash)) {
- free(target);
- applog(LOG_ERR, "sharelog hash OOM");
- return;
- }
-
data = bin2hex(work->data, sizeof(work->data));
- if (unlikely(!data)) {
- free(target);
- free(hash);
- applog(LOG_ERR, "sharelog data OOM");
- return;
- }
// timestamp,disposition,target,pool,dev,thr,sharehash,sharedata
rv = snprintf(s, sizeof(s), "%lu,%s,%s,%s,%s%u,%u,%s,%s\n", t, disposition, target, pool->rpc_url, cgpu->api->name, cgpu->device_id, thr_id, hash, data);
@@ -2022,10 +2005,6 @@ static bool submit_upstream_work(struct work *work, CURL *curl, bool resubmit)
/* build hex string */
hexstr = bin2hex(work->data, sizeof(work->data));
- if (unlikely(!hexstr)) {
- applog(LOG_ERR, "submit_upstream_work OOM");
- goto out_nofree;
- }
/* build JSON-RPC request */
sprintf(s,
@@ -2137,7 +2116,6 @@ static bool submit_upstream_work(struct work *work, CURL *curl, bool resubmit)
rc = true;
out:
free(hexstr);
-out_nofree:
return rc;
}
@@ -3158,10 +3136,6 @@ static inline bool from_existing_block(struct work *work)
char *hexstr = bin2hex(work->data + 8, 18);
bool ret;
- if (unlikely(!hexstr)) {
- applog(LOG_ERR, "from_existing_block OOM");
- return true;
- }
ret = block_exists(hexstr);
free(hexstr);
return ret;
@@ -3181,10 +3155,6 @@ static bool test_work_current(struct work *work)
return ret;
hexstr = bin2hex(work->data + 8, 18);
- if (unlikely(!hexstr)) {
- applog(LOG_ERR, "stage_thread OOM");
- return ret;
- }
/* Search to see if this block exists yet and if not, consider it a
* new block and set the current block details to this one */
@@ -4645,10 +4615,8 @@ static void set_work_target(struct work *work, int diff)
if (opt_debug) {
char *htarget = bin2hex(target, 32);
- if (likely(htarget)) {
- applog(LOG_DEBUG, "Generated target %s", htarget);
- free(htarget);
- }
+ applog(LOG_DEBUG, "Generated target %s", htarget);
+ free(htarget);
}
memcpy(work->target, target, 32);
}
@@ -4671,8 +4639,6 @@ static void gen_stratum_work(struct pool *pool, struct work *work)
/* Generate coinbase */
nonce2 = bin2hex((const unsigned char *)&pool->nonce2, pool->n2size);
- if (unlikely(!nonce2))
- quit(1, "Failed to convert nonce2 in gen_stratum_work");
pool->nonce2++;
cb1_len = strlen(pool->swork.coinbase1) / 2;
n1_len = strlen(pool->nonce1) / 2;
@@ -4700,8 +4666,6 @@ static void gen_stratum_work(struct pool *pool, struct work *work)
for (i = 0; i < 32 / 4; i++)
swap32[i] = swab32(data32[i]);
merkle_hash = (unsigned char *)bin2hex((const unsigned char *)merkle_root, 32);
- if (unlikely(!merkle_hash))
- quit(1, "Failed to conver merkle_hash in gen_stratum_work");
sprintf(header, "%s", pool->swork.bbversion);
strcat(header, pool->swork.prev_hash);
diff --git a/driver-icarus.c b/driver-icarus.c
index c013b5d..c3adafa 100644
--- a/driver-icarus.c
+++ b/driver-icarus.c
@@ -554,22 +554,19 @@ static bool icarus_detect_one(const char *devpath)
icarus_close(fd);
nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
- if (nonce_hex) {
- if (strncmp(nonce_hex, golden_nonce, 8)) {
- applog(LOG_ERR,
- "Icarus Detect: "
- "Test failed at %s: get %s, should: %s",
- devpath, nonce_hex, golden_nonce);
- free(nonce_hex);
- return false;
- }
- applog(LOG_DEBUG,
+ if (strncmp(nonce_hex, golden_nonce, 8)) {
+ applog(LOG_ERR,
"Icarus Detect: "
- "Test succeeded at %s: got %s",
- devpath, nonce_hex);
+ "Test failed at %s: get %s, should: %s",
+ devpath, nonce_hex, golden_nonce);
free(nonce_hex);
- } else
return false;
+ }
+ applog(LOG_DEBUG,
+ "Icarus Detect: "
+ "Test succeeded at %s: got %s",
+ devpath, nonce_hex);
+ free(nonce_hex);
/* We have a real Icarus! */
struct cgpu_info *icarus;
@@ -704,11 +701,9 @@ static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
if (opt_debug) {
ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
- if (ob_hex) {
- applog(LOG_DEBUG, "Icarus %d sent: %s",
- icarus->device_id, ob_hex);
- free(ob_hex);
- }
+ applog(LOG_DEBUG, "Icarus %d sent: %s",
+ icarus->device_id, ob_hex);
+ free(ob_hex);
}
/* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
diff --git a/util.c b/util.c
index 794f66f..c58969a 100644
--- a/util.c
+++ b/util.c
@@ -548,7 +548,7 @@ char *bin2hex(const unsigned char *p, size_t len)
slen += 4 - (slen % 4);
s = calloc(slen, 1);
if (unlikely(!s))
- return NULL;
+ quit(1, "Failed to calloc in bin2hex");
for (i = 0; i < len; i++)
sprintf(s + (i * 2), "%02x", (unsigned int) p[i]);