Skip to content

Commit da86365

Browse files
committed
add backtracking to opt decoding
1 parent d79dc98 commit da86365

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/candid/src/idl.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,16 @@ export class OptClass<T> extends ConstructType<[T] | []> {
922922
case 0:
923923
return [];
924924
case 1:
925-
return [this._type.decodeValue(b, opt._type)];
925+
let checkpoint = b.save();
926+
try {
927+
let v = this._type.decodeValue(b, opt._type)
928+
return [v];
929+
} catch (e : any) {
930+
b.restore(checkpoint);
931+
// skip value at wire typ (to advance b)
932+
let v = opt._type.decodeValue(b, opt._type)
933+
return [];
934+
};
926935
default:
927936
throw new Error('Not an option value');
928937
}

packages/candid/src/utils/buffer.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ export class PipeArrayBuffer {
3838
*/
3939
private _view: Uint8Array;
4040

41+
/**
42+
* Save a checkpoint of the reading view (for backtracking)
43+
*/
44+
public save() : Uint8Array {
45+
return this._view;
46+
};
47+
48+
/**
49+
* Restore a checkpoint of the reading view (for backtracking)
50+
*/
51+
public restore(checkPoint: Uint8Array) {
52+
this._view = checkPoint;
53+
};
54+
4155
/**
4256
* The actual buffer containing the bytes.
4357
* @private

0 commit comments

Comments
 (0)