Skip to content

Commit a2ff87b

Browse files
Merge pull request RustPython#506 from janczer/add_check_byte_value
Add checking if value more then 255 in bytearraya
2 parents b7dfbeb + b8cb9f7 commit a2ff87b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

tests/snippets/bytearray.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434
a.clear()
3535
assert len(a) == 0
3636

37+
try:
38+
bytearray([400])
39+
except ValueError:
40+
pass
41+
else:
42+
assert False
43+
3744
b = bytearray(b'test')
3845
assert len(b) == 4
3946
b.pop()

vm/src/obj/objbytearray.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
121121
let mut data_bytes = vec![];
122122
for elem in elements.iter() {
123123
let v = objint::to_int(vm, elem, 10)?;
124-
data_bytes.push(v.to_u8().unwrap());
124+
if let Some(i) = v.to_u8() {
125+
data_bytes.push(i);
126+
} else {
127+
return Err(vm.new_value_error("byte must be in range(0, 256)".to_string()));
128+
}
125129
}
126130
data_bytes
127131
// return Err(vm.new_type_error("Cannot construct bytes".to_string()));

0 commit comments

Comments
 (0)