Create helper functions for duplicating json strings to avoid keeping json references in use.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
diff --git a/miner.h b/miner.h
index a255a6f..c099012 100644
--- a/miner.h
+++ b/miner.h
@@ -754,8 +754,6 @@ enum pool_enable {
struct stratum_work {
/* id we sent to receive this work */
int id;
- /* Reference to json structure all the following were extracted from */
- json_t *json_val;
char *job_id;
char *prev_hash;
@@ -843,8 +841,6 @@ struct pool {
bool has_stratum;
bool stratum_active;
bool stratum_auth;
- /* Store json reference to clear it if we close connection */
- json_t *stratum_val;
struct stratum_work swork;
};
diff --git a/util.c b/util.c
index d97fc90..32ca0e6 100644
--- a/util.c
+++ b/util.c
@@ -926,8 +926,41 @@ out:
return sret;
}
+/* Extracts a string value from a json array with error checking. To be used
+ * when the value of the string returned is only examined and not to be stored.
+ * See json_array_string below */
+static char *__json_array_string(json_t *val, unsigned int entry)
+{
+ json_t *arr_entry;
+
+ if (json_is_null(val))
+ return NULL;
+ if (!json_is_array(val))
+ return NULL;
+ if (entry > json_array_size(val))
+ return NULL;
+ arr_entry = json_array_get(val, entry);
+ if (!json_is_string(arr_entry))
+ return NULL;
+
+ return (json_string_value(arr_entry));
+}
+
+/* Creates a freshly malloced dup of __json_array_string */
+static char *json_array_string(json_t *val, unsigned int entry)
+{
+ char *buf = __json_array_string(val, entry);
+
+ if (buf)
+ return strdup(buf);
+ return NULL;
+}
+
static bool parse_notify(struct pool *pool, json_t *val)
{
+ json_t *arr;
+
+
return true;
}
@@ -1093,18 +1126,18 @@ bool initiate_stratum(struct pool *pool)
goto out;
}
- buf = (char *)json_string_value(json_array_get(notify_val, 0));
+ buf = __json_array_string(notify_val, 0);
if (!buf || strcasecmp(buf, "mining.notify")) {
applog(LOG_WARNING, "Failed to get mining notify in initiate_stratum");
goto out;
}
- pool->subscription = strdup(json_string_value(json_array_get(notify_val, 1)));
+ pool->subscription = json_array_string(notify_val, 1);
if (!pool->subscription) {
applog(LOG_WARNING, "Failed to get a subscription in initiate_stratum");
goto out;
}
- pool->nonce1 = strdup(json_string_value(json_array_get(res_val, 1)));
+ pool->nonce1 = json_array_string(res_val, 1);
if (!pool->nonce1) {
applog(LOG_WARNING, "Failed to get nonce1 in initiate_stratum");
goto out;
@@ -1122,7 +1155,6 @@ out:
if (ret) {
pool->stratum_active = true;
- pool->stratum_val = val;
if (opt_protocol) {
applog(LOG_DEBUG, "Pool %d confirmed mining.notify with subscription %s extranonce1 %s extranonce2 %d",
pool->pool_no, pool->subscription, pool->nonce1, pool->nonce2);