Skip to content

Commit cd5b8c4

Browse files
jkauffman1jgriffiths
authored andcommitted
Fix false overflow in wally_tx_set_output_satoshi
1 parent 72b6d06 commit cd5b8c4

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/swig_python/contrib/tx.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ def test_tx_output(self):
5151
self.assertEqual(tx_output_get_script_len(tx_output), len(script))
5252
self.assertEqual(tx_output_get_script(tx_output), script)
5353

54+
def test_tx_set_output(self):
55+
satoshi, script = WALLY_SATOSHI_MAX, b'0000'
56+
57+
# Create tx with single output value = MAX
58+
tx = tx_init(2, 0, 10, 2)
59+
tx_add_output(tx, tx_output_init(satoshi, script))
60+
self.assertEqual(tx_get_output_satoshi(tx, 0), WALLY_SATOSHI_MAX)
61+
self.assertEqual(tx_get_total_output_satoshi(tx), WALLY_SATOSHI_MAX)
62+
63+
# Change value of output from MAX -> 1
64+
tx_set_output_satoshi(tx, 0, 1)
65+
self.assertEqual(tx_get_output_satoshi(tx, 0), 1)
66+
self.assertEqual(tx_get_total_output_satoshi(tx), 1)
67+
5468
def test_tx(self):
5569
txhash, seq, script, witness_script = b'0' * 32, 0xffffffff, b'0000', b'000000'
5670
witness = tx_witness_stack_init(5)

src/transaction.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,9 +1538,13 @@ int wally_tx_set_output_script(const struct wally_tx *tx, size_t index,
15381538

15391539
int wally_tx_set_output_satoshi(const struct wally_tx *tx, size_t index, uint64_t satoshi)
15401540
{
1541-
uint64_t total;
1542-
if (wally_tx_get_total_output_satoshi(tx, &total) != WALLY_OK ||
1543-
total + satoshi < total || total + satoshi > WALLY_SATOSHI_MAX)
1541+
uint64_t current, total;
1542+
1543+
if (wally_tx_get_output_satoshi(tx, index, &current) != WALLY_OK ||
1544+
wally_tx_get_total_output_satoshi(tx, &total) != WALLY_OK)
1545+
return WALLY_EINVAL;
1546+
total -= current;
1547+
if (total + satoshi < total || total + satoshi > WALLY_SATOSHI_MAX)
15441548
return WALLY_EINVAL;
15451549
return wally_tx_output_set_satoshi(tx_get_output(tx, index), satoshi);
15461550
}

0 commit comments

Comments
 (0)