Skip to content

Commit

Permalink
Fix tube.recvpred() timout argument
Browse files Browse the repository at this point in the history
The `timeout` argument wasn't implemented correctly causing the function to hang instead of timing out.
  • Loading branch information
peace-maker committed Oct 25, 2022
1 parent 493a3e3 commit 5128f01
Showing 1 changed file with 16 additions and 1 deletion.
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 5128f01

Please sign in to comment.