diff --git a/bufferpack.js b/bufferpack.js index ff4ec7b..337c894 100644 --- a/bufferpack.js +++ b/bufferpack.js @@ -172,6 +172,7 @@ function BufferPack() { bBE = (fmt.charAt(0) != '<'); p = p?p:0; + var al = a.length; var re = new RegExp(this._sPattern, 'g'); var m; var n; @@ -186,6 +187,8 @@ function BufferPack() { n = 0; // Need to deal with empty null term strings while(a[p + n] !== 0) { n++; + if (p + n >= al) // Don't read past the end of the buffer + return undefined; } n++; // Add one for null byte } diff --git a/test/cstring.test.js b/test/cstring.test.js index b7e2ebc..45be82a 100644 --- a/test/cstring.test.js +++ b/test/cstring.test.js @@ -88,5 +88,14 @@ describe('C String', function() { unpacked.forth.should.equal('asdf'); }); }); + describe('strings without null terminators', function() { + var values = [0x3c]; + var packed = bufferpack.pack('b', values); + + it('should return undefined', function() { + var unpacked = bufferpack.unpack('S', packed); + (unpacked === undefined).should.be.true; + }); + }); }); });