Skip to content

Fix panic when using negative int for bytes() #1418

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 1 commit into from
Sep 26, 2019
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/snippets/basic_types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from testutils import assert_raises


# Spec: https://docs.python.org/2/library/types.html
print(None)
# TypeType
Expand Down Expand Up @@ -36,6 +39,9 @@
except TypeError:
pass

with assert_raises(ValueError):
bytes(-1)

a = bytearray([1, 2, 3])
# assert a[1] == 2

Expand Down
4 changes: 3 additions & 1 deletion vm/src/obj/objbyteinner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ impl ByteInnerNewOptions {
let value = if let OptionalArg::Present(ival) = self.val_option {
match_class!(match ival.clone() {
i @ PyInt => {
let size = objint::get_value(&i.into_object()).to_usize().unwrap();
let size = objint::get_value(&i.into_object())
.to_usize()
.ok_or_else(|| vm.new_value_error("negative count".to_string()))?;
Ok(vec![0; size])
}
_l @ PyString => {
Expand Down