From 90912f763332a81ddbd10a091b7a7b0c79508832 Mon Sep 17 00:00:00 2001
From: Pierre LALET <pierre.lalet@cea.fr>
Date: Sat, 16 Sep 2017 20:57:43 +0200
Subject: [PATCH] Fix & clean FlagsField.*2i() and .i2*() methods

---
 scapy/fields.py | 39 ++++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/scapy/fields.py b/scapy/fields.py
index 4a5411ac..fc41b760 100644
--- a/scapy/fields.py
+++ b/scapy/fields.py
@@ -1139,31 +1139,40 @@ class FlagsField(BitField):
    """
     ismutable = True
     __slots__ = ["multi", "names"]
+
     def __init__(self, name, default, size, names):
         self.multi = isinstance(names, list)
         self.names = names
         BitField.__init__(self, name, default, size)
-    def any2i(self, pkt, x):
+
+    def _fixup_val(self, x):
+        """Returns a FlagValue instance when needed. Internal method, to be
+used in *2i() and i2*() methods.
+
+        """
         if isinstance(x, (list, tuple)):
-            return type(x)(None if v is None else FlagValue(v, self.names)
-                           for v in x)
-        return None if x is None else FlagValue(x, self.names)
+            return type(x)(
+                v if v is None or isinstance(v, FlagValue)
+                else FlagValue(v, self.names)
+                for v in x
+            )
+        return x if x is None or isinstance(x, FlagValue) else FlagValue(x, self.names)
+
+    def any2i(self, pkt, x):
+        return self._fixup_val(super(FlagsField, self).any2i(pkt, x))
+
     def m2i(self, pkt, x):
-        if isinstance(x, (list, tuple)):
-            return type(x)(None if v is None else FlagValue(v, self.names)
-                           for v in x)
-        return None if x is None else FlagValue(x, self.names)
+        return self._fixup_val(super(FlagsField, self).m2i(pkt, x))
+
     def i2h(self, pkt, x):
-        if isinstance(x, (list, tuple)):
-            return type(x)(None if v is None else FlagValue(v, self.names)
-                           for v in x)
-        return None if x is None else FlagValue(x, self.names)
+        return self._fixup_val(super(FlagsField, self).i2h(pkt, x))
+
     def i2repr(self, pkt, x):
         if isinstance(x, (list, tuple)):
             return repr(type(x)(
-                None if v is None else str(FlagValue(v, self.names))
-                for v in x))
-        return None if x is None else str(FlagValue(x, self.names))
+                None if v is None else str(self._fixup_val(v)) for v in x
+            ))
+        return None if x is None else str(self._fixup_val(x))
 
 
 MultiFlagsEntry = collections.namedtuple('MultiFlagEntry', ['short', 'long'])
-- 
GitLab