1
0
forked from Mirror/wren

Disallow non-Num arguments in Num for min(_), max(_), clamp(_,_), pow(_) and atan(_) (#859)

* Disallow non-Num arguments in `Num.min(_)`, `Num.max(_)`, `Num.clamp(_,_)`

Previously this was an Undefined Behavior

* also validate args for pow, atan2, add tests, fix error messages

Co-authored-by: ruby0x1 <ruby0x1@pm.me>
This commit is contained in:
Chayim Refael Friedman
2021-04-08 08:04:58 +03:00
committed by GitHub
parent 4847b37789
commit 68f5c096d8
7 changed files with 18 additions and 0 deletions

View File

@ -744,11 +744,15 @@ DEF_PRIMITIVE(num_dotDotDot)
DEF_PRIMITIVE(num_atan2)
{
if (!validateNum(vm, args[1], "x value")) return false;
RETURN_NUM(atan2(AS_NUM(args[0]), AS_NUM(args[1])));
}
DEF_PRIMITIVE(num_min)
{
if (!validateNum(vm, args[1], "Other value")) return false;
double value = AS_NUM(args[0]);
double other = AS_NUM(args[1]);
RETURN_NUM(value <= other ? value : other);
@ -756,6 +760,8 @@ DEF_PRIMITIVE(num_min)
DEF_PRIMITIVE(num_max)
{
if (!validateNum(vm, args[1], "Other value")) return false;
double value = AS_NUM(args[0]);
double other = AS_NUM(args[1]);
RETURN_NUM(value > other ? value : other);
@ -763,6 +769,9 @@ DEF_PRIMITIVE(num_max)
DEF_PRIMITIVE(num_clamp)
{
if (!validateNum(vm, args[1], "Min value")) return false;
if (!validateNum(vm, args[2], "Max value")) return false;
double value = AS_NUM(args[0]);
double min = AS_NUM(args[1]);
double max = AS_NUM(args[2]);
@ -772,6 +781,8 @@ DEF_PRIMITIVE(num_clamp)
DEF_PRIMITIVE(num_pow)
{
if (!validateNum(vm, args[1], "Power value")) return false;
RETURN_NUM(pow(AS_NUM(args[0]), AS_NUM(args[1])));
}

View File

@ -0,0 +1 @@
0.atan(false) // expect runtime error: x value must be a number.

View File

@ -0,0 +1 @@
1.clamp(0, false) // expect runtime error: Max value must be a number.

View File

@ -0,0 +1 @@
1.clamp(false, 2) // expect runtime error: Min value must be a number.

View File

@ -0,0 +1 @@
1.max(false) // expect runtime error: Other value must be a number.

View File

@ -0,0 +1 @@
1.min(false) // expect runtime error: Other value must be a number.

View File

@ -0,0 +1,2 @@
1.pow(false) // expect runtime error: Power value must be a number.