From 70379428d6f528ae51d3cd1fbd02a75a4e13f845 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Wed, 22 Apr 2015 19:51:20 +1000 Subject: [PATCH] Steal copied id_vals in the stratifier to avoid needing to copy them again, thus allowing opaque objects to be used as id values --- src/stratifier.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index 500cb835..e5f704a3 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -3265,7 +3265,7 @@ static void send_json_err(sdata_t *sdata, const int64_t client_id, json_t *id_va { json_t *val; - JSON_CPACK(val, "{soss}", "id", json_copy(id_val), "error", err_msg); + JSON_CPACK(val, "{soss}", "id", json_deep_copy(id_val), "error", err_msg); stratum_add_send(sdata, val, client_id); } @@ -3625,10 +3625,18 @@ static void discard_json_params(json_params_t *jp) { json_decref(jp->method); json_decref(jp->params); - json_decref(jp->id_val); + if (jp->id_val) + json_decref(jp->id_val); free(jp); } +static void steal_json_id(json_t *val, json_params_t *jp) +{ + /* Steal the id_val as is to avoid a copy */ + json_object_set_new_nocheck(val, "id", jp->id_val); + jp->id_val = NULL; +} + static void sshare_process(ckpool_t *ckp, json_params_t *jp) { json_t *result_val, *json_msg, *err_val = NULL; @@ -3651,7 +3659,7 @@ static void sshare_process(ckpool_t *ckp, json_params_t *jp) result_val = parse_submit(client, json_msg, jp->params, &err_val); json_object_set_new_nocheck(json_msg, "result", result_val); json_object_set_new_nocheck(json_msg, "error", err_val ? err_val : json_null()); - json_object_set_nocheck(json_msg, "id", jp->id_val); + steal_json_id(json_msg, jp); stratum_add_send(sdata, json_msg, client_id); out_decref: dec_instance_ref(sdata, client); @@ -3713,7 +3721,7 @@ static void sauth_process(ckpool_t *ckp, json_params_t *jp) json_msg = json_object(); json_object_set_new_nocheck(json_msg, "result", result_val); json_object_set_new_nocheck(json_msg, "error", err_val ? err_val : json_null()); - json_object_set_nocheck(json_msg, "id", jp->id_val); + steal_json_id(json_msg, jp); stratum_add_send(sdata, json_msg, client_id); if (!json_is_true(result_val) || !client->suggest_diff) @@ -3877,7 +3885,7 @@ static void send_transactions(ckpool_t *ckp, json_params_t *jp) goto out; } val = json_object(); - json_object_set_nocheck(val, "id", jp->id_val); + steal_json_id(val, jp); if (cmdmatch(msg, "mining.get_transactions")) { int txns;