Skip to content

Commit

Permalink
Prevent the use of floating numbers for keepalive
Browse files Browse the repository at this point in the history
  • Loading branch information
nightwolfz committed Jan 26, 2016
1 parent b7423d5 commit a59d020
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 7 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,11 @@ testGenerateError('Invalid keepalive', {
, password: 'password'
})

testGenerateError('Invalid keepalive', {
cmd: 'connect'
, keepalive: 3.1416
})

testGenerateError('Invalid will', {
cmd: 'connect'
, retain: false
Expand Down Expand Up @@ -912,7 +917,7 @@ testParseError('invalid protocol id', new Buffer([
116, 101, 115, 116 // Client id
]))

// CONNECT Packets that contain an unsupported protocol version
// CONNECT Packets that contain an unsupported protocol version
// flag (i.e. not `3` or `4`) should cause an error
testParseError('invalid protocol version', new Buffer([
16, 18,
Expand All @@ -926,7 +931,7 @@ testParseError('invalid protocol version', new Buffer([
]))

// when a packet contains a string in the variable header and the
// given string length of this exceeds the overall length of the packet that
// given string length of this exceeds the overall length of the packet that
// was specified in the fixed header, parsing must fail.
// this case simulates this behavior with the protocol id string of the
// CONNECT packet. The fixed header suggests a remaining length of 8 bytes
Expand Down
5 changes: 3 additions & 2 deletions writeToStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ function connect(opts, stream) {
// Must be a two byte number
if ('number' !== typeof keepalive ||
keepalive < 0 ||
keepalive > 65535) {
keepalive > 65535 ||
keepalive % 1 !== 0) {
stream.emit('error', new Error('Invalid keepalive'))
} else {
length += 2
Expand Down Expand Up @@ -563,7 +564,7 @@ function writeBuffer(buffer, pos, src) {
* @api private
*/
function writeNumber(stream, number) {
return stream.write(numCache[number | 0])
return stream.write(numCache[number])
}

/**
Expand Down

0 comments on commit a59d020

Please sign in to comment.