Skip to content

[bug] Avoid modifying default values of packet list fields #4735

Closed
@alxroyer-thales

Description

@alxroyer-thales

Brief description

When a packet list field is provided with a default list for value, instances of the given packet then just .copy() the packet list without copying the packets inside.
Consequently, modifying anything in a packet of the list from an instance of the owner packet modifies the thing for other instances (except for clones!)

Scapy version

2.6.1

Python version

3.8.6

Operating system

Windows

Additional environment information

No response

How to reproduce

class B(Packet):
    fields_desc = [
        ByteField("value", default=0),
    ]

class A(Packet):
    fields_desc = [
        PacketListField("b", pkt_cls=B, default=[B(value=i) for i in range(3)])
    ]

a0 = A()

a1 = A()
a1_clone = a1.copy()

a2 = A()

a1.b[1].value = 10

a3 = A()

print("a0:")
a0.show()
print("a1:")
a1.show()
print("Clone of a1:")
a1_clone.show()
print("a2:")
a2.show()
print("a3:")
a3.show()

Actual result

a0:
###[ A ]###
  \b         \
   |###[ B ]###
   |  value     = 0
   |###[ B ]###
   |  value     = 10
   |###[ B ]###
   |  value     = 2

a1:
###[ A ]###
  \b         \
   |###[ B ]###
   |  value     = 0
   |###[ B ]###
   |  value     = 10
   |###[ B ]###
   |  value     = 2

Clone of a1:
###[ A ]###
  \b         \
   |###[ B ]###
   |  value     = 0
   |###[ B ]###
   |  value     = 1
   |###[ B ]###
   |  value     = 2

a2:
###[ A ]###
  \b         \
   |###[ B ]###
   |  value     = 0
   |###[ B ]###
   |  value     = 10
   |###[ B ]###
   |  value     = 2

a3:
###[ A ]###
  \b         \
   |###[ B ]###
   |  value     = 0
   |###[ B ]###
   |  value     = 10
   |###[ B ]###
   |  value     = 2

Expected result

b[1].value should be 10 for a1 only.

Related resources

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions