Skip to content

Commit

Permalink
End of Buffer Error: Part Two
Browse files Browse the repository at this point in the history
Did the testing to see if I can put the error handling when the offset is set to an il-formatted value, and that doesn't appear to work. This is the demonstration I used to test that, and it started throwing `DataView` errors again, meaning that it can't catch it in that order. Thinking of it now, that makes sense. I think the only reason that kind of worked before, was because in the instance of the `#readString()` function, `Uint8Array.subarray()` will simply return a shorter-length view of the remaining data, rather than erroring-out and saying that the offset is outside the bounds, unline the `DataView.get*()` functions, since those *will* error out if the data can't be read at that offset. So, I may rework the wording or the code formatting, but I think it has to catch the offset issues in this order for it to work. Nice find!

*Edit: Try this out! This is what I was mentioning about what was happening for the `#readString()` function. Glad to have also found this!
```js
new Uint8Array(10).subarray(0,24)
```
  • Loading branch information
Offroaders123 committed Dec 12, 2022
1 parent ca05acd commit c84cb8a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ export class NBTReader {
}
const value = decoder.decode(this.#data.subarray(this.#byteOffset,this.#byteOffset + length));
this.#byteOffset += length;
console.log(value);
return value;
}

Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const result = await NBT.read(data);
console.log(result,"\n");

const result2 = await NBT.write(result,{ bedrockLevel: null })
.then(buffer => NBT.read(buffer,{ endian: "big" }));
.then(buffer => NBT.read(buffer.slice(0,-1),{ endian: "little" }));
console.log(result2,"\n");

const recompile = Buffer.from(await NBT.write(result2));
Expand Down

0 comments on commit c84cb8a

Please sign in to comment.