Skip to content

Commit ecfaed3

Browse files
committed
add boundary check
1 parent 5e90531 commit ecfaed3

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

crates/vm/src/builtins/code.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,16 @@ impl PyCode {
358358
vm: &VirtualMachine,
359359
) -> PyResult<PyRef<Self>> {
360360
if !crate::import::check_pyc_magic_number_bytes(pyc_bytes) {
361-
return Err(vm.new_runtime_error("pyc bytes has wrong MAGIC"));
361+
return Err(vm.new_value_error("pyc bytes has wrong MAGIC"));
362362
}
363363
let bootstrap_external = vm.import("_frozen_importlib_external", 0)?;
364364
let compile_bytecode = bootstrap_external.get_attr("_compile_bytecode", vm)?;
365-
let code_bytes = &pyc_bytes[16..];
365+
let Some((_, code_bytes)) = pyc_bytes.split_at_checked(16) else {
366+
return Err(vm.new_value_error(format!(
367+
"pyc_bytes header is broken. 16 bytes expected but {} bytes given.",
368+
pyc_bytes.len()
369+
)));
370+
};
366371
let code_bytes_obj = vm.ctx.new_bytes(code_bytes.to_vec());
367372
let compiled =
368373
compile_bytecode.call((code_bytes_obj, name, bytecode_path, source_path), vm)?;

crates/vm/src/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
};
1010

1111
pub(crate) fn check_pyc_magic_number_bytes(buf: &[u8]) -> bool {
12-
buf[..2] == crate::version::PYC_MAGIC_NUMBER_BYTES[..2]
12+
buf.starts_with(&crate::version::PYC_MAGIC_NUMBER_BYTES[..2])
1313
}
1414

1515
pub(crate) fn init_importlib_base(vm: &mut VirtualMachine) -> PyResult<PyObjectRef> {

0 commit comments

Comments
 (0)