Skip to content

ext/intl: reducing raw pointer usage in C++ part, using smart ones in… #19179

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

Merged
merged 1 commit into from
Jul 20, 2025
Merged
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
45 changes: 22 additions & 23 deletions ext/intl/calendar/gregoriancalendar_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static void _php_intlgregcal_constructor_body(

// instantion of ICU object
Calendar_object *co = Z_INTL_CALENDAR_P(return_value);
GregorianCalendar *gcal = NULL;
std::unique_ptr<GregorianCalendar> gcal;

if (co->ucal) {
zend_throw_error(NULL, "IntlGregorianCalendar object is already constructed");
Expand All @@ -159,15 +159,12 @@ static void _php_intlgregcal_constructor_body(
locale = const_cast<char*>(intl_locale_get_default());
}

gcal = new GregorianCalendar(tz, Locale::createFromName(locale),
status);
gcal = std::unique_ptr<GregorianCalendar>(new GregorianCalendar(tz, Locale::createFromName(locale),
status));
// Should this throw?
if (U_FAILURE(status)) {
intl_error_set(NULL, status, "intlgregcal_create_instance: error "
"creating ICU GregorianCalendar from time zone and locale", 0);
if (gcal) {
delete gcal;
}
delete tz;
if (!is_constructor) {
zval_ptr_dtor(return_value);
Expand All @@ -177,26 +174,28 @@ static void _php_intlgregcal_constructor_body(
}
} else {
// From date/time (3, 5 or 6 arguments)
GregorianCalendar *tmp;
for (int i = 0; i < variant; i++) {
ZEND_VALUE_ERROR_OUT_OF_BOUND_VALUE(largs[i], hasThis() ? (i-1) : i);
}

if (variant == 3) {
gcal = new GregorianCalendar((int32_t)largs[0], (int32_t)largs[1],
tmp = new GregorianCalendar((int32_t)largs[0], (int32_t)largs[1],
(int32_t)largs[2], status);
} else if (variant == 5) {
gcal = new GregorianCalendar((int32_t)largs[0], (int32_t)largs[1],
tmp = new GregorianCalendar((int32_t)largs[0], (int32_t)largs[1],
(int32_t)largs[2], (int32_t)largs[3], (int32_t)largs[4], status);
} else if (variant == 6) {
gcal = new GregorianCalendar((int32_t)largs[0], (int32_t)largs[1],
tmp = new GregorianCalendar((int32_t)largs[0], (int32_t)largs[1],
(int32_t)largs[2], (int32_t)largs[3], (int32_t)largs[4], (int32_t)largs[5],
status);
} else {
ZEND_UNREACHABLE();
}

if (!set_gregorian_calendar_time_zone(gcal, status)) {
delete gcal;
gcal = std::unique_ptr<GregorianCalendar>(tmp);

if (!set_gregorian_calendar_time_zone(gcal.get(), status)) {
if (!is_constructor) {
zval_ptr_dtor(return_value);
RETVAL_NULL();
Expand All @@ -205,7 +204,7 @@ static void _php_intlgregcal_constructor_body(
}
}

co->ucal = gcal;
co->ucal = gcal.release();
}

U_CFUNC PHP_FUNCTION(intlgregcal_create_instance)
Expand Down Expand Up @@ -234,7 +233,7 @@ U_CFUNC PHP_METHOD(IntlGregorianCalendar, createFromDate)
UErrorCode status = U_ZERO_ERROR;
zend_error_handling error_handling;
Calendar_object *co;
GregorianCalendar *gcal;
std::unique_ptr<GregorianCalendar> gcal;

intl_error_reset(NULL);

Expand All @@ -250,15 +249,14 @@ U_CFUNC PHP_METHOD(IntlGregorianCalendar, createFromDate)

zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);

gcal = new GregorianCalendar((int32_t) year, (int32_t) month, (int32_t) day, status);
if (!set_gregorian_calendar_time_zone(gcal, status)) {
delete gcal;
gcal = std::unique_ptr<GregorianCalendar>(new GregorianCalendar((int32_t) year, (int32_t) month, (int32_t) day, status));
if (!set_gregorian_calendar_time_zone(gcal.get(), status)) {
goto cleanup;
}

object_init_ex(return_value, GregorianCalendar_ce_ptr);
co = Z_INTL_CALENDAR_P(return_value);
co->ucal = gcal;
co->ucal = gcal.release();

cleanup:
zend_restore_error_handling(&error_handling);
Expand All @@ -271,7 +269,7 @@ U_CFUNC PHP_METHOD(IntlGregorianCalendar, createFromDateTime)
UErrorCode status = U_ZERO_ERROR;
zend_error_handling error_handling;
Calendar_object *co;
GregorianCalendar *gcal;
GregorianCalendar *tmp;

intl_error_reset(NULL);

Expand All @@ -294,19 +292,20 @@ U_CFUNC PHP_METHOD(IntlGregorianCalendar, createFromDateTime)
zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);

if (second_is_null) {
gcal = new GregorianCalendar((int32_t) year, (int32_t) month, (int32_t) day, (int32_t) hour, (int32_t) minute, status);
tmp = new GregorianCalendar((int32_t) year, (int32_t) month, (int32_t) day, (int32_t) hour, (int32_t) minute, status);
} else {
ZEND_VALUE_ERROR_OUT_OF_BOUND_VALUE(second, 6);
gcal = new GregorianCalendar((int32_t) year, (int32_t) month, (int32_t) day, (int32_t) hour, (int32_t) minute, (int32_t) second, status);
tmp = new GregorianCalendar((int32_t) year, (int32_t) month, (int32_t) day, (int32_t) hour, (int32_t) minute, (int32_t) second, status);
}
if (!set_gregorian_calendar_time_zone(gcal, status)) {
delete gcal;
auto gcal = std::unique_ptr<GregorianCalendar>(tmp);
if (!set_gregorian_calendar_time_zone(gcal.get(), status)) {
goto cleanup;
}

object_init_ex(return_value, GregorianCalendar_ce_ptr);
co = Z_INTL_CALENDAR_P(return_value);
co->ucal = gcal;
// TODO: trying to get passed the ownership change step
Copy link
Member

Choose a reason for hiding this comment

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

What do you mean by this?

Copy link
Member Author

Choose a reason for hiding this comment

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

if I can, changing the structure so I do not need to pass the ownership.

Copy link
Member

Choose a reason for hiding this comment

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

I see. I suppose that would be preferable

co->ucal = gcal.release();

cleanup:
zend_restore_error_handling(&error_handling);
Expand Down