Skip to content

Commit 33aeae4

Browse files
Fix parent not set when building default packet fields (#4706, #4707)
- `Packet._ensure_parent_of()` added. - Called in `do_init_cached_fields()`.
1 parent e7465c8 commit 33aeae4

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

scapy/packet.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ def do_init_cached_fields(self, for_dissect_only=False, init_fields=None):
319319

320320
# Fix: Use `copy_field_value()` instead of just `value.copy()`, in order to duplicate list items as well in case of a list.
321321
self.fields[fname] = self.copy_field_value(fname, self.default_fields[fname])
322+
self._ensure_parent_of(self.fields[fname])
322323

323324
def prepare_cached_fields(self, flist):
324325
# type: (Sequence[AnyField]) -> None
@@ -427,6 +428,15 @@ def remove_parent(self, other):
427428
point to the list owner packet."""
428429
self.parent = None
429430

431+
def _ensure_parent_of(self, val):
432+
# type: (Any) -> None
433+
"""Ensures a parent reference with self for val when applicable."""
434+
if isinstance(val, Packet):
435+
val.parent = self
436+
elif isinstance(val, list):
437+
for item in val: # type: Any
438+
self._ensure_parent_of(item)
439+
430440
def copy(self) -> Self:
431441
"""Returns a deep copy of the instance."""
432442
return self._fast_copy(for_clone_with=False)

0 commit comments

Comments
 (0)