Skip to content

Commit

Permalink
Close the __data__ attribute before reassigning it
Browse files Browse the repository at this point in the history
Fix #356
  • Loading branch information
adang1345 committed Feb 18, 2023
1 parent 1ab2807 commit 5774143
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
- "3.8"
- "3.9"
- "3.10"
- "pypy3.9"
include:
- os: ubuntu-latest
python-version: "3.10"
Expand Down
13 changes: 10 additions & 3 deletions pefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2903,7 +2903,7 @@ def __enter__(self):
def __exit__(self, type, value, traceback):
self.close()

def close(self):
def _close_data(self):
if (
self.__from_file is True
and hasattr(self, "__data__")
Expand All @@ -2915,6 +2915,9 @@ def close(self):
self.__data__.close()
del self.__data__

def close(self):
self._close_data()

def __unpack_data__(self, format, data, file_offset):
"""Apply structure format to raw data.
Expand Down Expand Up @@ -7332,7 +7335,9 @@ def set_bytes_at_offset(self, offset, data):

def set_data_bytes(self, offset: int, data: bytes):
if not isinstance(self.__data__, bytearray):
self.__data__ = bytearray(self.__data__)
new_data = bytearray(self.__data__)
self._close_data()
self.__data__ = new_data

self.__data__[offset : offset + len(data)] = data

Expand Down Expand Up @@ -7590,7 +7595,9 @@ def generate_checksum(self):
# print('Idx: {0} G {1:02x} {3} B {2:02x}'.format(
# idx, ord(self.__data__[idx]), b,
# self.__data__[idx], chr(b)))
self.__data__ = self.write()
new_data = self.write()
self._close_data()
self.__data__ = new_data

# Get the offset to the CheckSum field in the OptionalHeader
# (The offset is the same in PE32 and PE32+)
Expand Down

0 comments on commit 5774143

Please sign in to comment.