pdfdump crashes with UnicodeEncodeError
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 12.6k
- Forks
- 2.2k
- Avg merge
- 1d 4h
- Merged PRs (30d)
- 56
Description
Brief description
When dumping packets with random bytes the following happens:
Traceback (most recent call last):
File "/home/user/test2.py", line 10, in <module>
packet.pdfdump("test.pdf")
File "/home/user/.local/lib/python3.10/site-packages/scapy/base_classes.py", line 474, in pdfdump
canvas = self.canvas_dump(**kargs)
File "/home/user/.local/lib/python3.10/site-packages/scapy/packet.py", line 930, in canvas_dump
vt = pyx.text.text(XSTART + 3.5, (YTXT - y) * YMUL, r"\font\cmssfont=cmss10\cmssfont{%s}" % tex_escape(fval)) # noqa: E501
File "/home/user/.local/lib/python3.10/site-packages/pyx/text.py", line 1455, in wrapped
return f(self, *args, **kwargs)
File "/home/user/.local/lib/python3.10/site-packages/pyx/text.py", line 1491, in text
return self.instance.text(*args, **kwargs)
File "/home/user/.local/lib/python3.10/site-packages/pyx/text.py", line 1331, in text
return self.text_pt(unit.topt(x), unit.topt(y), *args, **kwargs)
File "/home/user/.local/lib/python3.10/site-packages/pyx/text.py", line 1305, in text_pt
left_pt, right_pt, height_pt, depth_pt = self.do_typeset(expr, self.texmessages_run_default + self.texmessages_run + texmessages)
File "/home/user/.local/lib/python3.10/site-packages/pyx/text.py", line 1225, in do_typeset
return self._execute(expr, texmessages, STATE_TYPESET, STATE_TYPESET)
File "/home/user/.local/lib/python3.10/site-packages/pyx/text.py", line 1085, in _execute
expr.encode(self.texenc)
UnicodeEncodeError: 'ascii' codec can't encode character '\u0700' in position 32: ordinal not in range(128)
It seems that texenc in pyx is set to default ASCII. If I change it to 'utf-8' everything works fine.
Scapy version
2.5.0
Python version
3.10
Operating system
Kubuntu 22.04
Additional environment information
No response
How to reproduce
This will eventually fail
from scapy.layers.inet import Ether, UDP, IP
from scapy.compat import raw
import numpy as np
import time
while True:
packet = Ether(dst="00:00:00:00:00:00", src="00:00:00:00:00:00", type=0x800)/IP(src="0.0.0.0", dst="0.0.0.0")/UDP(sport=0, dport=0)
packet = packet/raw(np.random.bytes(512))
packet.pdfdump("test.pdf")
time.sleep(1)
Actual result
Exception
Expected result
A PDF
Related resources
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with scapy/base_classes.py at pdfdump and scapy/packet.py at canvas_dump, then reproduce the failure with the random-bytes script and inspect the PyX text encoding path shown in the traceback. Done means packet.pdfdump("test.pdf") produces a PDF without raising UnicodeEncodeError for random byte payloads.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- computer-graphics
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100