Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

parsing an empty length encoded array correctly

  • Loading branch information...
commit 19fa32ae1be4fe7b8e257789ef45c23ef1638d97 1 parent 8e05a1b
Aaron Qian authored
Showing with 23 additions and 1 deletion.
  1. +9 −1 src/packet.coffee
  2. +14 −0 vows/packet-test.js
10 src/packet.coffee
View
@@ -74,6 +74,7 @@ class Packet
unpack: ->
bytes = @value
+ return null unless bytes?
pattern = @pattern[@patternIndex]
if pattern.type == "h"
hex bytes.reverse()
@@ -240,6 +241,13 @@ module.exports.Parser = class Parser extends Packet
break if @offset is @terminal
return @bytesRead - start if offset is end
+ # If we are parsing an array, and repeat is 0
+ # It is essentially an empty array
+ # We don't parse anything
+ else if @pattern[@patternIndex].arrayed and @pattern[@patternIndex].repeat == 0
+ @value = null
+ @terminated = true
+
# Otherwise we're packing bytes into an unsigned integer, the most
# common case.
else
@@ -258,7 +266,7 @@ module.exports.Parser = class Parser extends Packet
# If we are filling an array field the current fields is an array,
# otherwise current field is the value we've just read.
if @pattern[@patternIndex].arrayed
- @fields[@fields.length - 1].push(field)
+ @fields[@fields.length - 1].push(field) if field?
else
@fields.push(field)
14 vows/packet-test.js
View
@@ -186,6 +186,20 @@ vows.describe('Packet').addBatch({
assert.isTrue(invoked);
},
+ 'a length encoded array of bytes that is empty': function (topic) {
+ var invoked = false;
+ var bytes = [ 0x00, 0x00, 0x01, 0x02]
+
+ topic.reset();
+ topic.parse("b16/b8", function (field, engine) {
+ assert.equal(engine.getBytesRead(), 2);
+ assert.equal(field.length, 0);
+ invoked = true;
+ });
+ topic.read(bytes);
+
+ assert.isTrue(invoked);
+ },
'a zero terminated array of bytes': function (topic) {
var invoked = false;
var bytes = [ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00 ]
Please sign in to comment.
Something went wrong with that request. Please try again.