Commit b97a33a58a9195b76e115b98f05ba0593839e6b7

Con Kolivas 2012-10-01T14:44:44

Use statically allocated stratum strings in struct work to cope with the inability to safely deallocate dynamically allocated ram.

diff --git a/cgminer.c b/cgminer.c
index 65ea5d4..cb0ef19 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -4444,6 +4444,8 @@ 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;
@@ -4471,6 +4473,8 @@ 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);
@@ -4483,9 +4487,10 @@ static void gen_stratum_work(struct pool *pool, struct work *work)
 	diff = pool->swork.diff;
 
 	/* Copy parameters required for share submission */
-	work->job_id = strdup(pool->swork.job_id);
-	work->nonce2 = nonce2;
-	work->ntime = pool->swork.ntime;
+	sprintf(work->job_id, "%s", pool->swork.job_id);
+	sprintf(work->nonce2, "%s", nonce2);
+	sprintf(work->ntime, "%s", pool->swork.ntime);
+	free(nonce2);
 
 	mutex_unlock(&pool->pool_lock);
 
@@ -4495,11 +4500,11 @@ static void gen_stratum_work(struct pool *pool, struct work *work)
 	free(merkle_hash);
 
 	/* Convert hex data to binary data for work */
-	if (!hex2bin(work->data, header, 128))
+	if (unlikely(!hex2bin(work->data, header, 128)))
 		quit(1, "Failed to convert header to data in gen_stratum_work");
 	calc_midstate(work);
 	sprintf(hash1, "00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000");
-	if (!hex2bin(work->hash1, hash1, 64))
+	if (unlikely(!hex2bin(work->hash1, hash1, 64)))
 		quit(1,  "Failed to convert hash1 in gen_stratum_work");
 
 	/* Generate target as hex where 0x00000000FFFFFFFF is diff 1 */
@@ -4507,12 +4512,12 @@ static void gen_stratum_work(struct pool *pool, struct work *work)
 	diff64 = ~htobe64(diff64);
 	sprintf(target, "ffffffffffffffffffffffffffffffffffffffffffffffff");
 	buf = bin2hex((const unsigned char *)&diff64, 8);
-	if (!buf)
+	if (unlikely(!buf))
 		quit(1, "Failed to convert diff64 to buf in gen_stratum_work");
 	strcat(target, buf);
 	free(buf);
 	applog(LOG_DEBUG, "Generated target %s", target);
-	if (!hex2bin(work->target, target, 32))
+	if (unlikely(!hex2bin(work->target, target, 32)))
 		quit(1, "Failed to convert target to bin in gen_stratum_work");
 
 	work->pool = pool;
diff --git a/miner.h b/miner.h
index e216be6..fb30ac9 100644
--- a/miner.h
+++ b/miner.h
@@ -880,9 +880,11 @@ struct work {
 	bool		queued;
 
 	bool		stratum;
-	char 		*job_id;
-	char		*nonce2;
-	char		*ntime;
+	/* These are arbitrary lengths as it is too hard to keep track of
+	 * dynamically allocated ram in work structs */
+	char 		job_id[64];
+	char		nonce2[64];
+	char		ntime[16];
 
 	unsigned int	work_block;
 	int		id;