Skip to content

Commit 045674f

Browse files
committed
review comments
1 parent e52a349 commit 045674f

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

Sources/NIOCore/SingleStepByteToMessageDecoder.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,9 @@ public final class NIOSingleStepByteToMessageProcessor<Decoder: NIOSingleStepByt
233233
}
234234

235235
self._buffer = nil // To avoid CoW
236+
defer { self._buffer = buffer }
237+
236238
let result = try body(&buffer)
237-
self._buffer = buffer
238239
return result
239240
}
240241

@@ -272,7 +273,6 @@ public final class NIOSingleStepByteToMessageProcessor<Decoder: NIOSingleStepByt
272273
throw ByteToMessageDecoderError.PayloadTooLargeError()
273274
}
274275

275-
// force unwrapping is safe because nil case is handled already and asserted above
276276
if let readerIndex = self._buffer?.readerIndex, readerIndex > 0, self.decoder.shouldReclaimBytes(buffer: self._buffer!) {
277277
self._buffer!.discardReadBytes()
278278
}

Tests/NIOCoreTests/SingleStepByteToMessageDecoderTest.swift

+9-2
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ public final class NIOSingleStepByteToMessageDecoderTest: XCTestCase {
564564
struct DecodeLastError: Error {}
565565

566566
func decodeLast(buffer: inout NIOCore.ByteBuffer, seenEOF: Bool) throws -> NIOCore.ByteBuffer? {
567+
buffer = ByteBuffer() // to allow the decode loop to exit
567568
throw DecodeLastError()
568569
}
569570

@@ -574,10 +575,16 @@ public final class NIOSingleStepByteToMessageDecoderTest: XCTestCase {
574575

575576
let decoder = ThrowingOnLastDecoder()
576577
let b2mp = NIOSingleStepByteToMessageProcessor(decoder)
578+
var errorObserved = false
577579
XCTAssertNoThrow(try b2mp.process(buffer: ByteBuffer(string: "1\n\n2\n3\n")) { line in
578-
XCTAssertThrowsError(try b2mp.finishProcessing(seenEOF: true) { _ in }) { error in
579-
XCTAssertNotNil(error as? ThrowingOnLastDecoder.DecodeLastError)
580+
// We will throw an error to exit the decoding within the nested process call prematurely.
581+
// Unless this is carefully handled we can be left in an inconsistent state which the outer call will encounter
582+
do {
583+
try b2mp.finishProcessing(seenEOF: true) { _ in }
584+
} catch _ as ThrowingOnLastDecoder.DecodeLastError {
585+
errorObserved = true
580586
}
581587
})
588+
XCTAssertTrue(errorObserved)
582589
}
583590
}

0 commit comments

Comments
 (0)