Skip to content

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

Closed

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

Activity

  1. changed the title [-][bug] Avoid modifying default values of packet list fileds[/-] [+][bug] Avoid modifying default values of packet list fields[/+] on May 9, 2025
  2. gpotter2 commented on May 29, 2025

    @gpotter2
    Member

    I would argue that this is the expected behavior. In the same way that Python's default parameters can be modified, like in the following:

    >>> def function(a=[]):
    ...:     a.append(1)
    ...:     print(a)
    ...:
    ...: function()
    ...: function()
    ...: function()
    [1]
    [1, 1]
    [1, 1, 1]

    Modifying a default is unsupported.

  3. alxroyer-thales commented on Jun 2, 2025

    @alxroyer-thales
    Author

    Oh! I was not aware of that default behaviour of Python 😄
    But that makes it clear why IDEs like PyCharm raise warnings on it.

    Image

    I still think that this is some unwanted behaviour, and that Scapy should not rely on that to keep it the current way.

    For the reminder, the #4727 PR I'm working onto proposes a fix for this issue. Shall we reopen?

  4. gpotter2 commented on Jun 2, 2025

    @gpotter2
    Member

    For the reminder, the #4727 PR I'm working onto proposes a fix for this issue. Shall we reopen?

    If you're bringing a performance improvement, it's a good PR but separate from this issue. I don't consider the behavior described in this issue to be something we want to fix.

  5. alxroyer-thales commented on Jun 2, 2025

    @alxroyer-thales
    Author

    Understood. I'll remove the fix for this issue in PR #4727.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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