Skip to content

Commit

Permalink
Fix incorrect use of length on protocol byte strings
Browse files Browse the repository at this point in the history
  • Loading branch information
driskell committed Nov 10, 2015
1 parent 9cc2837 commit 8bbe619
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
7 changes: 6 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
log-courier (1.8.2.pre.2.pre.g7019112)
log-courier (1.8.3.pre.2.pre.g9cc2837)
cabin (~> 0.6)
ffi-rzmq (~> 2.0)
multi_json (~> 1.10)
Expand All @@ -15,16 +15,19 @@ GEM
cabin (0.7.2)
diff-lcs (1.2.5)
ffi (1.9.10)
ffi (1.9.10-java)
ffi-rzmq (2.0.4)
ffi-rzmq-core (>= 1.0.1)
ffi-rzmq-core (1.0.4)
ffi (~> 1.9)
jrjackson (0.3.7)
multi_json (1.11.2)
oj (2.13.0)
parser (2.2.3.0)
ast (>= 1.1, < 3.0)
powerpack (0.1.1)
rainbow (2.0.0)
rake (10.4.2)
rspec (3.3.0)
rspec-core (~> 3.3.0)
rspec-expectations (~> 3.3.0)
Expand All @@ -48,12 +51,14 @@ GEM
ruby-progressbar (1.7.5)

PLATFORMS
java
ruby

DEPENDENCIES
jrjackson (~> 0.2)
log-courier!
oj (~> 2.11)
rake
rspec (~> 3.1)
rspec-core (~> 3.1)
rubocop
Expand Down
6 changes: 3 additions & 3 deletions lib/log-courier/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def generate
events.each do |event|
json_data = self.class.get_json_adapter.dump(event)
# Add length and then the data
buffer << [json_data.length].pack('N') << json_data
buffer << [json_data.bytesize].pack('N') << json_data
end

# Generate and store the payload
Expand Down Expand Up @@ -435,7 +435,7 @@ def send_jdat(events)

def process_pong(message)
# Sanity
fail ProtocolError, "Unexpected data attached to pong message (#{message.length})" if message.length != 0
fail ProtocolError, "Unexpected data attached to pong message (#{message.bytesize})" if message.bytesize != 0

@logger.debug 'PONG message received' unless @logger.nil? || !@logger.debug?

Expand All @@ -446,7 +446,7 @@ def process_pong(message)

def process_ackn(message)
# Sanity
fail ProtocolError, "ACKN message size invalid (#{message.length})" if message.length != 20
fail ProtocolError, "ACKN message size invalid (#{message.bytesize})" if message.bytesize != 20

# Grab nonce
nonce, sequence = message.unpack('a16N')
Expand Down
18 changes: 9 additions & 9 deletions lib/log-courier/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ def stop

def process_ping(message, comm)
# Size of message should be 0
if message.length != 0
fail ProtocolError, "unexpected data attached to ping message (#{message.length})"
if message.bytesize != 0
fail ProtocolError, "unexpected data attached to ping message (#{message.bytesize})"
end

# PONG!
Expand All @@ -142,8 +142,8 @@ def process_jdat(message, comm, event_queue)
# OK - first is a nonce - we send this back with sequence acks
# This allows the client to know what is being acknowledged
# Nonce is 16 so check we have enough
if message.length < 17
fail ProtocolError, "JDAT message too small (#{message.length})"
if message.bytesize < 17
fail ProtocolError, "JDAT message too small (#{message.bytesize})"
end

nonce = message[0...16]
Expand All @@ -155,7 +155,7 @@ def process_jdat(message, comm, event_queue)
end

# The remainder of the message is the compressed data block
message = StringIO.new Zlib::Inflate.inflate(message[16...message.length])
message = StringIO.new Zlib::Inflate.inflate(message.byteslice(16, message.bytesize))

# Message now contains JSON encoded events
# They are aligned as [length][event]... so on
Expand All @@ -170,17 +170,17 @@ def process_jdat(message, comm, event_queue)
if ret.nil?
# Finished!
break
elsif length_buf.length < 4
fail ProtocolError, "JDAT length extraction failed (#{ret} #{length_buf.length})"
elsif length_buf.bytesize < 4
fail ProtocolError, "JDAT length extraction failed (#{ret} #{length_buf.bytesize})"
end

length = length_buf.unpack('N').first

# Extract message
ret = message.read length, data_buf
if ret.nil? or data_buf.length < length
if ret.nil? or data_buf.bytesize < length
@logger.warn()
fail ProtocolError, "JDAT message extraction failed #{ret} #{data_buf.length}"
fail ProtocolError, "JDAT message extraction failed #{ret} #{data_buf.bytesize}"
end

data_buf.force_encoding('utf-8')
Expand Down
2 changes: 1 addition & 1 deletion log-courier.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |gem|
gem.name = 'log-courier'
gem.version = version
gem.description = 'Log Courier library'
gem.summary = 'Ruby implementation of the Log Courier protocol'
gem.summary = 'Ruby implementation of the Courier protocol'
gem.homepage = 'https://github.com/driskell/ruby-log-courier'
gem.authors = ['Jason Woods']
gem.email = ['devel@jasonwoods.me.uk']
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.8.3
1.9

0 comments on commit 8bbe619

Please sign in to comment.