diff --git a/src/ckdb.h b/src/ckdb.h index a26155ec..61271803 100644 --- a/src/ckdb.h +++ b/src/ckdb.h @@ -55,7 +55,7 @@ #define DB_VLOCK "1" #define DB_VERSION "1.0.1" -#define CKDB_VERSION DB_VERSION"-1.210" +#define CKDB_VERSION DB_VERSION"-1.211" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ @@ -483,11 +483,28 @@ enum cmd_values { LIST_MEM_ADD(_list, _val); \ _fld = strdup(_val); \ if (!(_fld)) \ - quithere(1, "malloc OOM"); \ + quithere(1, "strdup OOM"); \ } \ } \ } while (0) +#define DUP_POINTER(_list, _fld, _val) do { \ + if ((_fld) && ((_fld) != EMPTY)) { \ + if (_list) \ + LIST_MEM_SUB(_list, _fld); \ + free(_fld); \ + } \ + if (!(_val) || !(*(_val))) \ + (_fld) = EMPTY; \ + else { \ + if (_list) \ + LIST_MEM_ADD(_list, _val); \ + _fld = strdup(_val); \ + if (!(_fld)) \ + quithere(1, "strdup OOM"); \ + } \ + } while (0) + #define SET_CREATEBY(_list, _fld, _val) SET_POINTER(_list, _fld, _val, by_default) #define SET_CREATECODE(_list, _fld, _val) SET_POINTER(_list, _fld, _val, EMPTY) #define SET_CREATEINET(_list, _fld, _val) SET_POINTER(_list, _fld, _val, inet_default) @@ -2276,7 +2293,7 @@ extern K_ITEM *find_markersummary_userid(int64_t userid, char *workername, _find_markersummary(_markerid, 0, KANO, EMPTY, true) #define POOL_MS(_row) do { \ (_row)->userid = KANO; \ - (_row)->workername = strdup(EMPTY); \ + (_row)->workername = EMPTY; \ } while (0) extern K_ITEM *_find_markersummary(int64_t markerid, int64_t workinfoid, int64_t userid, char *workername, bool pool); diff --git a/src/ckdb_cmd.c b/src/ckdb_cmd.c index 00d787c3..bdedbf89 100644 --- a/src/ckdb_cmd.c +++ b/src/ckdb_cmd.c @@ -3794,10 +3794,8 @@ static char *cmd_setopts(PGconn *conn, char *cmd, char *id, gotvalue = false; } if (strcmp(dot, "value") == 0) { - optioncontrol->optionvalue = strdup(data); - if (!(optioncontrol->optionvalue)) - quithere(1, "malloc (%d) OOM", (int)strlen(data)); - LIST_MEM_ADD(optioncontrol_free, optioncontrol->optionvalue); + DUP_POINTER(optioncontrol_free, + optioncontrol->optionvalue, data); gotvalue = true; } else if (strcmp(dot, "date") == 0) { att_to_date(&(optioncontrol->activationdate), data, now); @@ -4695,8 +4693,7 @@ static char *cmd_payouts(PGconn *conn, char *cmd, char *id, tv_t *now, payouts2->diffused = payouts->diffused; payouts2->shareacc = payouts->shareacc; copy_tv(&(payouts2->lastshareacc), &(payouts->lastshareacc)); - payouts2->stats = strdup(payouts->stats); - LIST_MEM_ADD(payouts_free, payouts2->stats); + DUP_POINTER(payouts_free, payouts2->stats, payouts->stats); ok = payouts_add(conn, true, p2_item, &old_p2_item, by, code, inet, now, NULL, false); @@ -4760,8 +4757,7 @@ static char *cmd_payouts(PGconn *conn, char *cmd, char *id, tv_t *now, payouts2->diffused = payouts->diffused; payouts2->shareacc = payouts->shareacc; copy_tv(&(payouts2->lastshareacc), &(payouts->lastshareacc)); - payouts2->stats = strdup(payouts->stats); - LIST_MEM_ADD(payouts_free, payouts2->stats); + DUP_POINTER(payouts_free, payouts2->stats, payouts->stats); ok = payouts_add(conn, true, p2_item, &old_p2_item, by, code, inet, now, NULL, false); diff --git a/src/ckdb_crypt.c b/src/ckdb_crypt.c index 42e7f43c..ba9c82f4 100644 --- a/src/ckdb_crypt.c +++ b/src/ckdb_crypt.c @@ -118,12 +118,7 @@ K_ITEM *gen_2fa_key(K_ITEM *old_u_item, int32_t entropy, char *by, char *code, K_WUNLOCK(users_free); DATA_USERS(users, u_item); memcpy(users, old_users, sizeof(*users)); - if (users->userdata != EMPTY) { - users->userdata = strdup(users->userdata); - if (!users->userdata) - quithere(1, "strdup OOM"); - LIST_MEM_ADD(users_free, users->userdata); - } + DUP_POINTER(users_free, users->userdata, old_users->userdata); users_userdata_add_bin(users, USER_TOTPAUTH_NAME, USER_TOTPAUTH, key, sizeof(key)); users_userdata_add_txt(users, USER_TEST2FA_NAME, @@ -226,12 +221,7 @@ bool tst_2fa(K_ITEM *old_u_item, int32_t value, char *by, char *code, K_WUNLOCK(users_free); DATA_USERS(users, u_item); memcpy(users, old_users, sizeof(*users)); - if (users->userdata != EMPTY) { - users->userdata = strdup(users->userdata); - if (!users->userdata) - quithere(1, "strdup OOM"); - LIST_MEM_ADD(users_free, users->userdata); - } + DUP_POINTER(users_free, users->userdata, old_users->userdata); users_userdata_del(users, USER_TEST2FA_NAME, USER_TEST2FA); ok = users_replace(NULL, u_item, old_u_item, by, code, inet, cd, trf_root); diff --git a/src/ckdb_data.c b/src/ckdb_data.c index d44d870d..697e9c43 100644 --- a/src/ckdb_data.c +++ b/src/ckdb_data.c @@ -3672,8 +3672,7 @@ bool process_pplns(int32_t height, char *blockhash, tv_t *addr_cd) diff_times, FLDSEP, diff_add, FLDSEP, total_share_count, FLDSEP, ss_count, FLDSEP, wm_count, FLDSEP, ms_count, FLDSEP, cd_buf); - payouts->stats = buf; - LIST_MEM_ADD(payouts_free, payouts->stats); + DUP_POINTER(payouts_free, payouts->stats, &buf[0]); conned = CKPQConn(&conn); begun = CKPQBegin(conn); @@ -3887,10 +3886,6 @@ bool process_pplns(int32_t height, char *blockhash, tv_t *addr_cd) ss_count, wm_count, ms_count, usercount, diff_times, diff_add, cd_buf); - // convert the stack memory to heap memeory - payouts->stats = strdup(payouts->stats); - LIST_MEM_ADD(payouts_free, payouts->stats); - K_WLOCK(payouts_free); p2_item = k_unlink_head(payouts_free); K_WUNLOCK(payouts_free); @@ -3908,8 +3903,7 @@ bool process_pplns(int32_t height, char *blockhash, tv_t *addr_cd) payouts2->diffused = payouts->diffused; payouts2->shareacc = payouts->shareacc; copy_tv(&(payouts2->lastshareacc), &(payouts->lastshareacc)); - payouts2->stats = strdup(payouts->stats); - LIST_MEM_ADD(payouts_free, payouts2->stats); + DUP_POINTER(payouts_free, payouts2->stats, payouts->stats); setnow(&now); /* N.B. the PROCESSING payouts could have expirydate = createdate diff --git a/src/ckdb_dbio.c b/src/ckdb_dbio.c index 3b131b55..ec7647c7 100644 --- a/src/ckdb_dbio.c +++ b/src/ckdb_dbio.c @@ -428,12 +428,7 @@ bool users_update(PGconn *conn, K_ITEM *u_item, char *oldhash, STRNCPY(row->emailaddress, email); if (status) STRNCPY(row->status, status); - if (row->userdata != EMPTY) { - row->userdata = strdup(users->userdata); - if (!row->userdata) - quithere(1, "strdup OOM"); - LIST_MEM_ADD(users_free, row->userdata); - } + DUP_POINTER(users_free, row->userdata, users->userdata); HISTORYDATEINIT(row, cd, by, code, inet); HISTORYDATETRANSFER(trf_root, row); @@ -2649,14 +2644,8 @@ int64_t workinfo_add(PGconn *conn, char *workinfoidstr, char *poolinstance, TXT_TO_BIGINT("workinfoid", workinfoidstr, row->workinfoid); STRNCPY(row->poolinstance, poolinstance); - row->transactiontree = strdup(transactiontree); - if (!(row->transactiontree)) - quithere(1, "malloc (%d) OOM", (int)strlen(transactiontree)); - LIST_MEM_ADD(workinfo_free, row->transactiontree); - row->merklehash = strdup(merklehash); - if (!(row->merklehash)) - quithere(1, "malloc (%d) OOM", (int)strlen(merklehash)); - LIST_MEM_ADD(workinfo_free, row->merklehash); + DUP_POINTER(workinfo_free, row->transactiontree, transactiontree); + DUP_POINTER(workinfo_free, row->merklehash, merklehash); STRNCPY(row->prevhash, prevhash); STRNCPY(row->coinbase1, coinbase1); STRNCPY(row->coinbase2, coinbase2); @@ -3680,9 +3669,11 @@ bool sharesummaries_to_markersummaries(PGconn *conn, WORKMARKERS *workmarkers, bzero(markersummary, sizeof(*markersummary)); markersummary->markerid = workmarkers->markerid; markersummary->userid = sharesummary->userid; - markersummary->workername = strdup(sharesummary->workername); - LIST_MEM_ADD(markersummary_free, markersummary->workername); - ms_root = add_to_ktree(ms_root, ms_item, cmp_markersummary); + DUP_POINTER(markersummary_free, + markersummary->workername, + sharesummary->workername); + ms_root = add_to_ktree(ms_root, ms_item, + cmp_markersummary); LOGDEBUG("%s() new ms %"PRId64"/%"PRId64"/%s", shortname, markersummary->markerid, @@ -3820,7 +3811,6 @@ flail: bzero(p_markersummary, sizeof(*p_markersummary)); p_markersummary->markerid = markersummary->markerid; POOL_MS(p_markersummary); - LIST_MEM_ADD(markersummary_free, p_markersummary->workername); markersummary_pool_root = add_to_ktree(markersummary_pool_root, p_ms_item, cmp_markersummary); @@ -4065,8 +4055,8 @@ bool _sharesummary_update(SHARES *s_row, SHAREERRORS *e_row, K_ITEM *ss_item, DATA_SHARESUMMARY(row, item); bzero(row, sizeof(*row)); row->userid = userid; - row->workername = strdup(workername); - LIST_MEM_ADD(sharesummary_free, row->workername); + DUP_POINTER(sharesummary_free, row->workername, + workername); row->workinfoid = workinfoid; } @@ -4136,7 +4126,6 @@ bool _sharesummary_update(SHARES *s_row, SHAREERRORS *e_row, K_ITEM *ss_item, DATA_SHARESUMMARY(p_row, p_item); bzero(p_row, sizeof(*p_row)); POOL_SS(p_row); - LIST_MEM_ADD(sharesummary_free, p_row->workername); p_row->workinfoid = workinfoid; } @@ -6416,7 +6405,6 @@ bool markersummary_fill(PGconn *conn) bzero(p_row, sizeof(*p_row)); p_row->markerid = row->markerid; POOL_MS(p_row); - LIST_MEM_ADD(markersummary_free, p_row->workername); markersummary_pool_root = add_to_ktree(markersummary_pool_root, p_item, cmp_markersummary); @@ -6596,12 +6584,10 @@ bool _workmarkers_process(PGconn *conn, bool already, bool add, } } - row->poolinstance = strdup(poolinstance); - LIST_MEM_ADD(workmarkers_free, poolinstance); + DUP_POINTER(workmarkers_free, row->poolinstance, poolinstance); row->workinfoidend = workinfoidend; row->workinfoidstart = workinfoidstart; - row->description = strdup(description); - LIST_MEM_ADD(workmarkers_free, description); + DUP_POINTER(workmarkers_free, row->description, description); STRNCPY(row->status, status); HISTORYDATEINIT(row, cd, by, code, inet); HISTORYDATETRANSFER(trf_root, row); @@ -6897,13 +6883,10 @@ bool _marks_process(PGconn *conn, bool add, char *poolinstance, K_WUNLOCK(marks_free); DATA_MARKS(row, m_item); bzero(row, sizeof(*row)); - row->poolinstance = strdup(poolinstance); - LIST_MEM_ADD(marks_free, poolinstance); + DUP_POINTER(marks_free, row->poolinstance, poolinstance); row->workinfoid = workinfoid; - row->description = strdup(description); - LIST_MEM_ADD(marks_free, description); - row->extra = strdup(extra); - LIST_MEM_ADD(marks_free, extra); + DUP_POINTER(marks_free, row->description, description); + DUP_POINTER(marks_free, row->extra, extra); STRNCPY(row->marktype, marktype); STRNCPY(row->status, status); HISTORYDATEINIT(row, cd, by, code, inet);