Skip to content

Commit

Permalink
Add safeguarding for unexpected object types
Browse files Browse the repository at this point in the history
  • Loading branch information
Lara Aydin committed Mar 18, 2020
1 parent 5007ef9 commit 7a88334
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions vm/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -1342,12 +1342,29 @@ var builtinClassCommonInstanceMethods = []*BuiltinMethodObject{
Name: "rand",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
aLen := len(args)

switch aLen {
case 0:
return t.vm.initFloatObject(rand.Float64())
case 1:
_, ok := args[0].(*IntegerObject)

if !ok {
return t.vm.InitErrorObject(errors.TypeError, sourceLine, errors.WrongArgumentTypeFormat, classes.StringClass, args[0].Class().Name)
}
return t.vm.InitIntegerObject(rand.Intn(args[0].Value().(int)))
case 2:
_, minOk := args[1].(*IntegerObject)

if !minOk {
return t.vm.InitErrorObject(errors.TypeError, sourceLine, errors.WrongArgumentTypeFormat, classes.StringClass, args[0].Class().Name)
}
_, maxOk := args[1].(*IntegerObject)

if !maxOk {
return t.vm.InitErrorObject(errors.TypeError, sourceLine, errors.WrongArgumentTypeFormat, classes.StringClass, args[1].Class().Name)
}

return t.vm.InitIntegerObject(rand.Intn(args[1].Value().(int)-args[0].Value().(int)+1) + args[0].Value().(int))
default:
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, errors.WrongNumberOfArgument, 2, aLen)
Expand Down
2 changes: 1 addition & 1 deletion vm/class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@ func TestSendMethod(t *testing.T) {
10
end
end
Foo.new.send(:bar)
`, 10},
}
Expand Down

0 comments on commit 7a88334

Please sign in to comment.