Skip to content

Commit 63065aa

Browse files
committed
common: handle taken() extra_tlvs in new_existing_htlc properly.
Reported-by: Christian Decker Signed-off-by: Rusty Russell <[email protected]>
1 parent 5c1fd78 commit 63065aa

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

common/htlc_wire.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ static struct failed_htlc *failed_htlc_dup(const tal_t *ctx,
2525
return newf;
2626
}
2727

28+
/* Helper to duplicate an array of tlv_field (vs an array of tlv_field *) */
29+
struct tlv_field *tlv_field_arr_dup(const tal_t *ctx,
30+
const struct tlv_field *arr TAKES)
31+
{
32+
struct tlv_field *ret;
33+
bool needs_copy = !is_taken(arr);
34+
35+
ret = tal_dup_talarr(ctx, struct tlv_field, arr);
36+
if (needs_copy) {
37+
for (size_t i = 0; i < tal_count(ret); i++) {
38+
/* We need to attach the value to the correct parent */
39+
ret[i].value = tal_dup_talarr(ret, u8, ret[i].value);
40+
}
41+
}
42+
return ret;
43+
}
44+
2845
struct existing_htlc *new_existing_htlc(const tal_t *ctx,
2946
u64 id,
3047
enum htlc_state state,
@@ -53,17 +70,10 @@ struct existing_htlc *new_existing_htlc(const tal_t *ctx,
5370
existing->failed = failed_htlc_dup(existing, failed);
5471
else
5572
existing->failed = NULL;
56-
if (extra_tlvs) {
57-
existing->extra_tlvs = tal_dup_talarr(existing, struct tlv_field, extra_tlvs);
58-
for (size_t i = 0; i < tal_count(extra_tlvs); i++) {
59-
/* We need to attach the value to the correct parent */
60-
existing->extra_tlvs[i].value
61-
= tal_dup_talarr(existing, u8,
62-
existing->extra_tlvs[i].value);
63-
}
64-
} else {
73+
if (extra_tlvs)
74+
existing->extra_tlvs = tlv_field_arr_dup(existing, extra_tlvs);
75+
else
6576
existing->extra_tlvs = NULL;
66-
}
6777

6878
return existing;
6979
}

common/htlc_wire.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ struct changed_htlc {
6262
u64 id;
6363
};
6464

65+
/* Helper to duplicate an array of tlv_field (vs an array of tlv_field *) */
66+
struct tlv_field *tlv_field_arr_dup(const tal_t *ctx,
67+
const struct tlv_field *arr TAKES);
68+
6569
struct existing_htlc *new_existing_htlc(const tal_t *ctx,
6670
u64 id,
6771
enum htlc_state state,

lightningd/htlc_end.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <ccan/crypto/siphash24/siphash24.h>
44
#include <ccan/tal/str/str.h>
55
#include <common/htlc.h>
6+
#include <common/htlc_wire.h>
67
#include <common/pseudorand.h>
78
#include <lightningd/htlc_end.h>
89
#include <lightningd/log.h>
@@ -148,15 +149,10 @@ struct htlc_in *new_htlc_in(const tal_t *ctx,
148149
hin->path_key = tal_dup_or_null(hin, struct pubkey, path_key);
149150
memcpy(hin->onion_routing_packet, onion_routing_packet,
150151
sizeof(hin->onion_routing_packet));
151-
if (extra_tlvs) {
152-
hin->extra_tlvs = tal_dup_talarr(hin, struct tlv_field, extra_tlvs);
153-
for (size_t i = 0; i < tal_count(extra_tlvs); i++) {
154-
/* We need to attach the value to the correct parent */
155-
hin->extra_tlvs[i].value = tal_dup_talarr(hin, u8, hin->extra_tlvs[i].value);
156-
}
157-
} else {
152+
if (extra_tlvs)
153+
hin->extra_tlvs = tlv_field_arr_dup(hin, extra_tlvs);
154+
else
158155
hin->extra_tlvs = NULL;
159-
}
160156

161157
hin->hstate = RCVD_ADD_COMMIT;
162158
hin->badonion = 0;
@@ -304,15 +300,10 @@ struct htlc_out *new_htlc_out(const tal_t *ctx,
304300

305301
hout->path_key = tal_dup_or_null(hout, struct pubkey, path_key);
306302

307-
if (extra_tlvs) {
308-
hout->extra_tlvs = tal_dup_talarr(hout, struct tlv_field, extra_tlvs);
309-
for (size_t i = 0; i < tal_count(extra_tlvs); i++) {
310-
/* We need to attach the value to the correct parent */
311-
hout->extra_tlvs[i].value = tal_dup_talarr(hout, u8, hout->extra_tlvs[i].value);
312-
}
313-
} else {
303+
if (extra_tlvs)
304+
hout->extra_tlvs = tlv_field_arr_dup(hout, extra_tlvs);
305+
else
314306
hout->extra_tlvs = NULL;
315-
}
316307

317308
hout->am_origin = am_origin;
318309
if (am_origin) {

0 commit comments

Comments
 (0)