Skip to content

Commit 9c26a32

Browse files
Fix Packet.setfieldval() when attr not already in self.fields (#4705)
1 parent 2fec703 commit 9c26a32

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

scapy/packet.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -643,24 +643,26 @@ def setfieldval(self, attr, val):
643643
# type: (str, Any) -> None
644644

645645
# Optimization:
646-
# Try to avoid replacing a value by the same value,
646+
# If `attr` is already in `self.fields`,
647+
# try to avoid replacing a value by the same value,
647648
# and avoid recursive cache invalidation by the way.
648-
try:
649-
old_val = self.getfieldval(attr) # type: Any
650-
if (
651-
# In case of packets, check the given packet reference differs from the previous one.
652-
(val is old_val) if isinstance(val, Packet)
653-
# In case of lists, let's take the new value whatever (no optimization).
654-
else False if isinstance(val, list)
655-
# In the general case, compare the values.
656-
else (val == old_val)
657-
):
658-
return
659-
except AttributeError:
660-
# Field name can't be found (yet?).
661-
# Let the execution go on, especially on the payload.
662-
# An `AttributeError` may eventually be raised in case of a `NoPayload`.
663-
pass
649+
if attr in self.fields:
650+
try:
651+
old_val = self.getfieldval(attr) # type: Any
652+
if (
653+
# In case of packets, check the given packet reference differs from the previous one.
654+
(val is old_val) if isinstance(val, Packet)
655+
# In case of lists, let's take the new value whatever (no optimization).
656+
else False if isinstance(val, list)
657+
# In the general case, compare the values.
658+
else (val == old_val)
659+
):
660+
return
661+
except AttributeError:
662+
# Field name can't be found (yet?).
663+
# Let the execution go on, especially on the payload.
664+
# An `AttributeError` may eventually be raised in case of a `NoPayload`.
665+
pass
664666

665667
if self.deprecated_fields and attr in self.deprecated_fields:
666668
attr = self._resolve_alias(attr)

0 commit comments

Comments
 (0)