Skip to content

Time\Duration::__unserialize() accepts a "negative zero" payload. #24505

Closed

Description

@azjezz

Description

The following code:

<?php

$payload = 'O:13:"Time\Duration":3:{s:7:"seconds";i:0;s:11:"nanoseconds";i:0;s:8:"negative";b:1;}';
$d = unserialize($payload);

var_dump($d->negative); // expected: exception, or false. actual: true
var_dump(Time\Duration::compare($d, Time\Duration::fromSeconds(0))); // int(0)
var_dump($d->absolute()->negative); // actual: true (absolute() of a value is still "negative")
echo json_encode($d), "\n"; // actual: {"seconds":0,"nanoseconds":0,"negative":true}
echo serialize(Time\Duration::fromSeconds(0)->negate()), "\n"; // canonical zero: ...s:8:"negative";b:0;}

// For comparison, other non-canonical payloads are rejected:
try {
    unserialize('O:13:"Time\Duration":3:{s:7:"seconds";i:-1;s:11:"nanoseconds";i:0;s:8:"negative";b:0;}');
} catch (Exception $e) {
    echo get_class($e), ': ', $e->getMessage(), "\n";
}

Expected: an exception ("Invalid serialization data for Time\Duration object"), as for the other non-canonical payloads (negative seconds, nanoseconds >= 1e9), or a canonicalized zero with $negative === false. No public factory or arithmetic method can produce a zero duration with $negative === true.

Actual: the object is created with seconds = 0, nanoseconds = 0, negative = true. compare() and == treat it as zero, but the public $negative property, json_encode(), serialize() and absolute() all expose the non-canonical sign.

PHP Version

PHP 8.6.0RC3 (cli) (built: Oct 20 2026 18:44:56) (NTS)
Copyright © The PHP Group and Contributors
Zend Engine v4.6.0RC3, Copyright © Zend by Perforce
    with Zend OPcache v8.6.0RC3, Copyright ©, by Zend by Perforce

Operating System

MacOS

Activity

  1. NickSdot commented on Oct 22, 2026

    @NickSdot
    Contributor

    Heads up, wrong info sneaked into the "PHP Version" section.

  2. azjezz commented on Oct 22, 2026

    @azjezz
    ContributorAuthor

    @NickSdot Thanks! fixed.

  3. TimWolla commented on Oct 23, 2026

    @TimWolla
    Member

    Should already be fixed with
    #24356.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions