Skip to content

Commit 6f92483

Browse files
committed
Throw an exception when an invalid comparison operator is passed to version_compare()
1 parent b2dc833 commit 6f92483

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

ext/opcache/Optimizer/zend_func_info.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ static const func_info_t func_infos[] = {
503503
FN("array_key_last", MAY_BE_NULL | MAY_BE_LONG | MAY_BE_STRING),
504504
F1("pos", UNKNOWN_INFO),
505505
F1("assert_options", MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_LONG | MAY_BE_STRING | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_LONG | MAY_BE_ARRAY_OF_STRING | MAY_BE_ARRAY_OF_OBJECT | MAY_BE_OBJECT),
506-
F0("version_compare", MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_TRUE | MAY_BE_LONG),
506+
F0("version_compare", MAY_BE_FALSE | MAY_BE_TRUE | MAY_BE_LONG),
507507
F1("str_rot13", MAY_BE_STRING),
508508
F1("stream_get_filters", MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_LONG | MAY_BE_ARRAY_OF_STRING),
509509
F0("stream_filter_register", MAY_BE_FALSE | MAY_BE_TRUE),
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
--TEST--
2-
Ensures null is returned if versions are compared with invalid operator
2+
Ensures an exception is thrown if versions are compared with an invalid operator
33
--CREDITS--
44
David Stockton - <[email protected]> - i3logix PHP Testfest 2017
55
--FILE--
66
<?php
7-
var_dump(version_compare('1.2', '2.1', '??'));
7+
try {
8+
version_compare('1.2', '2.1', '??');
9+
} catch (ValueError $exception) {
10+
echo $exception->getMessage() . "\n";
11+
}
812
?>
913
--EXPECT--
10-
NULL
14+
Invalid comparison operator

ext/standard/versioning.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ PHP_FUNCTION(version_compare)
237237
if (!strncmp(op, "!=", op_len) || !strncmp(op, "<>", op_len) || !strncmp(op, "ne", op_len)) {
238238
RETURN_BOOL(compare != 0);
239239
}
240-
RETURN_NULL();
240+
241+
zend_value_error("Invalid comparison operator");
241242
}
242243

243244
/* }}} */

0 commit comments

Comments
 (0)