Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
piskvorky opened this issue Sep 26, 2023 · 2 comments
Open

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

piskvorky opened this issue Sep 26, 2023 · 2 comments

Comments

@piskvorky
Copy link

piskvorky commented Sep 26, 2023

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.

@nolze
Copy link
Owner

nolze commented Sep 27, 2023

Thank you for reporting! This bug doesn't seem easy, and I can't think of a way to debug it without a sample. However, if I figure something out in the future, I will make fixes. Any information is also welcome.

(A quick possible fix would be changing if not h: to if not h or len(h) < 4:, but I'm not sure if it is consistent with the spec.)

@piskvorky
Copy link
Author

piskvorky commented Sep 27, 2023

Thank you.

Yes, if len(h) < 4: might be a solution. Depending on what happens after that return False.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants