Skip to content

Commit

Permalink
wallet: fix radio function memleaks
Browse files Browse the repository at this point in the history
-total redesign of dogecoin_unregister_watch_address_with_node to alleviate calls to concat
-populate wallet->filename on dogecoin_wallet_new otherwise it's populated by name and freed
-added replace_last_after_delim which searchs for a match and replaces with replacement
-added remove_substr which returns a string with substr removed
  • Loading branch information
xanimo committed Dec 22, 2023
1 parent e9ee8be commit dd0c2c1
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 94 deletions.
1 change: 1 addition & 0 deletions include/dogecoin/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ LIBDOGECOIN_API void prepend(char* s, const char* t);
LIBDOGECOIN_API void append(char* s, char* t);
LIBDOGECOIN_API char* concat(char* prefix, char* suffix);
LIBDOGECOIN_API void slice(const char *str, char *result, size_t start, size_t end);
LIBDOGECOIN_API void replace_last_after_delim(const char *str, char* delim, char* replacement);
LIBDOGECOIN_API void text_to_hex(char* in, char* out);
LIBDOGECOIN_API const char* get_build();
LIBDOGECOIN_API char* getpass(const char *prompt);
Expand Down
2 changes: 1 addition & 1 deletion include/dogecoin/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ DISABLE_WARNING_POP

/** single key/value record */
typedef struct dogecoin_wallet_ {
const char* filename;
const char filename[311]; // max path length
FILE *dbfile;
dogecoin_hdnode* masterkey;
uint32_t next_childindex; //cached next child index
Expand Down
9 changes: 7 additions & 2 deletions src/cli/spvnode.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,17 +477,22 @@ int main(int argc, char* argv[]) {
printf("registered: %d %s\n", res, address);
uint64_t amount = dogecoin_get_balance(address);
if (amount > 0) {
printf("amount: %s\n", dogecoin_get_balance_str(address));
char* amount_str = dogecoin_get_balance_str(address);
printf("amount: %s\n", amount_str);
unsigned int utxo_count = dogecoin_get_utxos_length(address);
if (utxo_count) {
printf("utxo count: %d\n", utxo_count);
unsigned int i = 1;
for (; i <= utxo_count; i++) {
printf("txid: %s\n", dogecoin_get_utxo_txid_str(address, i));
printf("vout: %d\n", dogecoin_get_utxo_vout(address, i));
printf("amount: %s\n", dogecoin_get_utxo_amount(address, i));
sleep(1);
char* utxo_amount_str = dogecoin_get_utxo_amount(address, i);
printf("amount: %s\n", utxo_amount_str);
dogecoin_free(utxo_amount_str);
}
}
dogecoin_free(amount_str);
}
res = dogecoin_unregister_watch_address_with_node(address);
printf("unregistered: %s\n", res ? "true" : "false");
Expand Down
25 changes: 25 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,31 @@ void slice(const char *str, char *result, size_t start, size_t end)
strncpy(result, str + start, end - start);
}

void remove_substr(char *string, char *sub) {
char *match;
int len = strlen(sub);
while ((match = strstr(string, sub))) {
*match = '\0';
strcat(string, match+len);
}
}

void replace_last_after_delim(const char *str, char* delim, char* replacement) {
char* tmp = strdup((char*)str);
char* new = tmp;
char *strptr = strtok(new, delim);
char* last = NULL;
while (strptr != NULL) {
last = strptr;
strptr = strtok(NULL, delim);
}
if (last) {
remove_substr(str, last);
append(str, replacement);
}
dogecoin_free(tmp);
}

/**
* @brief function to convert ascii text to hexadecimal string
*
Expand Down
Loading

0 comments on commit dd0c2c1

Please sign in to comment.