From a723f0cd25b4967c5476786c668ebd50434ab00a Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 5 Sep 2014 14:16:03 +1000 Subject: [PATCH] Rework subscribed bool, not dropping client while parsing method for risk of dereference --- src/stratifier.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index 3dc1c4a7..c0cc640b 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -231,6 +231,7 @@ struct stratum_instance { time_t start_time; char address[INET6_ADDRSTRLEN]; + bool subscribed; bool authorised; bool idle; bool notified_idle; @@ -1244,9 +1245,8 @@ static void new_enonce1(stratum_instance_t *client) } /* Extranonce1 must be set here */ -static json_t *parse_subscribe(int64_t client_id, json_t *params_val) +static json_t *parse_subscribe(stratum_instance_t *client, int64_t client_id, json_t *params_val) { - stratum_instance_t *client = NULL; bool old_match = false; int arr_size; json_t *ret; @@ -1255,15 +1255,6 @@ static json_t *parse_subscribe(int64_t client_id, json_t *params_val) if (unlikely(!json_is_array(params_val))) return json_string("params not an array"); - ck_rlock(&instance_lock); - client = __instance_by_id(client_id); - ck_runlock(&instance_lock); - - if (unlikely(!client)) { - LOGERR("Failed to find client id %d in hashtable!", client_id); - return NULL; - } - if (unlikely(!current_workbase)) return json_string("Initialising"); @@ -1308,6 +1299,8 @@ static json_t *parse_subscribe(int64_t client_id, json_t *params_val) n2len); ck_runlock(&workbase_lock); + client->subscribed = true; + return ret; } @@ -2131,16 +2124,20 @@ static void parse_method(const int64_t client_id, json_t *id_val, json_t *method * the json item for id_val as is for the response. */ method = json_string_value(method_val); if (cmdmatch(method, "mining.subscribe")) { - json_t *val, *result_val = parse_subscribe(client_id, params_val); + json_t *val, *result_val = parse_subscribe(client, client_id, params_val); - if (!result_val) + /* Shouldn't happen, sanity check */ + if (unlikely(!result_val)) { + LOGWARNING("parse_subscribe returned NULL result_val"); return; + } val = json_object(); json_object_set_new_nocheck(val, "result", result_val); json_object_set_nocheck(val, "id", id_val); json_object_set_new_nocheck(val, "error", json_null()); stratum_add_send(val, client_id); - update_client(client, client_id); + if (likely(client->subscribed)) + update_client(client, client_id); return; } @@ -2160,7 +2157,7 @@ static void parse_method(const int64_t client_id, json_t *id_val, json_t *method return; } - if (cmdmatch(method, "mining.auth")) { + if (cmdmatch(method, "mining.auth") && client->subscribed) { json_params_t *jp = create_json_params(client_id, params_val, id_val, address); ckmsgq_add(sauthq, jp);