Skip to content

Commit

Permalink
plugins/bcli: strip trailing whitespace appropriately
Browse files Browse the repository at this point in the history
Changelog-Fixed: Handle windows-style newlines and other trailing whitespaces correctly in bitcoin-cli interface
  • Loading branch information
nalinbhardwaj authored and rustyrussell committed May 3, 2021
1 parent ff6b26b commit ae06bb9
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions plugins/bcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,15 @@ start_bitcoin_cli(const tal_t *ctx,
next_bcli(bcli->prio);
}

static void strip_trailing_whitespace(char *str, size_t len)
{
size_t stripped_len = len;
while (stripped_len > 0 && cisspace(str[stripped_len-1]))
stripped_len--;

str[stripped_len] = 0x00;
}

static struct command_result *command_err_bcli_badjson(struct bitcoin_cli *bcli,
const char *errmsg)
{
Expand Down Expand Up @@ -595,8 +604,7 @@ static struct command_result *process_getrawblock(struct bitcoin_cli *bcli)
struct json_stream *response;
struct getrawblock_stash *stash = bcli->stash;

/* -1 to strip \n and steal onto the stash. */
bcli->output[bcli->output_bytes-1] = 0x00;
strip_trailing_whitespace(bcli->output, bcli->output_bytes);
stash->block_hex = tal_steal(stash, bcli->output);

response = jsonrpc_stream_success(bcli->cmd);
Expand Down Expand Up @@ -631,9 +639,8 @@ static struct command_result *process_getblockhash(struct bitcoin_cli *bcli)
return getrawblockbyheight_notfound(bcli);
}

/* `-1` to strip the newline character. */
stash->block_hash = tal_strndup(stash, bcli->output,
bcli->output_bytes-1);
strip_trailing_whitespace(bcli->output, bcli->output_bytes);
stash->block_hash = tal_strdup(stash, bcli->output);
if (!stash->block_hash || strlen(stash->block_hash) != 64) {
return command_err_bcli_badjson(bcli, "bad blockhash");
}
Expand Down

0 comments on commit ae06bb9

Please sign in to comment.