Skip to content
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

Fix tube.recvpred() timout argument #2124

Merged
merged 2 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -95,12 +95,14 @@ The table below shows which release corresponds to each branch, and what date th
- [#1939][1939] Fix error in validating log levels
- [#1981][1981] Fix `cyclic_find()` to make it work with large int values
- [#2123][2123] Fix ROP without a writeable cache directory
- [#2124][2124] Fix `tube.recvpred()` timout argument

[1922]: https://github.com/Gallopsled/pwntools/pull/1922
[1828]: https://github.com/Gallopsled/pwntools/pull/1828
[1939]: https://github.com/Gallopsled/pwntools/pull/1939
[1981]: https://github.com/Gallopsled/pwntools/pull/1981
[2123]: https://github.com/Gallopsled/pwntools/pull/2123
[2124]: https://github.com/Gallopsled/pwntools/pull/2124

## 4.7.1

Expand Down
17 changes: 16 additions & 1 deletion pwnlib/tubes/tube.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,29 @@ def recvpred(self, pred, timeout = default):
Returns:
A bytes object containing bytes received from the socket,
or ``''`` if a timeout occurred while waiting.

Examples:

>>> t = tube()
>>> t.recv_raw = lambda n: b'abbbaccc'
>>> pred = lambda p: p.count(b'a') == 2
>>> t.recvpred(pred)
b'abbba'
>>> pred = lambda p: p.count(b'd') > 0
>>> t.recvpred(pred, timeout=0.05)
b''
"""

data = b''

with self.countdown(timeout):
while not pred(data):
if not self.countdown_active():
self.unrecv(data)
return b''

try:
res = self.recv(1)
res = self.recv(1, timeout=timeout)
except Exception:
self.unrecv(data)
return b''
Expand Down