@dao-xyz/borsh
Advanced tools
Comparing version 5.1.5 to 5.1.6
@@ -338,4 +338,7 @@ "use strict"; | ||
const len = reader.u32(); | ||
const end = reader._offset + len; | ||
if (end > reader._buf.length) { | ||
throw new error_js_1.BorshError("Error decoding UTF-8 string: Invalid length"); | ||
} | ||
try { | ||
const end = reader._offset + len; | ||
const string = utf8_1.default.read(reader._buf, reader._offset, end); | ||
@@ -352,2 +355,5 @@ reader._offset = end; | ||
const end = reader._offset + len; | ||
if (end > reader._buf.length) { | ||
throw new error_js_1.BorshError("Error decoding UTF-8 string: Invalid length"); | ||
} | ||
const string = reader._buf.toString(undefined, reader._offset, end); | ||
@@ -359,4 +365,7 @@ reader._offset = end; | ||
const len = length(reader); | ||
const end = reader._offset + len; | ||
if (end > reader._buf.length) { | ||
throw new error_js_1.BorshError("Error decoding UTF-8 string: Invalid length"); | ||
} | ||
try { | ||
const end = reader._offset + len; | ||
const string = reader._buf.toString(undefined, reader._offset, end); | ||
@@ -372,4 +381,7 @@ reader._offset = end; | ||
const len = length(reader); | ||
const end = reader._offset + len; | ||
if (end > reader._buf.length) { | ||
throw new error_js_1.BorshError("Error decoding UTF-8 string: Invalid length"); | ||
} | ||
try { | ||
const end = reader._offset + len; | ||
const string = utf8_1.default.read(reader._buf, reader._offset, end); | ||
@@ -376,0 +388,0 @@ reader._offset = end; |
@@ -331,4 +331,7 @@ import { toBigIntLE, writeBufferLEBigInt, writeUInt32LE, readUInt32LE, readUInt16LE, writeUInt16LE, readBigUInt64LE, readUIntLE, checkInt, writeBigUint64Le } from './bigint.js'; | ||
const len = reader.u32(); | ||
const end = reader._offset + len; | ||
if (end > reader._buf.length) { | ||
throw new BorshError("Error decoding UTF-8 string: Invalid length"); | ||
} | ||
try { | ||
const end = reader._offset + len; | ||
const string = utf8.read(reader._buf, reader._offset, end); | ||
@@ -345,2 +348,5 @@ reader._offset = end; | ||
const end = reader._offset + len; | ||
if (end > reader._buf.length) { | ||
throw new BorshError("Error decoding UTF-8 string: Invalid length"); | ||
} | ||
const string = reader._buf.toString(undefined, reader._offset, end); | ||
@@ -352,4 +358,7 @@ reader._offset = end; | ||
const len = length(reader); | ||
const end = reader._offset + len; | ||
if (end > reader._buf.length) { | ||
throw new BorshError("Error decoding UTF-8 string: Invalid length"); | ||
} | ||
try { | ||
const end = reader._offset + len; | ||
const string = reader._buf.toString(undefined, reader._offset, end); | ||
@@ -365,4 +374,7 @@ reader._offset = end; | ||
const len = length(reader); | ||
const end = reader._offset + len; | ||
if (end > reader._buf.length) { | ||
throw new BorshError("Error decoding UTF-8 string: Invalid length"); | ||
} | ||
try { | ||
const end = reader._offset + len; | ||
const string = utf8.read(reader._buf, reader._offset, end); | ||
@@ -369,0 +381,0 @@ reader._offset = end; |
{ | ||
"name": "@dao-xyz/borsh", | ||
"version": "5.1.5", | ||
"version": "5.1.6", | ||
"readme": "README.md", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/dao-xyz/borsh-ts#README", |
@@ -1478,2 +1478,65 @@ import { BinaryReader, BinaryWriter } from "../binary.js"; | ||
test("large string", () => { | ||
let first = Buffer.from(crypto.randomBytes(10000)).toString("hex"); | ||
const bufSome = serialize(new TestStruct(first, 123, "that ends")); | ||
const deserializedSome = deserialize(bufSome, TestStruct); | ||
const deserializedSomeFromUint8Array = deserialize( | ||
new Uint8Array(bufSome), | ||
TestStruct | ||
); | ||
expect(deserializedSome).toMatchObject(deserializedSomeFromUint8Array); | ||
expect(deserializedSome.a).toEqual(first); | ||
expect(deserializedSome.b).toEqual(123); | ||
expect(deserializedSome.c).toEqual("that ends"); | ||
}); | ||
test("uint8array overflow will throw error", () => { | ||
// length 2 in u32 and string represented in length 1 (not ok) | ||
expect(() => | ||
new BinaryReader(new Uint8Array([2, 0, 0, 0, 0])).string() | ||
).toThrowError( | ||
new BorshError("Error decoding UTF-8 string: Invalid length") | ||
); | ||
// length 1 in u32 and tring represented in length 1 (ok) | ||
new BinaryReader(new Uint8Array([1, 0, 0, 0, 0])).string(); | ||
}); | ||
test("buffer overflow will throw error", () => { | ||
// length 2 in u32 and string represented in length 1 (not ok) | ||
expect(() => | ||
BinaryReader.bufferString(new BinaryReader(Buffer.from([2, 0, 0, 0, 0]))) | ||
).toThrowError( | ||
new BorshError("Error decoding UTF-8 string: Invalid length") | ||
); | ||
// length 1 in u32 and tring represented in length 1 (ok) | ||
BinaryReader.bufferString(new BinaryReader(Buffer.from([1, 0, 0, 0, 0]))); | ||
}); | ||
test("custom uint8array overflow will throw error", () => { | ||
// length 2 in u32 and string represented in length 1 (not ok) | ||
expect(() => | ||
BinaryReader.stringCustom(new BinaryReader(new Uint8Array([0])), () => 2) | ||
).toThrowError( | ||
new BorshError("Error decoding UTF-8 string: Invalid length") | ||
); | ||
// length 1 in u32 and tring represented in length 1 (ok) | ||
BinaryReader.stringCustom(new BinaryReader(new Uint8Array([0])), () => 1); | ||
}); | ||
test("custom buffer overflow will throw error", () => { | ||
// length 2 in u32 and string represented in length 1 (not ok) | ||
expect(() => | ||
BinaryReader.stringCustom(new BinaryReader(Buffer.from([0])), () => 2) | ||
).toThrowError( | ||
new BorshError("Error decoding UTF-8 string: Invalid length") | ||
); | ||
// length 1 in u32 and tring represented in length 1 (ok) | ||
BinaryReader.stringCustom(new BinaryReader(Buffer.from([0])), () => 1); | ||
}); | ||
test("custom length", () => { | ||
@@ -1480,0 +1543,0 @@ class TestStructCustom { |
@@ -422,4 +422,8 @@ import { toBigIntLE, writeBufferLEBigInt, writeUInt32LE, readUInt32LE, readUInt16LE, writeUInt16LE, readBigUInt64LE, readUIntLE, checkInt, writeBigUint64Le } from './bigint.js'; | ||
const len = reader.u32(); | ||
const end = reader._offset + len; | ||
if (end > reader._buf.length) { | ||
throw new BorshError("Error decoding UTF-8 string: Invalid length") | ||
} | ||
try { | ||
const end = reader._offset + len; | ||
const string = utf8.read(reader._buf, reader._offset, end); | ||
@@ -436,2 +440,6 @@ reader._offset = end; | ||
const end = reader._offset + len; | ||
if (end > reader._buf.length) { | ||
throw new BorshError("Error decoding UTF-8 string: Invalid length") | ||
} | ||
const string = (reader._buf as Buffer).toString(undefined, reader._offset, end); | ||
@@ -445,4 +453,9 @@ reader._offset = end; | ||
const len = length(reader); | ||
const end = reader._offset + len; | ||
if (end > reader._buf.length) { | ||
throw new BorshError("Error decoding UTF-8 string: Invalid length") | ||
} | ||
try { | ||
const end = reader._offset + len; | ||
const string = (reader._buf as Buffer).toString(undefined, reader._offset, end); | ||
@@ -458,4 +471,8 @@ reader._offset = end; | ||
const len = length(reader); | ||
const end = reader._offset + len; | ||
if (end > reader._buf.length) { | ||
throw new BorshError("Error decoding UTF-8 string: Invalid length") | ||
} | ||
try { | ||
const end = reader._offset + len; | ||
const string = utf8.read(reader._buf, reader._offset, end); | ||
@@ -462,0 +479,0 @@ reader._offset = end; |
@@ -474,3 +474,3 @@ import { | ||
let agg: number[] = []; | ||
for (let i = 0; i < variantType; i++) { | ||
for (let i = 0; i < (variantType as number); i++) { | ||
agg.push(reader.u8()) | ||
@@ -558,3 +558,3 @@ } | ||
const getSuperMostClass = (clazz: Constructor<any>) => { | ||
const getSuperMostClass = (clazz: AbstractType<any>) => { | ||
while (Object.getPrototypeOf(clazz).prototype != undefined) { | ||
@@ -754,3 +754,3 @@ clazz = Object.getPrototypeOf(clazz); | ||
const validateIterator = (clazzes: Constructor<any> | Constructor<any>[], allowUndefined: boolean, visited: Set<string>) => { | ||
const validateIterator = (clazzes: AbstractType<any> | AbstractType<any>[], allowUndefined: boolean, visited: Set<string>) => { | ||
clazzes = Array.isArray(clazzes) ? clazzes : [clazzes]; | ||
@@ -757,0 +757,0 @@ let schemas = new Map<any, StructKind>(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
359372
6633