php-src icon indicating copy to clipboard operation
php-src copied to clipboard

DateTime doesn't correctly parse PHP_INT_MIN timestamp

Open rybakit opened this issue 4 years ago • 1 comments

Description

The following code:

<?php

var_dump(PHP_INT_MIN);
var_dump((new DateTime("@-9223372036854775808"))->getTimestamp());
var_dump((new DateTime('-292277022657-01-27 08:29:52 UTC'))->getTimestamp());

Resulted in this output:

// Output for 8.1.0 - 8.1.2
int(-9223372036854775808)
int(-9223372036854775807) // wrong
int(-9223372036854775808)

// Output for 8.0.1 - 8.0.15
int(-9223372036854775808)
int(-922337203683200587) // wrong
int(-922337203683200588) // wrong

// Output for 7.4.11 - 7.4.27
int(-9223372036854775808)
int(-922337203683200587) // wrong
Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string (-292277022657-01-27 08:29:52 UTC) at position 11 (5): Double date specification ...

But I expected this output instead:

int(-9223372036854775808)
int(-9223372036854775808)
int(-9223372036854775808)

See https://3v4l.org/HdgS3.

PHP Version

PHP 7.4, 8.0, 8.1

Operating System

Linux, MacOS

rybakit avatar Jan 25 '22 22:01 rybakit

The totally wrong PHP 8.0 behavior has been fixed in the latest timelib version, so PHP 8.1 yields better results; I don't think this fix will be backported. The "slightly" wrong PHP 8.1 behavior is due to timelib_get_signed_nr() calling timelib_get_nr_ex() to get the actual number (without the sign), and that function calls strtoull(3) which saturates to the largest possible positive value. A possible fix would be to let strtoull(3) parse the sign as well.

Anyhow, this is actually an upstream issue; consider to report there, but see https://github.com/derickr/timelib/issues/108#issuecomment-820349578.

cmb69 avatar Jan 31 '22 17:01 cmb69