Skip to content

Commit e0c96c5

Browse files
authored
Fix Str*Field().i2repr() with random / fuzzed values (#3589)
1 parent 9c3a355 commit e0c96c5

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

scapy/fields.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,19 +1811,20 @@ class XStrField(StrField):
18111811

18121812
def i2repr(self, pkt, x):
18131813
# type: (Optional[Packet], bytes) -> str
1814-
if x is None:
1815-
return repr(x)
1816-
return bytes_hex(x).decode()
1814+
if isinstance(x, bytes):
1815+
return bytes_hex(x).decode()
1816+
return super(XStrField, self).i2repr(pkt, x)
18171817

18181818

18191819
class _XStrLenField:
18201820
def i2repr(self, pkt, x):
18211821
# type: (Optional[Packet], bytes) -> str
1822-
if not x:
1823-
return repr(x)
1824-
return bytes_hex(
1825-
x[:(self.length_from or (lambda x: 0))(pkt)] # type: ignore
1826-
).decode()
1822+
if isinstance(x, bytes):
1823+
return bytes_hex(
1824+
x[:(self.length_from or (lambda x: 0))(pkt)] # type: ignore
1825+
).decode()
1826+
# cannot use super() since _XStrLenField does not inherit from Field
1827+
return Field.i2repr(self, pkt, x)
18271828

18281829

18291830
class XStrLenField(_XStrLenField, StrLenField):

test/fields.uts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,3 +2102,18 @@ assert isinstance(p.packet.short1, RandShort)
21022102
assert isinstance(p.packet.byte, RandByte)
21032103
assert isinstance(p.packet.long, RandLong)
21042104
assert isinstance(p.short2, RandShort)
2105+
2106+
############
2107+
############
2108+
+ XStr(*)Field tests
2109+
2110+
= i2repr
2111+
~ field xstrfield
2112+
2113+
from collections import namedtuple
2114+
MockPacket = namedtuple('MockPacket', ['type'])
2115+
2116+
mp = MockPacket(0)
2117+
f = XStrField('test', None)
2118+
x = f.i2repr(mp, RandBin())
2119+
assert x == '<RandBin>'

0 commit comments

Comments
 (0)