@@ -78,6 +78,18 @@ struct existing_htlc *new_existing_htlc(const tal_t *ctx,
78
78
return existing ;
79
79
}
80
80
81
+ static void towire_len_and_tlvstream (u8 * * pptr , struct tlv_field * extra_tlvs )
82
+ {
83
+ /* Making a copy is a bit awful, but it's the easiest way to
84
+ * get the length */
85
+ u8 * tmp_pptr = tal_arr (tmpctx , u8 , 0 );
86
+ towire_tlvstream_raw (& tmp_pptr , extra_tlvs );
87
+
88
+ assert (tal_bytelen (tmp_pptr ) == (u16 )tal_bytelen (tmp_pptr ));
89
+ towire_u16 (pptr , tal_bytelen (tmp_pptr ));
90
+ towire_u8_array (pptr , tmp_pptr , tal_bytelen (tmp_pptr ));
91
+ }
92
+
81
93
/* FIXME: We could adapt tools/generate-wire.py to generate structures
82
94
* and code like this. */
83
95
void towire_added_htlc (u8 * * pptr , const struct added_htlc * added )
@@ -94,13 +106,8 @@ void towire_added_htlc(u8 **pptr, const struct added_htlc *added)
94
106
} else
95
107
towire_bool (pptr , false);
96
108
if (added -> extra_tlvs ) {
97
- u8 * tmp_pptr = tal_arr (tmpctx , u8 , 0 );
98
- towire_tlvstream_raw (& tmp_pptr , added -> extra_tlvs );
99
-
100
109
towire_bool (pptr , true);
101
- towire_u16 (pptr , tal_bytelen (tmp_pptr ));
102
- towire_u8_array (pptr , tmp_pptr ,
103
- tal_bytelen (tmp_pptr ));
110
+ towire_len_and_tlvstream (pptr , added -> extra_tlvs );
104
111
} else
105
112
towire_bool (pptr , false);
106
113
towire_bool (pptr , added -> fail_immediate );
@@ -131,13 +138,8 @@ void towire_existing_htlc(u8 **pptr, const struct existing_htlc *existing)
131
138
} else
132
139
towire_bool (pptr , false);
133
140
if (existing -> extra_tlvs ) {
134
- u8 * tmp_pptr = tal_arr (tmpctx , u8 , 0 );
135
- towire_tlvstream_raw (& tmp_pptr , existing -> extra_tlvs );
136
-
137
141
towire_bool (pptr , true);
138
- towire_u16 (pptr , tal_bytelen (tmp_pptr ));
139
- towire_u8_array (pptr , tmp_pptr ,
140
- tal_bytelen (tmp_pptr ));
142
+ towire_len_and_tlvstream (pptr , existing -> extra_tlvs );
141
143
} else
142
144
towire_bool (pptr , false);
143
145
}
@@ -192,6 +194,28 @@ void towire_shachain(u8 **pptr, const struct shachain *shachain)
192
194
}
193
195
}
194
196
197
+ static struct tlv_field * fromwire_len_and_tlvstream (const tal_t * ctx ,
198
+ const u8 * * cursor , size_t * max )
199
+ {
200
+ struct tlv_field * tlvs = tal_arr (ctx , struct tlv_field , 0 );
201
+ size_t len = fromwire_u16 (cursor , max );
202
+
203
+ /* Subtle: we are not using fromwire_tal_arrn here, which
204
+ * would do this. */
205
+ if (len > * max ) {
206
+ fromwire_fail (cursor , max );
207
+ return NULL ;
208
+ }
209
+
210
+ /* NOTE: We might consider to be more strict and only allow for
211
+ * known tlv types from the tlvs_tlv_update_add_htlc_tlvs
212
+ * record. */
213
+ if (!fromwire_tlv (cursor , & len , NULL , 0 , cast_const (void * , ctx ),
214
+ & tlvs , FROMWIRE_TLV_ANY_TYPE , NULL , NULL ))
215
+ return tal_free (tlvs );
216
+ return tlvs ;
217
+ }
218
+
195
219
void fromwire_added_htlc (const u8 * * cursor , size_t * max ,
196
220
struct added_htlc * added )
197
221
{
@@ -207,17 +231,7 @@ void fromwire_added_htlc(const u8 **cursor, size_t *max,
207
231
} else
208
232
added -> path_key = NULL ;
209
233
if (fromwire_bool (cursor , max )) {
210
- size_t tlv_len = fromwire_u16 (cursor , max );
211
- /* NOTE: We might consider to be more strict and only allow for
212
- * known tlv types from the tlvs_tlv_update_add_htlc_tlvs
213
- * record. */
214
- const u64 * allowed = cast_const (u64 * , FROMWIRE_TLV_ANY_TYPE );
215
- added -> extra_tlvs = tal_arr (added , struct tlv_field , 0 );
216
- if (!fromwire_tlv (cursor , & tlv_len , NULL , 0 , added ,
217
- & added -> extra_tlvs , allowed , NULL , NULL )) {
218
- tal_free (added -> extra_tlvs );
219
- added -> extra_tlvs = NULL ;
220
- }
234
+ added -> extra_tlvs = fromwire_len_and_tlvstream (added , cursor , max );
221
235
} else
222
236
added -> extra_tlvs = NULL ;
223
237
added -> fail_immediate = fromwire_bool (cursor , max );
@@ -250,17 +264,7 @@ struct existing_htlc *fromwire_existing_htlc(const tal_t *ctx,
250
264
} else
251
265
existing -> path_key = NULL ;
252
266
if (fromwire_bool (cursor , max )) {
253
- size_t tlv_len = fromwire_u16 (cursor , max );
254
- /* NOTE: We might consider to be more strict and only allow for
255
- * known tlv types from the tlvs_tlv_update_add_htlc_tlvs
256
- * record. */
257
- const u64 * allowed = cast_const (u64 * , FROMWIRE_TLV_ANY_TYPE );
258
- existing -> extra_tlvs = tal_arr (existing , struct tlv_field , 0 );
259
- if (!fromwire_tlv (cursor , & tlv_len , NULL , 0 , existing ,
260
- & existing -> extra_tlvs , allowed , NULL , NULL )) {
261
- tal_free (existing -> extra_tlvs );
262
- existing -> extra_tlvs = NULL ;
263
- }
267
+ existing -> extra_tlvs = fromwire_len_and_tlvstream (existing , cursor , max );
264
268
} else
265
269
existing -> extra_tlvs = NULL ;
266
270
return existing ;
0 commit comments