Skip to content

Commit 2114692

Browse files
authored
Fix readline omitting a trailing \n (#2349)
* Fix readline omitting a trailing \n This is a regression from #2129 which was based on a bogus test which mixed `input` and `readline` calls. Only `input` (and `raw_input`) are documented to strip the newline, but `readline` itself should include it. Fixes #2342 * Update CHANGELOG
1 parent b5e4ff1 commit 2114692

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ The table below shows which release corresponds to each branch, and what date th
9090
[2257]: https://github.com/Gallopsled/pwntools/pull/2257
9191
[2225]: https://github.com/Gallopsled/pwntools/pull/2225
9292

93+
## 4.11.2
94+
- [#2349][2349] Fix term.readline omitting a trailing \n
95+
96+
[2349]: https://github.com/Gallopsled/pwntools/pull/2349
97+
9398
## 4.11.1 (`stable`)
9499

95100
- [#2271][2271] FIX: Generated shebang with path to python invalid if path contains spaces

pwnlib/term/readline.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import six
77
import sys
8+
import os
89

910
from pwnlib.term import keyconsts as kc
1011
from pwnlib.term import keymap as km
@@ -404,7 +405,7 @@ def readline(_size=-1, prompt='', float=True, priority=10):
404405
buffer = (buffer_left + buffer_right)
405406
if buffer:
406407
history.insert(0, buffer)
407-
return force_to_bytes(buffer)
408+
return force_to_bytes(buffer) + b'\n'
408409
except KeyboardInterrupt:
409410
control_c()
410411
finally:
@@ -432,7 +433,7 @@ def raw_input(prompt='', float=True):
432433
float(bool): If set to `True`, prompt and input will float to the
433434
bottom of the screen when `term.term_mode` is enabled.
434435
"""
435-
return readline(-1, prompt, float)
436+
return readline(-1, prompt, float).rstrip(os.linesep.encode())
436437

437438
def str_input(prompt='', float=True):
438439
r"""str_input(prompt='', float=True)
@@ -445,7 +446,7 @@ def str_input(prompt='', float=True):
445446
float(bool): If set to `True`, prompt and input will float to the
446447
bottom of the screen when `term.term_mode` is enabled.
447448
"""
448-
return readline(-1, prompt, float).decode()
449+
return readline(-1, prompt, float).decode().rstrip(os.linesep)
449450

450451
def eval_input(prompt='', float=True):
451452
"""eval_input(prompt='', float=True)
@@ -471,7 +472,7 @@ def eval_input(prompt='', float=True):
471472
Favorite object? 20
472473
"""
473474
from pwnlib.util import safeeval
474-
return safeeval.const(readline(-1, prompt, float))
475+
return safeeval.const(readline(-1, prompt, float).rstrip(os.linesep.encode()))
475476

476477
def init():
477478
global safeeval

pwnlib/tubes/tube.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,8 @@ def recv_thread():
900900
while not go.isSet():
901901
if term.term_mode:
902902
data = term.readline.readline(prompt = prompt, float = True)
903-
if data:
904-
data += self.newline
903+
if data.endswith(b'\n') and self.newline != b'\n':
904+
data = data[:-1] + self.newline
905905
else:
906906
stdin = getattr(sys.stdin, 'buffer', sys.stdin)
907907
data = stdin.read(1)

0 commit comments

Comments
 (0)