Skip to content

Commit 0bb948d

Browse files
authored
Merge pull request #89 from oremanj/fix-str-segfault
Check whether payload is NULL before accessing it in __str__
2 parents a3dedae + 9f460d2 commit 0bb948d

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

netfilterqueue/_impl.pyx

+9-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ cdef class Packet:
5757
self._given_payload = None
5858

5959
def __str__(self):
60-
cdef iphdr *hdr = <iphdr*>self.payload
60+
cdef unsigned char *payload = NULL
61+
if self._owned_payload:
62+
payload = self._owned_payload
63+
elif self.payload != NULL:
64+
payload = self.payload
65+
else:
66+
return "%d byte packet, contents unretained" % (self.payload_len,)
67+
68+
cdef iphdr *hdr = <iphdr*>payload
6169
protocol = PROTOCOLS.get(hdr.protocol, "Unknown protocol")
6270
return "%s packet, %s bytes" % (protocol, self.payload_len)
6371

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
exec(open("netfilterqueue/_version.py", encoding="utf-8").read())
55

6-
setup_requires = []
6+
setup_requires = ["wheel"]
77
try:
88
# Use Cython
99
from Cython.Build import cythonize
@@ -21,7 +21,7 @@
2121
if "egg_info" in sys.argv:
2222
# We're being run by pip to figure out what we need. Request cython in
2323
# setup_requires below.
24-
setup_requires = ["cython"]
24+
setup_requires += ["cython"]
2525
elif not os.path.exists(
2626
os.path.join(os.path.dirname(__file__), "netfilterqueue/_impl.c")
2727
):

tests/test_basic.py

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ async def test_comms_without_queue(harness):
2323
async def test_queue_dropping(harness):
2424
async def drop(packets, msg):
2525
async for packet in packets:
26+
assert "UDP packet" in str(packet)
2627
if packet.get_payload()[28:] == msg:
2728
packet.drop()
2829
else:
@@ -190,6 +191,7 @@ async def test_errors(harness):
190191
async def test_unretained(harness):
191192
def cb(chan, pkt):
192193
# Can access payload within callback
194+
assert "UDP packet" in str(pkt)
193195
assert pkt.get_payload()[-3:] in (b"one", b"two")
194196
chan.send_nowait(pkt)
195197

@@ -202,6 +204,7 @@ def cb(chan, pkt):
202204
RuntimeError, match="Payload data is no longer available"
203205
):
204206
p.get_payload()
207+
assert "contents unretained" in str(p)
205208
# Can still issue verdicts though
206209
if accept:
207210
p.accept()

0 commit comments

Comments
 (0)