Skip to content

Commit 6910679

Browse files
committed
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.
1 parent 21a98b1 commit 6910679

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

index.js

+8
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ exports.stream = function (input) {
131131
self.buffer = function (name, bytes) {
132132
if (typeof bytes === 'string') {
133133
bytes = vars.get(bytes);
134+
} else if (bytes === undefined || bytes !== bytes) {
135+
bytes = 0;
134136
}
135137

136138
getBytes(bytes, function (buf) {
@@ -142,6 +144,8 @@ exports.stream = function (input) {
142144
self.skip = function (bytes) {
143145
if (typeof bytes === 'string') {
144146
bytes = vars.get(bytes);
147+
} else if (bytes === undefined || bytes !== bytes) {
148+
bytes = 0;
145149
}
146150

147151
getBytes(bytes, function () {
@@ -272,6 +276,8 @@ exports.parse = function parse (buffer) {
272276
self.buffer = function (name, size) {
273277
if (typeof size === 'string') {
274278
size = vars.get(size);
279+
} else if (size === undefined || size !== size) {
280+
size = 0;
275281
}
276282
var buf = buffer.slice(offset, Math.min(buffer.length, offset + size));
277283
offset += size;
@@ -283,6 +289,8 @@ exports.parse = function parse (buffer) {
283289
self.skip = function (bytes) {
284290
if (typeof bytes === 'string') {
285291
bytes = vars.get(bytes);
292+
} else if (bytes === undefined || bytes !== bytes) {
293+
bytes = 0;
286294
}
287295
offset += bytes;
288296

test/parse.js

+14
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,17 @@ test('loop', function (t) {
5252
y : 9,
5353
});
5454
});
55+
56+
test('NaN', function(t) {
57+
t.plan(3);
58+
var res = binary.parse(new Buffer([ 106, 35 ]))
59+
.buffer('a', NaN)
60+
.word8('b')
61+
.skip(NaN)
62+
.word8('c')
63+
.vars
64+
;
65+
t.equal(res.a.length, 0);
66+
t.equal(res.b, 106);
67+
t.equal(res.c, 35);
68+
});

0 commit comments

Comments
 (0)