Skip to content

Fix #81500: Interval serialization regression since 7.3.14 / 7.4.2 #7572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 6 additions & 1 deletion ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -4331,7 +4331,12 @@ static zval *date_interval_write_property(zval *object, zval *member, zval *valu
SET_VALUE_FROM_STRUCT(i, "i");
SET_VALUE_FROM_STRUCT(s, "s");
if (strcmp(Z_STRVAL_P(member), "f") == 0) {
obj->diff->us = zval_get_double(value) * 1000000;
double val = zval_get_double(value) * 1000000;
if (val >= 0 && val < 1000000) {
obj->diff->us = val;
} else {
obj->diff->us = -1000000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happen then to the actual value, I ->f is -0.123456 then we just replace it with -1?

}
break;
}
SET_VALUE_FROM_STRUCT(invert, "invert");
Expand Down
16 changes: 16 additions & 0 deletions ext/date/tests/bug81500.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Bug #81500 (Interval serialization regression since 7.3.14 / 7.4.2)
--FILE--
<?php
$interval = new DateInterval('PT1S');
$interval->f = -0.000001;
var_dump($interval->s, $interval->f);

$interval = unserialize(serialize($interval));
var_dump($interval->s, $interval->f);
?>
--EXPECT--
int(1)
int(-1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it int(-1) while we asked for float(-0.000001)? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Negative values are not supposed to be supported; -1 marks invalid µs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry but -1 is not a proper way to mark something as invalid, just throw an exception if it's forbidden. Here it will just become muted and unnoticed then doing calculation with it assuming it's a relevant value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHP 7 behavior just completely feel like the correct behavior, you pass a value to ->f serialize/unserialize and find there what you passed, exactly like any other units still do in PHP 8: https://3v4l.org/r0i54

Why would ->f would be a particular case and proceed this insane any-out-of-range-value to -1 transformation?

int(1)
int(-1)