You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# CWE-595: Comparison of Object References Instead of Object Contents
2
2
3
-
In Python, the `==` operator is implemented by the `__eq__` method on an object [[python.org data model 2023](https://docs.python.org/3/reference/datamodel.html?highlight=__eq__#object.__eq__)]. For built-in types like `int` and `str`, the comparison is implemented in the interpreter. The main issue comes when implementing custom classes, where the default implementation compares object references using the `is` operator. The `is` operator compares the identities of the objects, equivalent to `id(obj1) == id(obj2)`. The `id` function is built into Python, and in the CPython interpreter, the standard implementation, it returns the object's memory address [[de Langen 2023](https://realpython.com/python-is-identity-vs-equality/)].
3
+
Prevent unexpected results by knowing the differences between comparison operators such as `==` and `is`.
4
+
5
+
In Python, the `==` operator is implemented by the `__eq__` method on an object [[python.org data model 2023](https://docs.python.org/3/reference/datamodel.html?highlight=__eq__#object.__eq__)]. For built-in types like `int` and `str`, the comparison is implemented in the interpreter. The main issue comes when implementing custom classes, where the default implementation compares object references using the `is` operator. The `is` operator compares the identities of the objects, equivalent to `id(obj1) == id(obj2)`.
6
+
In CPython, this is their memory address. Everything in Python is an object, and each object is stored at a specific memory location [[de Langen 2023](https://realpython.com/python-is-identity-vs-equality/)].
4
7
5
8
You want to implement the `__eq__` method on a class if you believe you ever want to compare it to another object or find it in a list of objects. Actually, it is so common that the `dataclasses.dataclass` decorator by default implements it for you [[dataclasses — Data Classes — Python 3.11.4 documentation](https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass)].
6
9
10
+
Be aware of Python's memory optimization for strings and numbers as demonstrated in `example01.py` code.
11
+
Python tries to avoid allocating more memory for the same string. The process of reusing already existing strings is a Python optimization technique known as **String interning**[[sys — System-specific parameters and functions — Python 3.11.4 documentation](https://docs.python.org/3/library/sys.html#sys.intern)] According to the documentation, "CPython keeps an array of integer objects for all integers between `-5` and `256`. When you create an `int` in that range you actually just get back a reference to the existing object." [[Integer objects — Python 3.11.4 documentation](https://docs.python.org/3/c-api/long.html#c.PyLong_FromLong)]
12
+
13
+
_[example01.py:](example01.py)_
14
+
15
+
```py
16
+
""" Code Example """
17
+
18
+
print("-"*10+"Memory optimization with strings"+10*"-")
print("-"*10+"Memory optimization with numbers"+10*"-")
28
+
a = b =256
29
+
print (f"{a} is {b}?", a is b)
30
+
a = b =257
31
+
print (f"{a} is {b}?", a is b)
32
+
33
+
print("-"*10+"Memory optimization with numbers in a loop"+10*"-")
34
+
a = b =255
35
+
while(a is b):
36
+
a +=1
37
+
b +=1
38
+
print (f"{a} is {b}?", a is b)
39
+
```
40
+
41
+
**Output of example01.py:**
42
+
43
+
```bash
44
+
----------Memory optimization with strings----------
45
+
a is b: foobar is foobar? True
46
+
a is c: foobar is foobar? False
47
+
a == c: foobar == foobar? True
48
+
size? len(a)=6 len(b)=6 len(c)=6
49
+
----------Memory optimization with numbers----------
50
+
256 is 256? True
51
+
257 is 257? True
52
+
----------Memory optimization with numbers in a loop----------
53
+
256 is 256? True
54
+
257 is 257? False
55
+
```
56
+
57
+
The first set of print statements illustrates string interning. While `a` and `b` reuse the same object, `c` is created by joining two new strings, which results in an object with a different `id()`. The variables in the middle example both point to the same number object, which is why comparing them after `a = b = 257` still returns `True` even though `257` falls outside of the cached range. However, when assigning values in a loop, Python needs to allocate new objects for numbers greater than `256` and thus will create two separate objects as soon as it hits `257`. The way caching and interning works may differ between running a Python script from a file and using REPL, which may produce different results when running `example01.py` in Python's interactive mode.
58
+
7
59
## Non-Compliant Code Example
8
60
9
-
The non-compliant code shows how the default comparison operator compares object references rather than the object values. Furthermore, it displays how this causes issues when comparing lists of objects, although it applies to other types of collections as well. Finally, it shows how the `in` operator also depends on the behavior of the `__eq__` method and, therefore, also returns a non-desirable result.
61
+
The `noncompliant01.py` code demonstrates potentially unexpected outcomes when using different comparisons.
10
62
11
-
[*noncompliant01.py:*](noncompliant01.py)
63
+
* The `==` operator using `__eq__`, checks value equality for most build-in types, checks for reference equality if the `__eq__` is missing in a custom class. So `12 == 12` is `True` and `Integer(12) == Integer(12)` is `False`.
64
+
* The `==` comparing lists of objects, that also applies to other types of collections.
65
+
* The `in` operator also depends on the behavior of the `__eq__` method
66
+
* The `is` operator that checks the references point to the same object regardless of the stored value.
# And this is equally this will always be False as well
29
86
print(Integer(12) in [Integer(10), Integer(12)])
87
+
# The 'is' will return True only if both references point to the same object
88
+
a = Integer(12)
89
+
b = a
90
+
# Here, a and b point to the same Integer, so 'is' returns True
91
+
print(a is b)
92
+
93
+
b = Integer(12)
94
+
# Even though b still points to an Integer of the same value, it is a new object, so 'is' returns False
95
+
print(a is b)
96
+
97
+
```
98
+
99
+
**Output of noncompliant01.py:**
30
100
101
+
```bash
102
+
False
103
+
False
104
+
False
105
+
True
106
+
False
31
107
```
32
108
33
109
## Compliant Solution
34
110
35
-
In this compliant solution the `__eq__` method is implemented and all the comparisons now correctly compares the object values, rather than the object reference.
111
+
In this compliant solution, the `__eq__` method is implemented and the comparisons that not use `is`now correctly compare the object values, rather than the object reference. The `is` operator does not call `__eq__`, hence the last print will still display `False`.
36
112
37
-
[*compliant01.py:*](compliant01.py)
113
+
_[compliant01.py:](compliant01.py)_
38
114
39
115
```py
40
116
""" Compliant Code Example """
41
-
42
-
117
+
118
+
43
119
classInteger:
44
120
def__init__(self, value):
45
121
self.value = value
46
-
122
+
47
123
def__eq__(self, other):
48
124
ifisinstance(other, type(self)):
49
125
returnself.value == other.value
50
126
ifisinstance(other, int):
51
127
returnself.value == other
52
128
returnFalse
53
-
54
-
129
+
130
+
55
131
#####################
56
132
# exploiting above code example
57
133
#####################
58
134
# All these scenarios will now show True
59
135
print(Integer(12) == Integer(12))
60
136
print([Integer(12)] == [Integer(12)])
61
137
print(Integer(12) in [Integer(10), Integer(12)])
62
-
138
+
63
139
# By adding the handling for int we also support
64
140
print(Integer(12) ==12)
141
+
# The 'is' will return True only if both references point to the same object
142
+
a = Integer(12)
143
+
b = a
144
+
# Here, a and b point to the same Integer, so 'is' returns True
145
+
print(a is b)
146
+
147
+
b = Integer(12)
148
+
# Since the 'is' operator does not call __eq__, print below will still return False
149
+
print(a is b)
150
+
151
+
```
152
+
153
+
**Output of compliant01.py:**
65
154
155
+
```bash
156
+
True
157
+
True
158
+
True
159
+
True
160
+
True
161
+
False
66
162
```
67
163
68
164
## Automated Detection
@@ -86,3 +182,5 @@ print(Integer(12) == 12)
86
182
|[[python.org data model 2023](https://docs.python.org/3/reference/datamodel.html?highlight=__eq__#object.__eq__)]|[3. Data model — Python 3.11.3 documentation](https://docs.python.org/3/reference/datamodel.html?highlight=__eq__#object.__eq__)|
87
183
|[[de Langen 2023](https://realpython.com/python-is-identity-vs-equality/)]|[Python '!=' Is Not 'is not': Comparing Objects in Python – Real Python](https://realpython.com/python-is-identity-vs-equality/)|
0 commit comments