Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Update lisk-codec to check int range during write
Browse files Browse the repository at this point in the history
  • Loading branch information
Phanco committed Sep 19, 2023
1 parent 3a95f36 commit ead2caa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions elements/lisk-codec/src/varint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@
/* eslint-disable no-bitwise */
/* eslint-disable no-param-reassign */

import { MAX_SINT32, MAX_SINT64, MAX_UINT32, MAX_UINT64 } from '@liskhq/lisk-validator';

const msg = 0x80;
const rest = 0x7f;

export const writeUInt32 = (value: number): Buffer => {
if (value > MAX_UINT32) {
throw new Error('Value out of range of uint32');
}

const result: number[] = [];
let index = 0;
while (value > rest) {
Expand All @@ -32,13 +38,21 @@ export const writeUInt32 = (value: number): Buffer => {
};

export const writeSInt32 = (value: number): Buffer => {
if (value > MAX_SINT32) {
throw new Error('Value out of range of sint32');
}

if (value >= 0) {
return writeUInt32(2 * value);
}
return writeUInt32(-2 * value - 1);
};

export const writeUInt64 = (value: bigint): Buffer => {
if (value > MAX_UINT64) {
throw new Error('Value out of range of uint64');
}

const result: number[] = [];
let index = 0;
while (value > BigInt(rest)) {
Expand All @@ -53,6 +67,10 @@ export const writeUInt64 = (value: bigint): Buffer => {
};

export const writeSInt64 = (value: bigint): Buffer => {
if (value > MAX_SINT64) {
throw new Error('Value out of range of sint64');
}

if (value >= BigInt(0)) {
return writeUInt64(BigInt(2) * value);
}
Expand Down
19 changes: 19 additions & 0 deletions elements/lisk-codec/test/varint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*
* Removal or modification of this copyright notice is prohibited.
*/

import { MAX_SINT32, MAX_SINT64, MAX_UINT32, MAX_UINT64 } from '@liskhq/lisk-validator';

import {
writeUInt32,
writeSInt32,
Expand All @@ -24,6 +27,22 @@ import {

describe('varint', () => {
describe('writer', () => {
it('should fail to encode uint32 when input is out of range', () => {
expect(() => writeUInt32(MAX_UINT32 + 1)).toThrow('Value out of range of uint32');
});

it('should fail to encode uint64 when input is out of range', () => {
expect(() => writeUInt64(MAX_UINT64 + BigInt(1))).toThrow('Value out of range of uint64');
});

it('should fail to encode sint32 when input is out of range', () => {
expect(() => writeSInt32(MAX_SINT32 + 1)).toThrow('Value out of range of sint32');
});

it('should fail to encode sint64 when input is out of range', () => {
expect(() => writeSInt64(MAX_SINT64 + BigInt(1))).toThrow('Value out of range of sint64');
});

it('should encode uint32', () => {
expect(writeUInt32(0)).toEqual(Buffer.from('00', 'hex'));
expect(writeUInt32(300)).toEqual(Buffer.from('ac02', 'hex'));
Expand Down

0 comments on commit ead2caa

Please sign in to comment.