Skip to content

Commit 00823dd

Browse files
committed
Fix panic when using negative int for bytes()
Fixes RustPython#1401
1 parent 51c3f71 commit 00823dd

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/snippets/basic_types.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from testutils import assert_raises
2+
3+
14
# Spec: https://docs.python.org/2/library/types.html
25
print(None)
36
# TypeType
@@ -36,6 +39,9 @@
3639
except TypeError:
3740
pass
3841

42+
with assert_raises(ValueError):
43+
bytes(-1)
44+
3945
a = bytearray([1, 2, 3])
4046
# assert a[1] == 2
4147

vm/src/obj/objbyteinner.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ impl ByteInnerNewOptions {
117117
let value = if let OptionalArg::Present(ival) = self.val_option {
118118
match_class!(match ival.clone() {
119119
i @ PyInt => {
120-
let size = objint::get_value(&i.into_object()).to_usize().unwrap();
120+
let size = objint::get_value(&i.into_object())
121+
.to_usize()
122+
.ok_or_else(|| vm.new_value_error("negative count".to_string()))?;
121123
Ok(vec![0; size])
122124
}
123125
_l @ PyString => {

0 commit comments

Comments
 (0)