Browse Source

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.

nfactor-troky
Con Kolivas 12 years ago
parent
commit
99adf397bd
  1. 40
      cgminer.c
  2. 31
      driver-icarus.c
  3. 2
      util.c

40
cgminer.c

@ -379,25 +379,8 @@ static void sharelog(const char*disposition, const struct work*work)
pool = work->pool; pool = work->pool;
t = (unsigned long int)(work->tv_work_found.tv_sec); t = (unsigned long int)(work->tv_work_found.tv_sec);
target = bin2hex(work->target, sizeof(work->target)); target = bin2hex(work->target, sizeof(work->target));
if (unlikely(!target)) {
applog(LOG_ERR, "sharelog target OOM");
return;
}
hash = bin2hex(work->hash, sizeof(work->hash)); 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)); 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 // 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); 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 */ /* build hex string */
hexstr = bin2hex(work->data, sizeof(work->data)); hexstr = bin2hex(work->data, sizeof(work->data));
if (unlikely(!hexstr)) {
applog(LOG_ERR, "submit_upstream_work OOM");
goto out_nofree;
}
/* build JSON-RPC request */ /* build JSON-RPC request */
sprintf(s, sprintf(s,
@ -2137,7 +2116,6 @@ static bool submit_upstream_work(struct work *work, CURL *curl, bool resubmit)
rc = true; rc = true;
out: out:
free(hexstr); free(hexstr);
out_nofree:
return rc; return rc;
} }
@ -3158,10 +3136,6 @@ static inline bool from_existing_block(struct work *work)
char *hexstr = bin2hex(work->data + 8, 18); char *hexstr = bin2hex(work->data + 8, 18);
bool ret; bool ret;
if (unlikely(!hexstr)) {
applog(LOG_ERR, "from_existing_block OOM");
return true;
}
ret = block_exists(hexstr); ret = block_exists(hexstr);
free(hexstr); free(hexstr);
return ret; return ret;
@ -3181,10 +3155,6 @@ static bool test_work_current(struct work *work)
return ret; return ret;
hexstr = bin2hex(work->data + 8, 18); 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 /* 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 */ * 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) { if (opt_debug) {
char *htarget = bin2hex(target, 32); char *htarget = bin2hex(target, 32);
if (likely(htarget)) { applog(LOG_DEBUG, "Generated target %s", htarget);
applog(LOG_DEBUG, "Generated target %s", htarget); free(htarget);
free(htarget);
}
} }
memcpy(work->target, target, 32); memcpy(work->target, target, 32);
} }
@ -4671,8 +4639,6 @@ static void gen_stratum_work(struct pool *pool, struct work *work)
/* Generate coinbase */ /* Generate coinbase */
nonce2 = bin2hex((const unsigned char *)&pool->nonce2, pool->n2size); nonce2 = bin2hex((const unsigned char *)&pool->nonce2, pool->n2size);
if (unlikely(!nonce2))
quit(1, "Failed to convert nonce2 in gen_stratum_work");
pool->nonce2++; pool->nonce2++;
cb1_len = strlen(pool->swork.coinbase1) / 2; cb1_len = strlen(pool->swork.coinbase1) / 2;
n1_len = strlen(pool->nonce1) / 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++) for (i = 0; i < 32 / 4; i++)
swap32[i] = swab32(data32[i]); swap32[i] = swab32(data32[i]);
merkle_hash = (unsigned char *)bin2hex((const unsigned char *)merkle_root, 32); 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); sprintf(header, "%s", pool->swork.bbversion);
strcat(header, pool->swork.prev_hash); strcat(header, pool->swork.prev_hash);

31
driver-icarus.c

@ -554,22 +554,19 @@ static bool icarus_detect_one(const char *devpath)
icarus_close(fd); icarus_close(fd);
nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin)); nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
if (nonce_hex) { if (strncmp(nonce_hex, golden_nonce, 8)) {
if (strncmp(nonce_hex, golden_nonce, 8)) { applog(LOG_ERR,
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,
"Icarus Detect: " "Icarus Detect: "
"Test succeeded at %s: got %s", "Test failed at %s: get %s, should: %s",
devpath, nonce_hex); devpath, nonce_hex, golden_nonce);
free(nonce_hex); free(nonce_hex);
} else
return false; return false;
}
applog(LOG_DEBUG,
"Icarus Detect: "
"Test succeeded at %s: got %s",
devpath, nonce_hex);
free(nonce_hex);
/* We have a real Icarus! */ /* We have a real Icarus! */
struct cgpu_info *icarus; struct cgpu_info *icarus;
@ -704,11 +701,9 @@ static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
if (opt_debug) { if (opt_debug) {
ob_hex = bin2hex(ob_bin, sizeof(ob_bin)); ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
if (ob_hex) { applog(LOG_DEBUG, "Icarus %d sent: %s",
applog(LOG_DEBUG, "Icarus %d sent: %s", icarus->device_id, ob_hex);
icarus->device_id, ob_hex); free(ob_hex);
free(ob_hex);
}
} }
/* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */ /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */

2
util.c

@ -548,7 +548,7 @@ char *bin2hex(const unsigned char *p, size_t len)
slen += 4 - (slen % 4); slen += 4 - (slen % 4);
s = calloc(slen, 1); s = calloc(slen, 1);
if (unlikely(!s)) if (unlikely(!s))
return NULL; quit(1, "Failed to calloc in bin2hex");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
sprintf(s + (i * 2), "%02x", (unsigned int) p[i]); sprintf(s + (i * 2), "%02x", (unsigned int) p[i]);

Loading…
Cancel
Save