From 1d54822295a1c628a9f23e6dcbdee40c8e14c3a2 Mon Sep 17 00:00:00 2001 From: Dimitri Bouniol Date: Thu, 18 Nov 2021 13:02:19 -0800 Subject: [PATCH] Fixed an issue where a max bytes size of 0 could incorrectly read from the stream --- Sources/Bytes/AsyncBytes.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sources/Bytes/AsyncBytes.swift b/Sources/Bytes/AsyncBytes.swift index 8f44553..45e003b 100644 --- a/Sources/Bytes/AsyncBytes.swift +++ b/Sources/Bytes/AsyncBytes.swift @@ -42,6 +42,8 @@ extension AsyncIteratorProtocol where Element == Byte { ) async throws -> Bytes { precondition(minCount <= maxCount, "maxCount must be larger than or equal to minCount") precondition(minCount >= 0, "minCount must be larger than 0") + guard maxCount > 0 else { return [] } + var result = Bytes() result.reserveCapacity(minCount) @@ -72,6 +74,8 @@ extension AsyncIteratorProtocol where Element == Byte { max maxCount: Int ) async rethrows -> Bytes { precondition(maxCount >= 0, "maxCount must be larger than 0") + guard maxCount > 0 else { return [] } + var result = Bytes() result.reserveCapacity(maxCount) @@ -118,6 +122,8 @@ extension AsyncIteratorProtocol where Element == Byte { ) async throws -> Bytes? { precondition(minCount <= maxCount, "maxCount must be larger than or equal to minCount") precondition(minCount >= 0, "minCount must be larger than 0") + guard maxCount > 0 else { return [] } + var result = Bytes() result.reserveCapacity(minCount) @@ -149,7 +155,9 @@ extension AsyncIteratorProtocol where Element == Byte { bytes type: Bytes.Type, max maxCount: Int ) async rethrows -> Bytes? { - precondition(maxCount > 0, "maxCount must be larger than 0") + precondition(maxCount >= 0, "maxCount must be larger than 0") + guard maxCount > 0 else { return [] } + var result = Bytes() result.reserveCapacity(maxCount)