wire: add explicit-length fromwire_peektype variant.
What changed, and why it matters
This commit adds a new helper function for reading message types from raw byte buffers where the caller already knows the buffer length. The existing helper required a special memory-managed pointer; the new variant works with plain length-counted buffers, which is useful for streaming data. There is no security-relevant change here—just a small, clean API addition.
No action required. This is a routine API enhancement with no security implications.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces fromwire_peektypen(cursor, max) and refactors fromwire_peektype(cursor) to call it with tal_count(cursor). The new variant lets callers peek at a 16-bit big-endian message type without requiring the buffer to be a tal-allocated array. The underlying read logic is unchanged; only the length source differs. No bounds-checking behavior changes, no callers are modified, and no bug is fixed.
Changed components
wire/fromwire.cwire/wire.hInspect captured patch +8 / −2
diff --git a/wire/fromwire.c b/wire/fromwire.c
index 8e81c815..9fded589 100644
--- a/wire/fromwire.c
+++ b/wire/fromwire.c
@@ -41,10 +41,9 @@ const u8 *fromwire(const u8 **cursor, size_t *max, void *copy, size_t n)
return memcheck(p, n);
}
-int fromwire_peektype(const u8 *cursor)
+int fromwire_peektypen(const u8 *cursor, size_t max)
{
be16 be_type;
- size_t max = tal_count(cursor);
fromwire(&cursor, &max, &be_type, sizeof(be_type));
if (!cursor)
@@ -52,6 +51,11 @@ int fromwire_peektype(const u8 *cursor)
return be16_to_cpu(be_type);
}
+int fromwire_peektype(const u8 *cursor)
+{
+ return fromwire_peektypen(cursor, tal_count(cursor));
+}
+
u8 fromwire_u8(const u8 **cursor, size_t *max)
{
u8 ret;
diff --git a/wire/wire.h b/wire/wire.h
index 8e8f12ac..da3858cc 100644
--- a/wire/wire.h
+++ b/wire/wire.h
@@ -15,6 +15,8 @@ typedef char utf8;
/* Read the type; returns -1 if not long enough. cursor is a tal ptr. */
int fromwire_peektype(const u8 *cursor);
+/* Same, but doesn't need to be a tal ptr */
+int fromwire_peektypen(const u8 *cursor, size_t len);
void *fromwire_fail(const u8 **cursor, size_t *max);
void towire(u8 **pptr, const void *data, size_t len);
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.