Commit 5412323e2634b0c9f450570fb13763983577dcca

Con Kolivas 2013-03-09T15:12:41

Fix memory leak with share submission on GPU work structures as discovered by twobitcoins.

diff --git a/cgminer.c b/cgminer.c
index 4f8c66a..cad7719 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -3466,7 +3466,7 @@ void switch_pools(struct pool *selected)
 
 }
 
-static void discard_work(struct work *work)
+void discard_work(struct work *work)
 {
 	if (!work->clone && !work->rolls && !work->mined) {
 		if (work->pool)
diff --git a/findnonce.c b/findnonce.c
index fcf75f2..e99b7e1 100644
--- a/findnonce.c
+++ b/findnonce.c
@@ -1,5 +1,5 @@
 /*
- * Copyright 2011-2012 Con Kolivas
+ * Copyright 2011-2013 Con Kolivas
  * Copyright 2011 Nils Schneider
  *
  * This program is free software; you can redistribute it and/or modify it
@@ -173,7 +173,7 @@ void precalc_hash(dev_blk_ctx *blk, uint32_t *state, uint32_t *data)
 
 struct pc_data {
 	struct thr_info *thr;
-	struct work work;
+	struct work *work;
 	uint32_t res[MAXBUFFERS];
 	pthread_t pth;
 	int found;
@@ -182,10 +182,10 @@ struct pc_data {
 static void send_scrypt_nonce(struct pc_data *pcd, uint32_t nonce)
 {
 	struct thr_info *thr = pcd->thr;
-	struct work *work = &pcd->work;
+	struct work *work = pcd->work;
 
 	if (scrypt_test(work->data, work->target, nonce))
-		submit_nonce(thr, &pcd->work, nonce);
+		submit_nonce(thr, work, nonce);
 	else {
 		applog(LOG_INFO, "Scrypt error, review settings");
 		thr->cgpu->hw_errors++;
@@ -217,9 +217,10 @@ static void *postcalc_hash(void *userdata)
 		if (opt_scrypt)
 			send_scrypt_nonce(pcd, nonce);
 		else
-			submit_nonce(thr, &pcd->work, nonce);
+			submit_nonce(thr, pcd->work, nonce);
 	}
 
+	discard_work(pcd->work);
 	free(pcd);
 
 	return NULL;
@@ -234,7 +235,7 @@ void postcalc_hash_async(struct thr_info *thr, struct work *work, uint32_t *res)
 	}
 
 	pcd->thr = thr;
-	memcpy(&pcd->work, work, sizeof(struct work));
+	pcd->work = copy_work(work);
 	memcpy(&pcd->res, res, BUFFERSIZE);
 
 	if (pthread_create(&pcd->pth, NULL, postcalc_hash, (void *)pcd)) {
diff --git a/miner.h b/miner.h
index a44b9c1..bd24305 100644
--- a/miner.h
+++ b/miner.h
@@ -1119,6 +1119,7 @@ extern int curses_int(const char *query);
 extern char *curses_input(const char *query);
 extern void kill_work(void);
 extern void switch_pools(struct pool *selected);
+extern void discard_work(struct work *work);
 extern void remove_pool(struct pool *pool);
 extern void write_config(FILE *fcfg);
 extern void zero_bestshare(void);