Browse Source

bitcoin/varint: fix varint reading for multibyte varints.

Embarrassing error.

Reported-by: throckmorton on #lightning-dev
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
78174a09e2
  1. 14
      bitcoin/varint.c

14
bitcoin/varint.c

@ -39,21 +39,21 @@ size_t varint_get(const u8 *p, size_t max, varint_t *val)
case 0xfd: case 0xfd:
if (max < 3) if (max < 3)
return 0; return 0;
*val = ((u64)p[1] << 8) + p[0]; *val = ((u64)p[2] << 8) + p[1];
return 3; return 3;
case 0xfe: case 0xfe:
if (max < 5) if (max < 5)
return 0; return 0;
*val = ((u64)p[3] << 24) + ((u64)p[2] << 16) *val = ((u64)p[4] << 24) + ((u64)p[3] << 16)
+ ((u64)p[1] << 8) + p[0]; + ((u64)p[2] << 8) + p[1];
return 5; return 5;
case 0xff: case 0xff:
if (max < 9) if (max < 9)
return 0; return 0;
*val = ((u64)p[7] << 56) + ((u64)p[6] << 48) *val = ((u64)p[8] << 56) + ((u64)p[7] << 48)
+ ((u64)p[5] << 40) + ((u64)p[4] << 32) + ((u64)p[6] << 40) + ((u64)p[5] << 32)
+ ((u64)p[3] << 24) + ((u64)p[2] << 16) + ((u64)p[4] << 24) + ((u64)p[3] << 16)
+ ((u64)p[1] << 8) + p[0]; + ((u64)p[2] << 8) + p[1];
return 9; return 9;
default: default:
*val = *p; *val = *p;

Loading…
Cancel
Save