Skip to content

Return buffered data on first EOF in tube.readline() #2376

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 5 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ The table below shows which release corresponds to each branch, and what date th

- [#2356][2356] Add local libc database provider for libcdb
- [#2374][2374] libcdb.unstrip_libc: debug symbols are fetched only if not present
- [#2376][2376] Return buffered data on first EOF in tube.readline()

[2356]: https://github.com/Gallopsled/pwntools/pull/2356
[2374]: https://github.com/Gallopsled/pwntools/pull/2374
[2376]: https://github.com/Gallopsled/pwntools/pull/2376

## 4.13.0 (`beta`)

Expand Down
35 changes: 32 additions & 3 deletions pwnlib/tubes/tube.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,15 +467,22 @@ def recvline(self, keepends=True, timeout=default):
Receive a single line from the tube.

A "line" is any sequence of bytes terminated by the byte sequence
set in :attr:`newline`, which defaults to ``'\n'``.
set in :attr:`newline`, which defaults to ``b'\n'``.

If the connection is closed (:class:`EOFError`) before a newline is received, the
buffered data is returned. If the buffer is empty, an :class:`EOFError` is raised.

If the request is not satisfied before ``timeout`` seconds pass,
all data is buffered and an empty string (``''``) is returned.
all data is buffered and an empty byte string (``b''``) is returned.

Arguments:
keepends(bool): Keep the line ending (:const:`True`).
timeout(int): Timeout

Raises:
exceptions.EOFError: The connection closed before the request
could be satisfied and the buffer is empty

Return:
All bytes received over the tube until the first
newline ``'\n'`` is received. Optionally retains
Expand All @@ -494,8 +501,30 @@ def recvline(self, keepends=True, timeout=default):
>>> t.newline = b'\r\n'
>>> t.recvline(keepends = False)
b'Foo\nBar'
>>> t = tube()
>>> def _recv_eof(n):
... if not _recv_eof.throw:
... _recv_eof.throw = True
... return b'real line\ntrailing data'
... raise EOFError
>>> _recv_eof.throw = False
>>> t.recv_raw = _recv_eof
>>> t.recvline()
b'real line\n'
>>> t.recvline()
b'trailing data'
>>> t.recvline() # doctest: +ELLIPSIS
Traceback (most recent call last):
...
EOFError
"""
return self.recvuntil(self.newline, drop = not keepends, timeout = timeout)
try:
return self.recvuntil(self.newline, drop = not keepends, timeout = timeout)
except EOFError:
if self.buffer.size > 0:
self.warn_once('EOFError during recvline. Returning buffered data without trailing newline.')
return self.buffer.get()
raise

def recvline_pred(self, pred, keepends=False, timeout=default):
r"""recvline_pred(pred, keepends=False) -> bytes
Expand Down
Loading