Skip to content

Commit 4890d96

Browse files
committed
Allow 256MiB payloads
1 parent 1e15e68 commit 4890d96

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

spec/io_spec.cr

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,20 +304,18 @@ describe MQTT::Protocol::IO do
304304
end
305305
end
306306

307-
it "can checks max_packet_size on remainin length" do
307+
it "checks max_packet_size on remaining length" do
308308
mio = IO::Memory.new
309309
mio.write Bytes[0xFFu8, 0xFFu8, 0xFFu8, 0x7Fu8]
310310

311311
mio.rewind
312312

313313
io = MQTT::Protocol::IO.new(mio)
314314

315+
io = MQTT::Protocol::IO.new(mio, max_packet_size: 268435454u32)
315316
expect_raises(MQTT::Protocol::Error::PacketTooLarge) do
316317
io.read_remaining_length
317318
end
318-
mio.rewind
319-
io = MQTT::Protocol::IO.new(mio, max_packet_size: 268435455u32)
320-
io.read_remaining_length
321319
end
322320

323321
it "checks read_string for max_packet_size" do

src/mqtt/protocol/io.cr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
require "./packets"
22

33
module MQTT
4-
MAX_REMAINING_LENGTH = (128 * 128 * 128).to_u32
4+
MAX_PAYLOAD_SIZE = 268_435_455u32 # 256MiB
5+
MAX_MULTIPLIER = (128 * 128 * 128).to_u32
56

67
module Protocol
78
struct IO
89
getter io
910

1011
def self.new(io : ::IO, max_packet_size : UInt32? = nil,
1112
byte_format = ::IO::ByteFormat::NetworkEndian)
12-
new(io, max_packet_size || MAX_REMAINING_LENGTH, byte_format)
13+
new(io, max_packet_size || MAX_PAYLOAD_SIZE, byte_format)
1314
end
1415

1516
protected def initialize(@io : ::IO, @max_packet_size : UInt32,
@@ -56,7 +57,7 @@ module MQTT
5657
value += (b.to_u32 & 127u32) * multiplier
5758
break if b & 128 == 0
5859
multiplier *= 128
59-
raise Error::PacketDecode.new "invalid remaining length" if multiplier > MAX_REMAINING_LENGTH
60+
raise Error::PacketDecode.new "invalid remaining length" if multiplier > MAX_MULTIPLIER
6061
end
6162
raise Error::PacketTooLarge.new(@max_packet_size, value) if value > @max_packet_size
6263
value

0 commit comments

Comments
 (0)