From 3dfe7b95e38c4eeb2a33d2ee35b69a7c2ab9db17 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 2 Dec 2016 13:46:29 +1100 Subject: [PATCH] Fix potential memleak when stratum_add_send is unable to send to a queue. --- src/ckpool.c | 6 ++++-- src/ckpool.h | 2 +- src/stratifier.c | 5 ++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/ckpool.c b/src/ckpool.c index 6b4eb4e3..2fb89da5 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -203,7 +203,7 @@ ckmsgq_t *create_ckmsgqs(ckpool_t *ckp, const char *name, const void *func, cons /* Generic function for adding messages to a ckmsgq linked list and signal the * ckmsgq parsing thread(s) to wake up and process it. */ -void _ckmsgq_add(ckmsgq_t *ckmsgq, void *data, const char *file, const char *func, const int line) +bool _ckmsgq_add(ckmsgq_t *ckmsgq, void *data, const char *file, const char *func, const int line) { ckmsg_t *msg; @@ -212,7 +212,7 @@ void _ckmsgq_add(ckmsgq_t *ckmsgq, void *data, const char *file, const char *fun /* Discard data if we're unlucky enough to be sending it to * msg queues not set up during start up */ free(data); - return; + return false; } while (unlikely(!ckmsgq->active)) cksleep_ms(10); @@ -225,6 +225,8 @@ void _ckmsgq_add(ckmsgq_t *ckmsgq, void *data, const char *file, const char *fun DL_APPEND(ckmsgq->msgs, msg); pthread_cond_broadcast(ckmsgq->cond); mutex_unlock(ckmsgq->lock); + + return true; } /* Return whether there are any messages queued in the ckmsgq linked list. */ diff --git a/src/ckpool.h b/src/ckpool.h index db425b67..8b395234 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -326,7 +326,7 @@ static const char __maybe_unused *stratum_msgs[] = { ckmsgq_t *create_ckmsgq(ckpool_t *ckp, const char *name, const void *func); ckmsgq_t *create_ckmsgqs(ckpool_t *ckp, const char *name, const void *func, const int count); -void _ckmsgq_add(ckmsgq_t *ckmsgq, void *data, const char *file, const char *func, const int line); +bool _ckmsgq_add(ckmsgq_t *ckmsgq, void *data, const char *file, const char *func, const int line); #define ckmsgq_add(ckmsgq, data) _ckmsgq_add(ckmsgq, data, __FILE__, __func__, __LINE__) bool ckmsgq_empty(ckmsgq_t *ckmsgq); unix_msg_t *get_unix_msg(proc_instance_t *pi); diff --git a/src/stratifier.c b/src/stratifier.c index 720731bd..b3e69549 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -3082,7 +3082,10 @@ static void stratum_add_send(sdata_t *sdata, json_t *val, const int64_t client_i msg = ckzalloc(sizeof(smsg_t)); msg->json_msg = val; msg->client_id = client_id; - ckmsgq_add(sdata->ssends, msg); + if (likely(ckmsgq_add(sdata->ssends, msg))) + return; + json_decref(msg->json_msg); + free(msg); } static void drop_client(ckpool_t *ckp, sdata_t *sdata, const int64_t id)