Prechádzať zdrojové kódy

Make max() and min() global functions only accept numbers

The behavior for those are not well defined for non-numeric arguments.
To avoid confusion the other types are forbidden.
George Marques 2 rokov pred
rodič
commit
ed81b165eb

+ 14 - 0
core/variant/variant_utility.cpp

@@ -543,6 +543,13 @@ struct VariantUtilityFunctions {
 		Variant base = *p_args[0];
 		Variant ret;
 		for (int i = 1; i < p_argcount; i++) {
+			Variant::Type arg_type = p_args[i]->get_type();
+			if (arg_type != Variant::INT && arg_type != Variant::FLOAT) {
+				r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
+				r_error.expected = Variant::FLOAT;
+				r_error.argument = i;
+				return Variant();
+			}
 			bool valid;
 			Variant::evaluate(Variant::OP_LESS, base, *p_args[i], ret, valid);
 			if (!valid) {
@@ -576,6 +583,13 @@ struct VariantUtilityFunctions {
 		Variant base = *p_args[0];
 		Variant ret;
 		for (int i = 1; i < p_argcount; i++) {
+			Variant::Type arg_type = p_args[i]->get_type();
+			if (arg_type != Variant::INT && arg_type != Variant::FLOAT) {
+				r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
+				r_error.expected = Variant::FLOAT;
+				r_error.argument = i;
+				return Variant();
+			}
 			bool valid;
 			Variant::evaluate(Variant::OP_GREATER, base, *p_args[i], ret, valid);
 			if (!valid) {

+ 2 - 2
doc/classes/@GlobalScope.xml

@@ -656,7 +656,7 @@
 		<method name="max" qualifiers="vararg">
 			<return type="Variant" />
 			<description>
-				Returns the maximum of the given values. This function can take any number of arguments.
+				Returns the maximum of the given numeric values. This function can take any number of arguments.
 				[codeblock]
 				max(1, 7, 3, -6, 5) # Returns 7
 				[/codeblock]
@@ -689,7 +689,7 @@
 		<method name="min" qualifiers="vararg">
 			<return type="Variant" />
 			<description>
-				Returns the minimum of the given values. This function can take any number of arguments.
+				Returns the minimum of the given numeric values. This function can take any number of arguments.
 				[codeblock]
 				min(1, 7, 3, -6, 5) # Returns -6
 				[/codeblock]