Skip to content

Commit

Permalink
Fix tube.recvpred() timout argument (#2124)
Browse files Browse the repository at this point in the history
* Fix tube.recvpred() timout argument

The `timeout` argument wasn't implemented correctly causing the function to hang instead of timing out.

* Update CHANGELOG.md
  • Loading branch information
peace-maker committed Nov 1, 2022
1 parent a7cb549 commit 67d49a6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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

0 comments on commit 67d49a6

Please sign in to comment.