substack/node-binary — pull request #18 by wanderview (2013-01-25 04:49:20+00:00)
Title: Check buffer/skip size for undefined and NaN.

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:

``` javascript
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.