-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Raise ValueError if \x00 character exists for eval argument
#4052
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
Changes from 6 commits
56f08a1
bb8526a
c7689c9
20e7752
7e88c8f
6d01742
0bd702d
714ce4d
879ed6e
c672d8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -248,11 +248,35 @@ mod builtins { | |
| #[cfg(feature = "rustpython-compiler")] | ||
| #[pyfunction] | ||
| fn eval( | ||
| source: Either<PyStrRef, PyRef<crate::builtins::PyCode>>, | ||
| source: Either< | ||
| Either<PyStrRef, crate::builtins::PyBytesRef>, | ||
| PyRef<crate::builtins::PyCode>, | ||
| >, | ||
| scope: ScopeArgs, | ||
| vm: &VirtualMachine, | ||
| ) -> PyResult { | ||
| run_code(vm, source, scope, compile::Mode::Eval, "eval") | ||
| // source as string | ||
| let code = match source { | ||
| Either::A(either) => { | ||
| let source: &[u8] = match either { | ||
| Either::A(ref a) => a.as_str().as_bytes(), | ||
| Either::B(ref b) => b.as_bytes(), | ||
| }; | ||
| for x in source { | ||
| if *x == 0 { | ||
| return Err(vm.new_value_error( | ||
| "source code string cannot contain null bytes".to_owned(), | ||
| )); | ||
| } | ||
| } | ||
|
moreal marked this conversation as resolved.
Outdated
|
||
|
|
||
| Ok(Either::A( | ||
| vm.ctx.new_str(std::str::from_utf8(source).unwrap()), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It causes eval(b'\xff')And it prints: Because the // _PyPegen_run_parser_from_string
if (flags != NULL && flags->cf_flags & PyCF_IGNORE_COOKIE) {
tok = PyTokenizer_FromUTF8(str, exec_input);
} else {
tok = PyTokenizer_FromString(str, exec_input);
}
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I thought was
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I agree with your thought. 🙏🏻 I'll work for wrapping the decode error.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you execute I set After the commit, in RustPython: |
||
| )) | ||
| } | ||
| Either::B(code) => Ok(Either::B(code)), | ||
| }?; | ||
| run_code(vm, code, scope, compile::Mode::Eval, "eval") | ||
| } | ||
|
|
||
| /// Implements `exec` | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this exactly (
str|bytes) orArgStrOrBytesLike?ArgStrOrBytesLikeaccepts more types likebytearray. Some functions take exactlybytes, while other functions take alsobytearrayThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I run rustpython with
ArgStrOrBytesLike, it seems very suit for this case.string, bytes or codeis just error message and theevalalso receivesbytearrayBuffer🤔 . So I'll push a commit applyArgStrOrBytesLike. Thanks! 🙏🏻