Skip to content

Commit

Permalink
Fix unsafe loading from file buffer (#58)
Browse files Browse the repository at this point in the history
* Fix unsafe loading from file buffer

The numpy documentation states for numpy.frombuffer` that:
> This function creates a view into the original object.
> This should be safe in general, but it may make sense to copy
> the result when the original object is mutable or untrusted.

When the filebuffer is mutable a `ValueError: output array is read-only`
is raised in:
https://github.com/eyurtsev/fcsparser/blob/701000af178e36e0dedb53d409119c25675b9d16/fcsparser/api.py#L590

* Update pyproject.toml

---------

Co-authored-by: Eugene Yurtsev <[email protected]>
  • Loading branch information
felixthebeard and eyurtsev authored Sep 6, 2023
1 parent e3672ff commit 55235c7
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion fcsparser/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,18 @@ def fromfile(file, dtype, count, *args, **kwargs):
file, dtype=",".join(["u1"] * record_width), count=count, *args, **kwargs
)
except (TypeError, IOError):
ret = numpy.frombuffer(
_ret = numpy.frombuffer(
file.read(count * record_width),
dtype=",".join(["u1"] * record_width),
count=count,
*args,
**kwargs
)
# Create a copy of the file content as `numpy.frombuffer`
# returns a view into the original object which is not
# safe for mutable file buffers.
# See https://numpy.org/doc/stable/reference/generated/numpy.frombuffer.html
ret = _ret.copy()

# convert the DATA segment from a 1 x `count` array of records
# (and remember, each record is composed of `record_width`
Expand Down

0 comments on commit 55235c7

Please sign in to comment.