From 4ab64d5746da204b6e13a58c78891cff5dbfa4e3 Mon Sep 17 00:00:00 2001 From: Mike Kaplinskiy Date: Sun, 16 Nov 2014 04:14:02 -0500 Subject: [PATCH] Fix issue with receiving 0-length strings & framed transport. When we request to read 0 bytes we would attempt to read a frame. If we're at the end of the message, this will never return. --- tests/test_framed_transport.py | 15 +++++++++++++++ thriftpy/transport/transport.py | 5 +++++ 2 files changed, 20 insertions(+) diff --git a/tests/test_framed_transport.py b/tests/test_framed_transport.py index 4fdcab0..b8c50f3 100644 --- a/tests/test_framed_transport.py +++ b/tests/test_framed_transport.py @@ -37,6 +37,14 @@ def add(self, person): self.registry[person.name] = person return True + def get(self, name): + """ + Person get(1: string name) + """ + if name not in self.registry: + raise addressbook.PersonNotExistsError() + return self.registry[name] + class FramedTransportTestCase(TestCase): def mk_server(self): @@ -75,3 +83,10 @@ def test_able_to_communicate(self): assert success success = self.client.add(dennis) assert not success + + def test_zero_length_string(self): + dennis = addressbook.Person(name='') + success = self.client.add(dennis) + assert success + success = self.client.get(name='') + assert success diff --git a/thriftpy/transport/transport.py b/thriftpy/transport/transport.py index 8187786..9500514 100644 --- a/thriftpy/transport/transport.py +++ b/thriftpy/transport/transport.py @@ -156,6 +156,11 @@ def close(self): return self.__trans.close() def read(self, sz): + # Important: don't attempt to read the next frame if the caller + # doesn't actually need any data. + if sz == 0: + return b'' + ret = self.__rbuf.read(sz) if len(ret) != 0: return ret