Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ ZEND_FRAMELESS_FUNCTION(min, 2)
zend_long lhs_lval = Z_LVAL_P(lhs);

if (EXPECTED(Z_TYPE_P(rhs) == IS_LONG)) {
RETURN_COPY_VALUE(lhs_lval < Z_LVAL_P(rhs) ? lhs : rhs);
RETURN_COPY_VALUE(lhs_lval <= Z_LVAL_P(rhs) ? lhs : rhs);
} else if (Z_TYPE_P(rhs) == IS_DOUBLE && (zend_dval_to_lval_silent((double) lhs_lval) == lhs_lval)) {
/* if lhs_lval can be exactly represented as a double, go to double dedicated code */
lhs_dval = (double) lhs_lval;
Expand All @@ -1224,16 +1224,16 @@ ZEND_FRAMELESS_FUNCTION(min, 2)

if (EXPECTED(Z_TYPE_P(rhs) == IS_DOUBLE)) {
double_compare:
RETURN_COPY_VALUE(lhs_dval < Z_DVAL_P(rhs) ? lhs : rhs);
RETURN_COPY_VALUE(lhs_dval <= Z_DVAL_P(rhs) ? lhs : rhs);
} else if (Z_TYPE_P(rhs) == IS_LONG && (zend_dval_to_lval_silent((double) Z_LVAL_P(rhs)) == Z_LVAL_P(rhs))) {
/* if the value can be exactly represented as a double, use double dedicated code otherwise generic */
RETURN_COPY_VALUE(lhs_dval < (double)Z_LVAL_P(rhs) ? lhs : rhs);
RETURN_COPY_VALUE(lhs_dval <= (double)Z_LVAL_P(rhs) ? lhs : rhs);
} else {
goto generic_compare;
}
} else {
generic_compare:
RETURN_COPY(zend_compare(lhs, rhs) < 0 ? lhs : rhs);
RETURN_COPY(zend_compare(lhs, rhs) <= 0 ? lhs : rhs);
}
}

Expand Down
32 changes: 32 additions & 0 deletions ext/standard/tests/array/gh20221.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
GH-20221 (min()/max() tie-break inconsistently on signed zero via the 2-arg frameless fast path)
--FILE--
<?php

$negZero = -0.0;

// Direct calls with exactly two positional arguments are compiled to the
// frameless min()/max() opcodes, which used to break ties on -0.0 vs 0
// differently from every other min()/max() code path (and from each other).
var_dump(min($negZero, 0));
var_dump(max($negZero, 0));
var_dump(min(0, $negZero));
var_dump(max(0, $negZero));

// The variadic (3+ args) and array forms already resolved ties in favor of
// the first-seen operand; the frameless 2-arg results above must now agree.
var_dump(min($negZero, 0, 0));
var_dump(max($negZero, 0, 0));
var_dump(min([$negZero, 0]));
var_dump(max([$negZero, 0]));

?>
--EXPECT--
float(-0)
float(-0)
int(0)
int(0)
float(-0)
float(-0)
float(-0)
float(-0)
Loading