Skip to content

Commit

Permalink
Correctly handle numpy stream
Browse files Browse the repository at this point in the history
  • Loading branch information
seanpmorgan committed Mar 28, 2024
1 parent c8dfa47 commit 99514a3
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions modelscan/tools/picklescanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,11 @@ def scan_numpy(model: Model, settings: Dict[str, Any]) -> ScanResults:
_ZIP_PREFIX = b"PK\x03\x04"
_ZIP_SUFFIX = b"PK\x05\x06" # empty zip files start with this
N = len(np.lib.format.MAGIC_PREFIX)
magic = model.get_stream().read(N)
stream = model.get_stream()
magic = stream.read(N)
# If the file size is less than N, we need to make sure not
# to seek past the beginning of the file
model._stream.seek(-min(N, len(magic)), 1) # back-up
stream.seek(-min(N, len(magic)), 1) # back-up
if magic.startswith(_ZIP_PREFIX) or magic.startswith(_ZIP_SUFFIX):
# .npz file
return ScanResults(
Expand All @@ -208,9 +209,9 @@ def scan_numpy(model: Model, settings: Dict[str, Any]) -> ScanResults:

elif magic == np.lib.format.MAGIC_PREFIX:
# .npy file
version = np.lib.format.read_magic(model._stream) # type: ignore[no-untyped-call]
version = np.lib.format.read_magic(stream) # type: ignore[no-untyped-call]
np.lib.format._check_version(version) # type: ignore[attr-defined]
_, _, dtype = np.lib.format._read_array_header(model._stream, version) # type: ignore[attr-defined]
_, _, dtype = np.lib.format._read_array_header(stream, version) # type: ignore[attr-defined]

if dtype.hasobject:
return scan_pickle_bytes(model, settings, scan_name)
Expand Down

0 comments on commit 99514a3

Please sign in to comment.