Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
LightMotif
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Larralde
LightMotif
Commits
5b3c2c86
Commit
5b3c2c86
authored
6 months ago
by
Martin Larralde
Browse files
Options
Downloads
Patches
Plain Diff
Avoid use of unsafe `OsStr::from_encoded_bytes_unchecked` in `lightmotif-py`
parent
02f682b0
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lightmotif-py/lightmotif/io.rs
+4
-5
4 additions, 5 deletions
lightmotif-py/lightmotif/io.rs
with
4 additions
and
5 deletions
lightmotif-py/lightmotif/io.rs
+
4
−
5
View file @
5b3c2c86
...
...
@@ -9,7 +9,7 @@ use lightmotif_io::error::Error;
use
pyo3
::
exceptions
::
PyOSError
;
use
pyo3
::
exceptions
::
PyValueError
;
use
pyo3
::
prelude
::
*
;
use
pyo3
::
types
::
Py
Bytes
;
use
pyo3
::
types
::
Py
String
;
use
super
::
pyfile
::
PyFileRead
;
use
super
::
CountMatrixData
;
...
...
@@ -163,15 +163,14 @@ pub fn load(file: Bound<PyAny>, format: &str, protein: bool) -> PyResult<Loader>
let
py
=
file
.py
();
let
pathlike
=
py
.import_bound
(
pyo3
::
intern!
(
py
,
"os"
))
?
.call_method1
(
"fse
n
code"
,
(
&
file
,));
.call_method1
(
pyo3
::
intern!
(
py
,
"fs
d
ecode"
)
,
(
&
file
,));
let
b
:
Box
<
dyn
BufRead
+
Send
>
=
if
let
Ok
(
path
)
=
pathlike
{
// NOTE(@althonos): In theory this is safe because `os.fsencode` encodes
// the PathLike object into the OS prefered encoding,
// which is was OsStr wants. In practice, there may be
// some weird bugs if that encoding is incorrect, idk...
let
encoded
=
path
.downcast
::
<
PyBytes
>
()
?
;
let
s
=
unsafe
{
std
::
ffi
::
OsStr
::
from_encoded_bytes_unchecked
(
encoded
.as_bytes
())
};
std
::
fs
::
File
::
open
(
s
)
let
decoded
=
path
.downcast
::
<
PyString
>
()
?
;
std
::
fs
::
File
::
open
(
decoded
.to_str
()
?
)
.map
(
std
::
io
::
BufReader
::
new
)
.map
(
Box
::
new
)
?
}
else
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment