Skip to content

Commit

Permalink
Cleanup __class__ comparisons (secdev#3127)
Browse files Browse the repository at this point in the history
  • Loading branch information
polybassa authored and bzalkilani committed Jun 12, 2022
1 parent 9e7070b commit 085b580
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 60 deletions.
4 changes: 2 additions & 2 deletions scapy/contrib/automotive/bmw/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ class DEV_JOB_PR(Packet):
]

def answers(self, other):
return other.__class__ == DEV_JOB \
and self.identifier == other.identifier
return isinstance(other, DEV_JOB) and \
self.identifier == other.identifier


UDS.services[0xBF] = "DevelopmentJob"
Expand Down
6 changes: 0 additions & 6 deletions scapy/contrib/automotive/bmw/hsfz.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ def hashret(self):
pay_hash = self.payload.hashret()
return hdr_hash + pay_hash

def answers(self, other):
# type: (Packet) -> int
if other.__class__ == self.__class__:
return self.payload.answers(other.payload)
return 0

def extract_padding(self, s):
# type: (bytes) -> Tuple[bytes, bytes]
return s[:self.length - 2], s[self.length - 2:]
Expand Down
2 changes: 1 addition & 1 deletion scapy/contrib/automotive/doip.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class DoIP(Packet):
def answers(self, other):
# type: (Packet) -> int
"""DEV: true if self is an answer from other"""
if other.__class__ == self.__class__:
if isinstance(other, type(self)):
if self.payload_type == 0:
return 1

Expand Down
20 changes: 10 additions & 10 deletions scapy/contrib/automotive/gm/gmlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def determine_len(x):
]

def answers(self, other):
if other.__class__ != self.__class__:
if not isinstance(other, type(self)):
return False
if self.service == 0x7f:
return self.payload.answers(other)
Expand Down Expand Up @@ -165,7 +165,7 @@ class GMLAN_RFRDPR(Packet):
]

def answers(self, other):
return other.__class__ == GMLAN_RFRD and \
return isinstance(other, GMLAN_RFRD) and \
other.subfunction == self.subfunction


Expand Down Expand Up @@ -301,7 +301,7 @@ class GMLAN_RDBIPR(Packet):
]

def answers(self, other):
return other.__class__ == GMLAN_RDBI and \
return isinstance(other, GMLAN_RDBI) and \
other.dataIdentifier == self.dataIdentifier


Expand Down Expand Up @@ -333,7 +333,7 @@ class GMLAN_RDBPIPR(Packet):
]

def answers(self, other):
return other.__class__ == GMLAN_RDBPI and \
return isinstance(other, GMLAN_RDBPI) and \
self.parameterIdentifier in other.identifiers


Expand Down Expand Up @@ -400,7 +400,7 @@ class GMLAN_RMBAPR(Packet):
]

def answers(self, other):
return other.__class__ == GMLAN_RMBA and \
return isinstance(other, GMLAN_RMBA) and \
other.memoryAddress == self.memoryAddress


Expand Down Expand Up @@ -444,7 +444,7 @@ class GMLAN_SAPR(Packet):
]

def answers(self, other):
return other.__class__ == GMLAN_SA \
return isinstance(other, GMLAN_SA) \
and other.subfunction == self.subfunction


Expand All @@ -470,7 +470,7 @@ class GMLAN_DDMPR(Packet):
]

def answers(self, other):
return other.__class__ == GMLAN_DDM \
return isinstance(other, GMLAN_DDM) \
and other.DPIDIdentifier == self.DPIDIdentifier


Expand Down Expand Up @@ -506,7 +506,7 @@ class GMLAN_DPBAPR(Packet):
]

def answers(self, other):
return other.__class__ == GMLAN_DPBA \
return isinstance(other, GMLAN_DPBA) \
and other.parameterIdentifier == self.parameterIdentifier


Expand Down Expand Up @@ -579,7 +579,7 @@ class GMLAN_WDBIPR(Packet):
]

def answers(self, other):
return other.__class__ == GMLAN_WDBI \
return isinstance(other, GMLAN_WDBI) \
and other.dataIdentifier == self.dataIdentifier


Expand Down Expand Up @@ -693,7 +693,7 @@ class GMLAN_DCPR(Packet):
]

def answers(self, other):
return other.__class__ == GMLAN_DC \
return isinstance(other, GMLAN_DC) \
and other.CPIDNumber == self.CPIDNumber


Expand Down
2 changes: 1 addition & 1 deletion scapy/contrib/automotive/obd/iid/iids.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class OBD_S09_PR(Packet):
]

def answers(self, other):
return other.__class__ == OBD_S09 \
return isinstance(other, OBD_S09) \
and all(r.iid in other.iid for r in self.data_records)


Expand Down
2 changes: 1 addition & 1 deletion scapy/contrib/automotive/obd/mid/mids.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ class OBD_S06_PR(Packet):
]

def answers(self, other):
return other.__class__ == OBD_S06 \
return isinstance(other, OBD_S06) \
and all(r.mid in other.mid for r in self.data_records)


Expand Down
4 changes: 2 additions & 2 deletions scapy/contrib/automotive/obd/pid/pids.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class OBD_S01_PR(Packet):
]

def answers(self, other):
return other.__class__ == OBD_S01 \
return isinstance(other, OBD_S01) \
and all(r.pid in other.pid for r in self.data_records)


Expand All @@ -49,7 +49,7 @@ class OBD_S02_PR(Packet):
]

def answers(self, other):
return other.__class__ == OBD_S02 \
return isinstance(other, OBD_S02) \
and all(r.pid in [o.pid for o in other.requests]
for r in self.data_records)

Expand Down
8 changes: 4 additions & 4 deletions scapy/contrib/automotive/obd/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class OBD_S03_PR(Packet):
]

def answers(self, other):
return other.__class__ == OBD_S03
return isinstance(other, OBD_S03)


class OBD_S04(Packet):
Expand All @@ -99,7 +99,7 @@ class OBD_S04_PR(Packet):
name = "S4_ClearDTCsPositiveResponse"

def answers(self, other):
return other.__class__ == OBD_S04
return isinstance(other, OBD_S04)


class OBD_S06(Packet):
Expand All @@ -121,7 +121,7 @@ class OBD_S07_PR(Packet):
]

def answers(self, other):
return other.__class__ == OBD_S07
return isinstance(other, OBD_S07)


class OBD_S08(Packet):
Expand Down Expand Up @@ -150,4 +150,4 @@ class OBD_S0A_PR(Packet):
]

def answers(self, other):
return other.__class__ == OBD_S0A
return isinstance(other, OBD_S0A)
2 changes: 1 addition & 1 deletion scapy/contrib/automotive/obd/tid/tids.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class OBD_S08_PR(Packet):
]

def answers(self, other):
return other.__class__ == OBD_S08 \
return isinstance(other, OBD_S08) \
and all(r.tid in other.tid for r in self.data_records)


Expand Down
2 changes: 1 addition & 1 deletion scapy/contrib/automotive/someip.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def post_build(self, pkt, pay):
return pkt + pay

def answers(self, other):
if other.__class__ == self.__class__:
if isinstance(other, type(self)):
if self.msg_type in [SOMEIP.TYPE_REQUEST_NO_RET,
SOMEIP.TYPE_REQUEST_NORET_ACK,
SOMEIP.TYPE_NOTIFICATION,
Expand Down
Loading

0 comments on commit 085b580

Please sign in to comment.