Skip to content

Commit af9567d

Browse files
committed
add necessary handling to correctly read when b value is corrected to -1
1 parent 4b0fb9f commit af9567d

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/aws_encryption_sdk/streaming_client.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def read(self, b=-1):
223223
if self.closed:
224224
raise ValueError("I/O operation on closed file")
225225

226-
if b:
226+
if b >= 0:
227227
self._read_bytes(b)
228228
output.write(self.output_buffer[:b])
229229
self.output_buffer = self.output_buffer[b:]
@@ -539,13 +539,19 @@ def _read_bytes_to_framed_body(self, b):
539539
"""
540540
_LOGGER.debug("collecting %s bytes", b)
541541
_b = b
542-
b = int(math.ceil(b / float(self.config.frame_length)) * self.config.frame_length)
543-
_LOGGER.debug("%s bytes requested; reading %s bytes after normalizing to frame length", _b, b)
542+
543+
if b > 0:
544+
_frames_to_read = math.ceil(b / float(self.config.frame_length))
545+
b = int(_frames_to_read * self.config.frame_length)
546+
_LOGGER.debug("%d bytes requested; reading %d bytes after normalizing to frame length", _b, b)
547+
544548
plaintext = self.source_stream.read(b)
545-
_LOGGER.debug("%s bytes read from source", len(plaintext))
549+
plaintext_length = len(plaintext)
550+
_LOGGER.debug("%d bytes read from source", plaintext_length)
551+
546552
finalize = False
547553

548-
if len(plaintext) < b:
554+
if b < 0 or plaintext_length < b:
549555
_LOGGER.debug("Final plaintext read from source")
550556
finalize = True
551557

@@ -594,8 +600,8 @@ def _read_bytes(self, b):
594600
:param int b: Number of bytes to read
595601
:raises NotSupportedError: if content type is not supported
596602
"""
597-
_LOGGER.debug("%s bytes requested from stream with content type: %s", b, self.content_type)
598-
if b <= len(self.output_buffer) or self.source_stream.closed:
603+
_LOGGER.debug("%d bytes requested from stream with content type: %s", b, self.content_type)
604+
if 0 <= b <= len(self.output_buffer) or self.source_stream.closed:
599605
_LOGGER.debug("No need to read from source stream or source stream closed")
600606
return
601607

@@ -858,7 +864,8 @@ def _read_bytes(self, b):
858864
_LOGGER.debug("Source stream closed")
859865
return
860866

861-
if b <= len(self.output_buffer):
867+
buffer_length = len(self.output_buffer)
868+
if 0 <= b <= buffer_length:
862869
_LOGGER.debug(
863870
"%s bytes requested less than or equal to current output buffer size %s", b, len(self.output_buffer)
864871
)

0 commit comments

Comments
 (0)