diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index ef83bd9c1..7143ddc72 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -793,7 +793,8 @@ static void htlc_accepted_hook_serialize(struct htlc_accepted_hook_payload *p, * correctly. */ static bool htlc_accepted_can_continue(struct route_step *rs) { - if (rs->type == SPHINX_TLV_PAYLOAD && !tlv_payload_is_valid(rs->payload.tlv)) { + if (rs->type == SPHINX_TLV_PAYLOAD && + !tlv_payload_is_valid(rs->payload.tlv, NULL)) { SUPERVERBOSE("Encoding of TLV payload is invalid"); return false; } diff --git a/tools/gen/header_template b/tools/gen/header_template index 2cd7ad50c..2c7f0cf26 100644 --- a/tools/gen/header_template +++ b/tools/gen/header_template @@ -75,10 +75,12 @@ bool fromwire_${tlv.name}(const u8 **cursor, size_t *max, struct ${tlv.struct_na * - Types must be in monotonic non-repeating order * - We must understand all even types * - * Returns the index of the field that was invalid, or -1 if the stream is - * valid. + * Returns false if an error was detected, otherwise returns true. If err_index + * is non-null and we detect an error it is set to the index of the first error + * detected. */ -int ${tlv.name}_is_valid(const struct ${tlv.struct_name()} *record); +bool ${tlv.name}_is_valid(const struct ${tlv.struct_name()} *record, + size_t *err_index); % if tlv.name in options.expose_tlv_type: #define TLVS_${tlv.name.upper()}_ARRAY_SIZE ${len(tlv.messages)} diff --git a/tools/gen/impl_template b/tools/gen/impl_template index 950a54673..197692e7c 100644 --- a/tools/gen/impl_template +++ b/tools/gen/impl_template @@ -306,7 +306,7 @@ fail: return false; } -int ${tlv.name}_is_valid(const struct ${tlv.struct_name()} *record) +bool ${tlv.name}_is_valid(const struct ${tlv.struct_name()} *record, size_t *err_index) { size_t numfields = tal_count(record->fields); bool first = true; @@ -321,8 +321,10 @@ int ${tlv.name}_is_valid(const struct ${tlv.struct_name()} *record) * - otherwise, if `type` is odd: * - MUST discard the next `length` bytes. */ - SUPERVERBOSE("Unknown even type in TLV"); - return i; + SUPERVERBOSE("unknown even"); + if (err_index != NULL) + *err_index = i; + return false; } else if (!first && f->numtype <= prev_type) { /* BOLT #1: * - if decoded `type`s are not monotonically-increasing: @@ -332,12 +334,14 @@ int ${tlv.name}_is_valid(const struct ${tlv.struct_name()} *record) SUPERVERBOSE("duplicate tlv type"); else SUPERVERBOSE("invalid ordering"); - return i; + if (err_index != NULL) + *err_index = i; + return false; } first = false; prev_type = f->numtype; } - return -1; + return true; } % endfor ## END TLV's diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 4a162a8c5..8dad28495 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -546,7 +546,8 @@ void subd_req_(const tal_t *ctx UNNEEDED, void subd_send_msg(struct subd *sd UNNEEDED, const u8 *msg_out UNNEEDED) { fprintf(stderr, "subd_send_msg called!\n"); abort(); } /* Generated stub for tlv_payload_is_valid */ -int tlv_payload_is_valid(const struct tlv_tlv_payload *record UNNEEDED) +bool tlv_payload_is_valid(const struct tlv_tlv_payload *record UNNEEDED, + size_t *err_index UNNEEDED) { fprintf(stderr, "tlv_payload_is_valid called!\n"); abort(); } /* Generated stub for topology_add_sync_waiter_ */ void topology_add_sync_waiter_(const tal_t *ctx UNNEEDED,