From bf64d5060745b6c54107017ab18157dc1fb43b3a Mon Sep 17 00:00:00 2001 From: Anton Trunov Date: Mon, 7 Oct 2024 21:14:20 +0400 Subject: [PATCH] fix: always pop current element in readStringOpt and readBufferOpt In the case of null elements, the current stack element wasn't popped which caused incorrect processing of the subsequent tuple elements. --- src/tuple/reader.ts | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/tuple/reader.ts b/src/tuple/reader.ts index f2768c3..57f859b 100644 --- a/src/tuple/reader.ts +++ b/src/tuple/reader.ts @@ -185,18 +185,16 @@ export class TupleReader { } readBufferOpt() { - let popped = this.peek(); - if (popped.type === 'null') { + let r = this.readCellOpt(); + if (r !== null) { + let s = r.beginParse(); + if (s.remainingRefs !== 0 || s.remainingBits % 8 !== 0) { + throw Error('Not a buffer'); + } + return s.loadBuffer(s.remainingBits / 8); + } else { return null; - } - let s = this.readCell().beginParse(); - if (s.remainingRefs !== 0) { - throw Error('Not a buffer'); - } - if (s.remainingBits % 8 !== 0) { - throw Error('Not a buffer'); - } - return s.loadBuffer(s.remainingBits / 8); + } } readString() { @@ -205,11 +203,13 @@ export class TupleReader { } readStringOpt() { - let popped = this.peek(); - if (popped.type === 'null') { + let r = this.readCellOpt(); + if (r !== null) { + let s = r.beginParse(); + return s.loadStringTail(); + } else { return null; } - let s = this.readCell().beginParse(); - return s.loadStringTail(); } -} \ No newline at end of file +} +