Skip to content

Fix leak on double DatePeriod::__construct() call#22643

Open
iluuu1994 wants to merge 1 commit into
php:PHP-8.4from
iluuu1994:dateperiod-double-constructor-leak
Open

Fix leak on double DatePeriod::__construct() call#22643
iluuu1994 wants to merge 1 commit into
php:PHP-8.4from
iluuu1994:dateperiod-double-constructor-leak

Conversation

@iluuu1994

@iluuu1994 iluuu1994 commented Jul 8, 2026

Copy link
Copy Markdown
Member

See https://github.com/php/php-src/actions/runs/28911263674/job/85769105449.

Normally we might want to discuss whether such code is even sensible, but it seems carbon is relying on it, as evidenced by the CI failure above. A minimal carbon example that reproduces the error:

<?php

use Carbon\Carbon;

require __DIR__ . '/vendor/autoload.php';

$day = Carbon::now();
$period = Carbon::now()->toPeriod($day);

Just remembered, will have to rebase onto 8.2 to keep nightly green.

@iluuu1994 iluuu1994 requested a review from derickr as a code owner July 8, 2026 17:21
@iluuu1994 iluuu1994 requested a review from TimWolla July 8, 2026 17:21

@TimWolla TimWolla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just looked at the diff, didn't check for completeness. I trust you there.

Comment thread ext/date/php_date.c
if (period_obj->interval) {
timelib_rel_time_dtor(period_obj->interval);
}
memset(period_obj, 0, XtOffsetOf(php_period_obj, std));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
memset(period_obj, 0, XtOffsetOf(php_period_obj, std));
memset(period_obj, 0, offsetof(php_period_obj, std));

We only removed XtOffsetOf with 8.6, but offsetof() is reliably available even for 8.4.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I remembered your change. Will have to be backported to 8.2, so I'd prefer just changing on master to match your other PR.

@iluuu1994

Copy link
Copy Markdown
Member Author

@kylekatarnls I hope it's ok to ping you directly. Just wanted to check with you whether the double constructor call is even intentional. This issue popped up recently in our CI, so I figured it might not be.

@kylekatarnls

kylekatarnls commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Hello, indeed I got this change submitted to us recently: CarbonPHP/carbon@9227630

And merged it, as you can read from the description, the goal is to update the native properties when the period get modified and __construct can actually do it in PHP 8.5.

We're considering CarbonPeriod mutable and have CarbonPeriodImmutable as an immutable version, DatePeriod becoming immutable (since 8.0 IIRC) made CarbonPeriod properties no longer compatible with the parent.

So I guess since the construct is skipped, the effect of the change above will be lost. At some point, we might have to drop mutable objects all at once at a certain PHP version.

@iluuu1994

Copy link
Copy Markdown
Member Author

@kylekatarnls Thank you for the explanation.

So I guess since the construct is skipped, the effect of the change above will be lost. At some point, we might have to drop mutable objects all at once at a certain PHP version.

Clarification: This PR fixes the leak, but does not change behavior (in the regular case at least, when the constructor call succeeds). So your fix should keep working.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants