Skip to content

Commit c928204

Browse files
committed
Fix bulkString bug
1 parent abcab12 commit c928204

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

Sources/NIORedis/Coders/RedisDataDecoder.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,22 @@ extension RedisDataDecoder {
128128
// Redis sends '-1' to represent a null string
129129
guard size > -1 else { return .parsed(.null) }
130130

131-
// Redis can hold empty bulk strings, and represents it with a 0 size
132-
// so return an empty string
131+
// verify that we have our expected bulk string message
132+
// by adding the expected CRLF bytes to the parsed size
133+
// even if the size is 0, Redis provides line endings (i.e. $0\r\n\r\n)
134+
let readableByteCount = buffer.readableBytes - position
135+
let expectedRemainingMessageSize = size + 2
136+
guard readableByteCount >= expectedRemainingMessageSize else { return .notYetParsed }
137+
133138
guard size > 0 else {
134139
// Move the tip of the message position
135-
// since size = 0, and we successfully parsed the size
136-
// the beginning of the next message should be 2 further (the final \r\n - $0\r\n\r\n)
137140
position += 2
138141
return .parsed(.bulkString(Data()))
139142
}
140143

141-
// verify that we have at least our expected bulk string message
142-
// by adding the expected CRLF bytes to the parsed size
143-
let readableByteCount = buffer.readableBytes - position
144-
let expectedRemainingMessageSize = size + 2
145-
guard readableByteCount >= expectedRemainingMessageSize else { return .notYetParsed }
146-
147-
guard let bytes = buffer.copyBytes(at: position, length: expectedRemainingMessageSize) else { return .notYetParsed }
144+
guard let bytes = buffer.copyBytes(at: position, length: expectedRemainingMessageSize) else {
145+
return .notYetParsed
146+
}
148147

149148
// Move the tip of the message position for recursive parsing to just after the newline
150149
// of the bulk string content

Tests/NIORedisTests/RedisDataDecoder+ParsingTests.swift renamed to Tests/NIORedisTests/Data/RedisDataDecoder+ParsingTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,13 @@ extension RedisDataDecoderParsingTests {
255255

256256
extension RedisDataDecoderParsingTests {
257257
func testParsing_bulkString_handlesMissingEndings() throws {
258-
for message in ["$6", "$6\r\n", "$6\r\nabcdef"] {
258+
for message in ["$6", "$6\r\n", "$6\r\nabcdef", "$0\r\n"] {
259259
XCTAssertNil(parseTestBulkString(message))
260260
}
261261
}
262262

263263
func testParsing_bulkString_withNoSize_returnsEmpty() throws {
264-
let result = parseTestBulkString("$0\r\n")
264+
let result = parseTestBulkString("$0\r\n\r\n")
265265
XCTAssertEqual(result?.data?.count, 0)
266266
}
267267

0 commit comments

Comments
 (0)