Skip to content

Commit

Permalink
fix: Fix invariant assertion in binary-codec (#2429)
Browse files Browse the repository at this point in the history
* Fix assertion

* Add a test
  • Loading branch information
JST5000 authored Aug 9, 2023
1 parent 958c97b commit bcc9057
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/ripple-binary-codec/src/serdes/binary-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class BinaryParser {
* @return The number represented by those bytes
*/
readUIntN(n: number): number {
if (0 >= n && n > 4) {
if (0 >= n || n > 4) {
throw new Error('invalid n')
}
return this.read(n).reduce((a, b) => (a << 8) | b) >>> 0
Expand Down
6 changes: 6 additions & 0 deletions packages/ripple-binary-codec/test/binary-parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ function fieldParsingTests() {
new Error('Cannot read FieldOrdinal, type_code out of range'),
)
})
test('readUIntN', () => {
const parser = makeParser('0009')
expect(parser.readUIntN(2)).toEqual(9)
expect(() => parser.readUIntN(-1)).toThrow(new Error('invalid n'))
expect(() => parser.readUIntN(5)).toThrow(new Error('invalid n'))
})
}

function assertRecyclable(json, forField) {
Expand Down

0 comments on commit bcc9057

Please sign in to comment.