wire: rename var_int to bigsize, and insist on minimal.

The new TLV spec uses BigSize, like Bitcoin's CompactInt but
*little-endian*.  So change our name for clarity, and insist that
decoding be minimal as the spec requires.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2019-07-18 14:35:55 +09:30
parent 868bde5e00
commit 54790c17ea
4 changed files with 24 additions and 13 deletions

View File

@ -1057,7 +1057,7 @@ tlv__type_impl_fromwire_template = """static struct {tlv_name} *fromwire__{tlv_n
\twhile (*plen) {{
\t\tmsg_type = fromwire_u8(p, plen);
\t\tmsg_len = fromwire_var_int(p, plen);
\t\tmsg_len = fromwire_bigsize(p, plen);
\t\tif (*plen < msg_len) {{
\t\t\tfromwire_fail(p, plen);
\t\t\tbreak;
@ -1103,7 +1103,7 @@ print_tlv_template = """static void printwire_{tlv_name}(const char *fieldname,
\twhile (cursor) {{
\t\tmsg_type = fromwire_u8(&cursor, &plen);
\t\tmsg_size = fromwire_var_int(&cursor, &plen);
\t\tmsg_size = fromwire_bigsize(&cursor, &plen);
\t\tif (!cursor)
\t\t\tbreak;
\t\tswitch ((enum {tlv_name}_type)msg_type) {{

View File

@ -102,20 +102,31 @@ bool fromwire_bool(const u8 **cursor, size_t *max)
return ret;
}
u64 fromwire_var_int(const u8 **cursor, size_t *max)
u64 fromwire_bigsize(const u8 **cursor, size_t *max)
{
u8 flag = fromwire_u8(cursor, max);
u64 ret;
switch(flag) {
case 0xff:
return fromwire_u64(cursor, max);
ret = fromwire_u64(cursor, max);
if ((ret >> 32) == 0)
fromwire_fail(cursor, max);
break;
case 0xfe:
return (u64)fromwire_u32(cursor, max);
ret = fromwire_u32(cursor, max);
if ((ret >> 16) == 0)
fromwire_fail(cursor, max);
break;
case 0xfd:
return (u64)fromwire_u16(cursor, max);
ret = fromwire_u16(cursor, max);
if (ret < 0xfd)
fromwire_fail(cursor, max);
break;
default:
return (u64)flag;
ret = flag;
}
return ret;
}
void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey)

View File

@ -55,16 +55,16 @@ void towire_bool(u8 **pptr, bool v)
towire(pptr, &val, sizeof(val));
}
void towire_var_int(u8 **pptr, const u64 val)
void towire_bigsize(u8 **pptr, const u64 val)
{
if (val < 0xfd) {
towire_u8(pptr, (u8)val);
towire_u8(pptr, val);
} else if (val <= 0xffff) {
towire_u8(pptr, 0xfd);
towire_u16(pptr, (u16)val);
towire_u16(pptr, val);
} else if (val <= 0xffffffff) {
towire_u8(pptr, 0xfe);
towire_u32(pptr, (u32)val);
towire_u32(pptr, val);
} else {
towire_u8(pptr, 0xff);
towire_u64(pptr, val);

View File

@ -69,7 +69,7 @@ void towire_u64(u8 **pptr, u64 v);
void towire_double(u8 **pptr, const double *v);
void towire_pad(u8 **pptr, size_t num);
void towire_bool(u8 **pptr, bool v);
void towire_var_int(u8 **pptr, const u64 val);
void towire_bigsize(u8 **pptr, const u64 val);
void towire_u8_array(u8 **pptr, const u8 *arr, size_t num);
@ -86,7 +86,7 @@ u32 fromwire_u32(const u8 **cursor, size_t *max);
u64 fromwire_u64(const u8 **cursor, size_t *max);
void fromwire_double(const u8 **cursor, size_t *max, double *v);
bool fromwire_bool(const u8 **cursor, size_t *max);
u64 fromwire_var_int(const u8 **cursor, size_t *max);
u64 fromwire_bigsize(const u8 **cursor, size_t *max);
void fromwire_secret(const u8 **cursor, size_t *max, struct secret *secret);
void fromwire_privkey(const u8 **cursor, size_t *max, struct privkey *privkey);
void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey);