Skip to content

Commit 5f899e9

Browse files
committed
Handle Int64 comparisons with other zvals in PHP 7.4
1 parent e52355e commit 5f899e9

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

src/BSON/Int64.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,49 @@ static int php_phongo_int64_compare_objects(zval* o1, zval* o2)
346346
}
347347

348348
#if PHP_VERSION_ID < 80000
349+
static int php_phongo_int64_compare_with_other_type(zval* object, zval* value)
350+
{
351+
zval tmp_value;
352+
zval result;
353+
int ret;
354+
355+
if (Z_OBJ_HT_P(object)->cast_object(object, &tmp_value, ((Z_TYPE_P(value) == IS_FALSE || Z_TYPE_P(value) == IS_TRUE) ? _IS_BOOL : Z_TYPE_P(value))) == FAILURE) {
356+
zval_ptr_dtor(&tmp_value);
357+
return 1;
358+
}
359+
360+
compare_function(&result, &tmp_value, value);
361+
362+
ret = Z_LVAL(result);
363+
zval_ptr_dtor(&tmp_value);
364+
zval_ptr_dtor(&result);
365+
366+
return ret;
367+
}
368+
349369
static int php_phongo_int64_compare_zvals(zval* result, zval* op1, zval* op2)
350370
{
351-
ZVAL_LONG(result, php_phongo_int64_compare_objects(op1, op2));
371+
/* Happy case: compare an int64 object with another object, long, or double */
372+
if ((php_phongo_int64_is_int64_object(op1) || php_phongo_int64_is_long_or_double(op1)) && (php_phongo_int64_is_int64_object(op2) || php_phongo_int64_is_long_or_double(op2))) {
373+
ZVAL_LONG(result, php_phongo_int64_compare_objects(op1, op2));
374+
return SUCCESS;
375+
}
376+
377+
/* When comparing an int64 object with any other type, cast the int64 object to the desired type.
378+
* We know that if op1 is an object, op2 has to be the other type and vice versa. For op2 being
379+
* the object, we can again flip the values used for comparison and multiply the result with -1. */
380+
381+
if (php_phongo_int64_is_int64_object(op1)) {
382+
ZVAL_LONG(result, php_phongo_int64_compare_with_other_type(op1, op2));
383+
return SUCCESS;
384+
}
385+
386+
if (php_phongo_int64_is_int64_object(op2)) {
387+
ZVAL_LONG(result, -1 * php_phongo_int64_compare_with_other_type(op2, op1));
388+
return SUCCESS;
389+
}
352390

353-
return SUCCESS;
391+
return FAILURE;
354392
}
355393
#endif
356394

0 commit comments

Comments
 (0)