Skip to content

Commit 1173604

Browse files
authored
Add Optional skipChecks to Disable Array Size Check (#154)
* 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
1 parent 55e6c63 commit 1173604

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

doc/compiler.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,27 @@ Example:
205205
}]
206206
}
207207
```
208+
209+
### Skip Checks (optional)
210+
211+
ProtoDef Compiler allows an optional `noArraySizeCheck` to be set. By default this value is `false`.
212+
213+
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.
214+
215+
```javascript
216+
const { ProtoDefCompiler } = require('protodef').Compiler
217+
218+
// Create a ProtoDefCompiler instance
219+
const compiler = new ProtoDefCompiler()
220+
compiler.addTypesToCompile(require('./protocol.json'))
221+
222+
// Compile a ProtoDef instance
223+
const compiledProto = await compiler.compileProtoDef()
224+
225+
// Set the `noArraySizeCheck` variable to skip array checks.
226+
compiledProto.setVariable('noArraySizeCheck', true);
227+
228+
// Use it as if it were a normal ProtoDef
229+
const buffer = compiledProto.createPacketBuffer('mainType', result)
230+
const result = compiledProto.parsePacketBuffer('mainType', buffer)
231+
```

src/datatypes/compiler-structures.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
} else {
1111
throw new Error('Array must contain either count or countType')
1212
}
13-
code += 'if (count > 0xffffff) throw new Error("array size is abnormally large, not reading: " + count)\n'
13+
code += 'if (count > 0xffffff && !ctx.noArraySizeCheck) throw new Error("array size is abnormally large, not reading: " + count)\n'
1414
code += 'const data = []\n'
1515
code += 'let size = countSize\n'
1616
code += 'for (let i = 0; i < count; i++) {\n'

0 commit comments

Comments
 (0)