Check buffer/skip size for undefined and NaN. #18

Open
wants to merge 1 commit into
from

Projects

None yet

1 participant

Currently, if you accidentally pass undefined or NaN as the size to buffer() or skip() you will encounter an obscure failure mode. The code will not fail immediately, but will instead cause all following word*() functions to store null for their values. This occurs because offset gets set to NaN which causes the buffer size check to fail.

This pull manually coerces the bad values to 0, matching the behavior when null is passed. Arguably it should throw instead.

If you're wondering how I encountered this, I was trying to do something like this:

var parser = binary.parse(buf);
var res = parser(buf)
    .word8('header.words')
    .buffer('header.data', 4 * parser.vars['header.words'])
    .word8('body.byteCount')
    .vars

This passed NaN because parser.vars['header.words'] should instead be parser.var['header']['words']. It was tricky to track down, though, because the immediate symptom was that 'body.byteCount' was set to null.

So first, I'm obviously an idiot and need to fix my code. It might be helpful, though, if we could make the failure mode either immediate or forgiving of bogus values.

Thoughts?

Thank you for your excellent work.

@wanderview wanderview Check buffer/skip size for undefined and NaN.
Manually coerce to 0 if found.  Previously the offset variable would be
corrupted to NaN causing all following word*() functions to mysteriously store
null.
6910679
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment