diff options
author | Ted Pudlik <tpudlik@google.com> | 2022-11-15 00:47:37 +0000 |
---|---|---|
committer | CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-11-15 00:47:37 +0000 |
commit | fce539a9f31933ec69a148beef7257b5bd630156 (patch) | |
tree | 6d71794f8f599ca3d166e736aab3cbdb907749da /pw_allocator/py | |
parent | af4b887230dfa2b857ae9b5ffdd26968d9e0e954 (diff) | |
download | pigweed-fce539a9f31933ec69a148beef7257b5bd630156.tar.gz |
pw_allocator: Remove mutable default value
Avoid unintentional sharing of the default value between instances
of HeapUsage. This has always been a bug, but will become a runtime
exception in Python 3.11
(https://docs.python.org/3/library/dataclasses.html#mutable-default-values,
https://stackoverflow.com/q/53632152/1224002).
Bug: 259121920
Change-Id: I0315a917316ae9f3bcf59e1015446f572dbbb7b5
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/119312
Reviewed-by: Alexei Frolov <frolv@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Pigweed-Auto-Submit: Ted Pudlik <tpudlik@google.com>
Diffstat (limited to 'pw_allocator/py')
-rw-r--r-- | pw_allocator/py/pw_allocator/heap_viewer.py | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/pw_allocator/py/pw_allocator/heap_viewer.py b/pw_allocator/py/pw_allocator/heap_viewer.py index 56e539674..324fee07f 100644 --- a/pw_allocator/py/pw_allocator/heap_viewer.py +++ b/pw_allocator/py/pw_allocator/heap_viewer.py @@ -18,7 +18,7 @@ import sys import math import logging from typing import Optional -from dataclasses import dataclass +from dataclasses import dataclass, field import coloredlogs # type: ignore @@ -33,7 +33,9 @@ class HeapBlock: @dataclass class HeapUsage: """Contains a linked list of allocated HeapBlocks.""" - begin: HeapBlock = HeapBlock(0, 0) + # Use a default_factory to avoid mutable default value. See + # https://docs.python.org/3/library/dataclasses.html#mutable-default-values + begin: HeapBlock = field(default_factory=lambda: HeapBlock(0, 0)) def add_block(self, block): cur_block = self.begin.next |