Skip to content

Fix decompression of empty messages with a ratio limit #2246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Sources/GRPC/Compression/Zlib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ enum Zlib {
/// - Returns: The number of bytes written into `output`.
@discardableResult
func inflate(_ input: inout ByteBuffer, into output: inout ByteBuffer) throws -> Int {
if input.readableBytes == 0 {
// Zero length compressed messages are always empty messages. Skip the inflate step
// below and just return the number of bytes we wrote.
return 0
}

return try input.readWithUnsafeMutableReadableBytes { inputPointer -> (Int, Int) in
// Setup the input buffer.
self.stream.availableInputBytes = inputPointer.count
Expand Down
12 changes: 12 additions & 0 deletions Tests/GRPCTests/ZlibTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,16 @@ class ZlibTests: GRPCTestCase {
let ratio: DecompressionLimit = .ratio(2)
XCTAssertEqual(ratio.maximumDecompressedSize(compressedSize: 10), 20)
}

func testDecompressEmptyPayload() throws {
for limit in [DecompressionLimit.ratio(1), .absolute(1)] {
for format in [Zlib.CompressionFormat.deflate, .gzip] {
var compressed = self.allocator.buffer(capacity: 0)
let inflate = Zlib.Inflate(format: format, limit: limit)
var output = self.allocator.buffer(capacity: 0)
XCTAssertEqual(try inflate.inflate(&compressed, into: &output), 0)
XCTAssertEqual(output.readableBytes, 0)
}
}
}
}