Skip to content

Commit

Permalink
bitcoin: use a length arg to bitcoin_tx_from_hex
Browse files Browse the repository at this point in the history
Our json parser doesn't use nul-terminated strings.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jan 21, 2016
1 parent cf547d4 commit 3374ddd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
20 changes: 14 additions & 6 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -432,18 +432,19 @@ static struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx,
return tx;
}

struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex)
struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex,
size_t hexlen)
{
char *end;
u8 *linear_tx;
const u8 *p;
struct bitcoin_tx *tx;
size_t len;

end = strchr(hex, ':');
end = memchr(hex, ':', hexlen);
if (!end) {
end = cast_const(char *, hex) + strlen(hex);
if (strends(hex, "\n"))
end = cast_const(char *, hex) + hexlen;
if (hexlen > 0 && hex[hexlen-1] == '\n')
end--;
}

Expand All @@ -460,10 +461,17 @@ struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex)
for (len = 0; len < tx->input_count; len++) {
if (*end != ':')
break;
tx->input[len].input_amount = strtoull(end + 1, &end, 10);

tx->input[len].input_amount = 0;
end++;
while (end < hex + hexlen && cisdigit(*end)) {
tx->input[len].input_amount *= 10;
tx->input[len].input_amount += *end - '0';
end++;
}
}
if (len == tx->input_count) {
if (*end != '\0' && *end != '\n')
if (end != hex + hexlen && *end != '\n')
goto fail_free_tx;
} else {
/* Input amounts are compulsory for alpha, to generate sigs */
Expand Down
3 changes: 2 additions & 1 deletion bitcoin/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count,

/* This takes a raw bitcoin tx in hex, with [:<64-bit-satoshi>] appended
* for each input (required for -DALPHA). */
struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex);
struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex,
size_t hexlen);

bool bitcoin_tx_write(int fd, const struct bitcoin_tx *tx);

Expand Down
2 changes: 1 addition & 1 deletion test-cli/tx_from_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct bitcoin_tx *bitcoin_tx_from_file(const tal_t *ctx, const char *filename)
if (!hex)
err(1, "Opening %s", filename);

tx = bitcoin_tx_from_hex(ctx, hex);
tx = bitcoin_tx_from_hex(ctx, hex, strlen(hex));
if (!tx)
err(1, "Failed to decode tx '%s'", hex);
tal_free(hex);
Expand Down

0 comments on commit 3374ddd

Please sign in to comment.