Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 10 commits into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ def test_verbose(self):
rc, out, err = assert_python_ok('-vv')
self.assertNotIn(b'stack overflow', err)

# TODO: RUSTPYTHON
@unittest.expectedFailure
@unittest.skipIf(interpreter_requires_environment(),
'Cannot run -E tests when PYTHON env vars are required.')
def test_xoptions(self):
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,8 +761,6 @@ def test_env(self):
stdout, stderr = p.communicate()
self.assertEqual(stdout, b"orange")

# TODO: RUSTPYTHON
@unittest.expectedFailure
# Windows requires at least the SYSTEMROOT environment variable to start
# Python
@unittest.skipIf(sys.platform == 'win32',
Expand Down
4 changes: 1 addition & 3 deletions extra_tests/snippets/syntax_non_utf8.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

dir_path = os.path.dirname(os.path.realpath(__file__))

# TODO: RUSTPYTHON, RustPython raises a SyntaxError here, but cpython raise a ValueError
error = SyntaxError if platform.python_implementation() == 'RustPython' else ValueError
with assert_raises(error):
with assert_raises(ValueError):
with open(os.path.join(dir_path , "non_utf8.txt")) as f:
eval(f.read())
31 changes: 27 additions & 4 deletions vm/src/stdlib/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ mod builtins {
format::call_object_format,
function::Either,
function::{
ArgBytesLike, ArgCallable, ArgIntoBool, ArgIterable, ArgMapping, FuncArgs, KwArgs,
OptionalArg, OptionalOption, PosArgs, PyArithmeticValue,
ArgBytesLike, ArgCallable, ArgIntoBool, ArgIterable, ArgMapping, ArgStrOrBytesLike,
FuncArgs, KwArgs, OptionalArg, OptionalOption, PosArgs, PyArithmeticValue,
},
protocol::{PyIter, PyIterReturn},
py_io,
Expand Down Expand Up @@ -248,11 +248,34 @@ mod builtins {
#[cfg(feature = "rustpython-compiler")]
#[pyfunction]
fn eval(
source: Either<PyStrRef, PyRef<crate::builtins::PyCode>>,
source: Either<ArgStrOrBytesLike, 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] = &either.borrow_bytes();
if source.contains(&0) {
return Err(vm.new_value_error(
"source code string cannot contain null bytes".to_owned(),
));
}

let source = std::str::from_utf8(source).map_err(|err| {
let msg = format!(
"(unicode error) 'utf-8' codec can't decode byte 0x{:x?} in position {}: invalid start byte",
source[err.valid_up_to()],
err.valid_up_to()
);

vm.new_exception_msg(vm.ctx.exceptions.syntax_error.to_owned(), msg)
})?;
Ok(Either::A(vm.ctx.new_str(source)))
}
Either::B(code) => Ok(Either::B(code)),
}?;
run_code(vm, code, scope, compile::Mode::Eval, "eval")
}

/// Implements `exec`
Expand Down