Skip to content

Commit 39ac5a8

Browse files
author
Chandra Pratap
committed
common/test: Add a test to trigger the undefined behaviour
Add a test that reproduces the UB when the fix is not applied.
1 parent aba798d commit 39ac5a8

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

common/test/run-tlv_span.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ int main(int argc, char *argv[])
131131
len = tlv_span(wire, 0, 1, &start);
132132
assert(start == 0);
133133
assert(len == strlen("0010b8538094dbd70d8a0f0439d8e64f766f") / 2);
134+
135+
/* Simulate an empty tlvstream */
136+
wire = tal_arr(tmpctx, u8, 0);
137+
len = tlv_span(wire, 0, UINT64_MAX, &start);
138+
assert(start == 0);
139+
assert(len == 0);
140+
141+
/* Simulate a TLV stream where the payload is shorter
142+
* than its length field indicates.
143+
*/
144+
wire = tal_hexdata(tmpctx, "0502beef0a03de", strlen("0502beef0a03de"));
145+
len = tlv_span(wire, 0, UINT64_MAX, &start);
146+
assert(start == 0);
147+
/* The span should cover only the first valid record, which is 4 bytes long. */
148+
assert(len == 4);
149+
134150
common_shutdown();
135151
return 0;
136152
}

0 commit comments

Comments
 (0)