Skip to content

struct.error: unpack requires a buffer of 4 bytes #83

@piskvorky

Description

@piskvorky
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/msoffcrypto/format/xls97.py", line 644, in is_encrypted
    if not workbook.has_record(recordNameNum["FilePass"]):
  File "/usr/local/lib/python3.10/dist-packages/msoffcrypto/format/xls97.py", line 418, in has_record
    num, size = unpack("<HH", h)
struct.error: unpack requires a buffer of 4 bytes

Unfortunately I cannot share the file that triggers this exception.

But looking at xls97.py line 418, the code assumes that self.data.read(4) returns either exactly 4 bytes or nothing:

def has_record(self, target):
pos = self.data.tell()
while True:
h = self.data.read(4)
if not h:
self.data.seek(pos)
return False
num, size = unpack("<HH", h)
if num == target:
self.data.seek(pos)
return True
else:
self.data.read(size)

I'm not sure what self.data there is but normally read(n) is guaranteed to return at most 4 bytes, not exactly 4 bytes.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions