From 16559ce4f36b9a92f14aa618863b5f485bff66b4 Mon Sep 17 00:00:00 2001 From: Alexander Borisov Date: Tue, 19 Mar 2019 18:26:44 +0300 Subject: [PATCH] Fixed Math.max() for undefined arguments. --- njs/njs_math.c | 8 +++++++- njs/test/njs_unit_test.c | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/njs/njs_math.c b/njs/njs_math.c index 31a8f11b..e96f562e 100644 --- a/njs/njs_math.c +++ b/njs/njs_math.c @@ -502,7 +502,11 @@ njs_object_math_max(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, if (nargs > 1) { for (i = 1; i < nargs; i++) { - if (!njs_is_numeric(&args[i])) { + if (njs_is_undefined(&args[i])) { + num = NAN; + goto done; + + } else if (!njs_is_numeric(&args[i])) { njs_vm_trap_value(vm, &args[i]); return njs_trap(vm, NJS_TRAP_NUMBER_ARG); @@ -519,6 +523,8 @@ njs_object_math_max(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, num = -INFINITY; } +done: + njs_value_number_set(&vm->retval, num); return NXT_OK; diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index 8edae9c0..e11a5054 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -10028,6 +10028,18 @@ static njs_unit_test_t njs_test[] = { nxt_string("Math.max('1', '2', '5')"), nxt_string("5") }, + { nxt_string("Math.max(5, {valueOf: function () {return 10}}, 6)"), + nxt_string("10") }, + + { nxt_string("Math.max(5, {valueOf: function () {return 10}}, 20)"), + nxt_string("20") }, + + { nxt_string("Math.max(5, undefined, 20)"), + nxt_string("NaN") }, + + { nxt_string("Math.max(-10, null, -30)"), + nxt_string("0") }, + { nxt_string("Math.min()"), nxt_string("Infinity") }, -- 2.47.3