Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/cjs/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ function decode(buffer, offset) {
}
else if (first === 0xfd) {
const val = tools.readUInt16(buffer, offset + 1, 'LE');
if (val < 0xfd)
throw new Error('non-minimal encoding');
return {
numberValue: val,
bigintValue: BigInt(val),
Expand All @@ -97,6 +99,8 @@ function decode(buffer, offset) {
}
else if (first === 0xfe) {
const val = tools.readUInt32(buffer, offset + 1, 'LE');
if (val <= 0xffff)
throw new Error('non-minimal encoding');
return {
numberValue: val,
bigintValue: BigInt(val),
Expand All @@ -106,6 +110,8 @@ function decode(buffer, offset) {
}
else {
const number = tools.readUInt64(buffer, offset + 1, 'LE');
if (number <= 0xffffffffn)
throw new Error('non-minimal encoding');
return { numberValue: number <= Number.MAX_SAFE_INTEGER ? Number(number) : null, bigintValue: number, bytes: 9 };
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export function decode(buffer, offset) {
}
else if (first === 0xfd) {
const val = tools.readUInt16(buffer, offset + 1, 'LE');
if (val < 0xfd)
throw new Error('non-minimal encoding');
return {
numberValue: val,
bigintValue: BigInt(val),
Expand All @@ -70,6 +72,8 @@ export function decode(buffer, offset) {
}
else if (first === 0xfe) {
const val = tools.readUInt32(buffer, offset + 1, 'LE');
if (val <= 0xffff)
throw new Error('non-minimal encoding');
return {
numberValue: val,
bigintValue: BigInt(val),
Expand All @@ -79,6 +83,8 @@ export function decode(buffer, offset) {
}
else {
const number = tools.readUInt64(buffer, offset + 1, 'LE');
if (number <= 0xffffffffn)
throw new Error('non-minimal encoding');
return { numberValue: number <= Number.MAX_SAFE_INTEGER ? Number(number) : null, bigintValue: number, bytes: 9 };
}
}
Expand Down
13 changes: 13 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ tape("decode", function (t) {
t.end();
});

t.test("should reject a non-minimal encoding", function (t) {
t.throws(function () {
decode(Buffer.from("fd0100", "hex"));
}, new Error("non-minimal encoding"));
t.throws(function () {
decode(Buffer.from("fe01000000", "hex"));
}, new Error("non-minimal encoding"));
t.throws(function () {
decode(Buffer.from("ff0100000000000000", "hex"));
}, new Error("non-minimal encoding"));
t.end();
});

t.test("should return null if a number is invalid", function (t) {
var buffer = Buffer.alloc(18);
buffer.writeUIntBE(0xff, 0, 1);
Expand Down
3 changes: 3 additions & 0 deletions ts_src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export function decode (
// 16 bit
} else if (first === 0xfd) {
const val = tools.readUInt16(buffer, offset + 1, 'LE')
if (val < 0xfd) throw new Error('non-minimal encoding')
return {
numberValue: val,
bigintValue: BigInt(val),
Expand All @@ -86,6 +87,7 @@ export function decode (
// 32 bit
} else if (first === 0xfe) {
const val = tools.readUInt32(buffer, offset + 1, 'LE')
if (val <= 0xffff) throw new Error('non-minimal encoding')
return {
numberValue: val,
bigintValue: BigInt(val),
Expand All @@ -95,6 +97,7 @@ export function decode (
// 64 bit
} else {
const number = tools.readUInt64(buffer, offset + 1, 'LE')
if (number <= 0xffffffffn) throw new Error('non-minimal encoding')

return { numberValue: number <= Number.MAX_SAFE_INTEGER ? Number(number) : null, bigintValue: number, bytes: 9 }
}
Expand Down