Skip to content

Commit

Permalink
Add Optional skipChecks to Disable Array Size Check (#154)
Browse files Browse the repository at this point in the history
* feat: add optional `skipChecks` compiler option

* fix: add `options` to `ReadCompiler`

* docs: describe `skipChecks` for `ProtoDefCompiler`

* improve: add `skipChecks` to type file

* improve: add compiler constructor types

* feat: use protodef variables to skip checks

* revert: changes

* test: attempt to read variable

* fix: add skip checks condition

* improve: skip checks variable name

* chore: update  docs
  • Loading branch information
bdkopen authored Oct 31, 2024
1 parent 55e6c63 commit 1173604
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions doc/compiler.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,27 @@ Example:
}]
}
```
### Skip Checks (optional)
ProtoDef Compiler allows an optional `noArraySizeCheck` to be set. By default this value is `false`.
If set to `true`, the compiler will skip array checks that appliy safety limits to avoid out of memory crashes. Sometimes these checks can be too restrictive, and the `noArraySizeCheck` parameter allows you to disable them.
```javascript
const { ProtoDefCompiler } = require('protodef').Compiler

// Create a ProtoDefCompiler instance
const compiler = new ProtoDefCompiler()
compiler.addTypesToCompile(require('./protocol.json'))

// Compile a ProtoDef instance
const compiledProto = await compiler.compileProtoDef()

// Set the `noArraySizeCheck` variable to skip array checks.
compiledProto.setVariable('noArraySizeCheck', true);

// Use it as if it were a normal ProtoDef
const buffer = compiledProto.createPacketBuffer('mainType', result)
const result = compiledProto.parsePacketBuffer('mainType', buffer)
```
2 changes: 1 addition & 1 deletion src/datatypes/compiler-structures.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
} else {
throw new Error('Array must contain either count or countType')
}
code += 'if (count > 0xffffff) throw new Error("array size is abnormally large, not reading: " + count)\n'
code += 'if (count > 0xffffff && !ctx.noArraySizeCheck) throw new Error("array size is abnormally large, not reading: " + count)\n'
code += 'const data = []\n'
code += 'let size = countSize\n'
code += 'for (let i = 0; i < count; i++) {\n'
Expand Down

0 comments on commit 1173604

Please sign in to comment.