Skip to content

Commit ef96201

Browse files
committed
fix(javascript): clean up failed input binding
1 parent b2ab955 commit ef96201

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

javascript/packages/core/lib/fory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ export default class Fory {
179179

180180
deserialize<T = any>(bytes: Uint8Array, serializer: Serializer = this.anySerializer): T | null {
181181
this.registrationFrozen = true;
182-
this.readContext.reset(bytes);
183182
try {
183+
this.readContext.reset(bytes);
184184
const reader = this.readContext.reader;
185185
const bitmap = reader.readUint8();
186186
if (bitmap !== ConfigFlags.isCrossLanguageFlag) {
@@ -243,8 +243,8 @@ export default class Fory {
243243
const rootHeader = ConfigFlags.isCrossLanguageFlag;
244244
rootDeserializer = (bytes: Uint8Array) => {
245245
this.registrationFrozen = true;
246-
readContext.reset(bytes);
247246
try {
247+
readContext.reset(bytes);
248248
const bitmap = reader.readUint8();
249249
if (bitmap !== rootHeader) {
250250
this.throwInvalidRootHeader(bitmap);

javascript/test/rootCleanup.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,30 @@ describe.each([
9191

9292
expect(readContext.typeMetaCache.get(headerHash)).toBe(typeMeta);
9393
});
94+
95+
test("clears retained state when input binding fails", () => {
96+
const fory = new Fory({ compatible: true, ref: true });
97+
const registered = fory.register(Type.struct(7615, {}));
98+
const readContext = (fory as any).readContext;
99+
const typeMeta = TypeMeta.fromTypeInfo(Type.struct(7616, {}));
100+
101+
registered.serializer.readRef = () => {
102+
readContext.refReader.reference({});
103+
populateLogicalTables(readContext, typeMeta);
104+
return 7;
105+
};
106+
expect(invoke(fory, registered, new Uint8Array([1]))).toBe(7);
107+
108+
const invalidInput = new Uint8Array([1]);
109+
Object.defineProperty(invalidInput, "buffer", {
110+
get() {
111+
throw new Error("input binding failed");
112+
},
113+
});
114+
expect(() => invoke(fory, registered, invalidInput)).toThrow("input binding failed");
115+
expect(readContext.reader.platformBuffer).toHaveLength(0);
116+
expectRootStateCleared(readContext);
117+
});
94118
});
95119

96120
test("restores root write state after failure", () => {

0 commit comments

Comments
 (0)