Skip to content

Fix GH-17317: ResourceBundle crash on undefined iterator key. #17318

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 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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/intl/resourcebundle/resourcebundle_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ static void resourcebundle_iterator_key( zend_object_iterator *iter, zval *key )
}

if (iterator->is_table) {
ZVAL_STRING(key, iterator->currentkey);
if (EXPECTED(iterator->currentkey)) {
ZVAL_STRING(key, iterator->currentkey);
} else {
ZVAL_NULL(key);
ZVAL_NULL(&iterator->current);
Copy link
Member

Choose a reason for hiding this comment

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

Setting this here wouldn't have any effect when users traverse over the values only. Changing it in resourcebundle_iterator_read() would require more changes (elsewhere there are checks for Z_ISUNDEF(iterator->current)), and we might break existing semantics. Maybe resourcebundle_iterator_current() would need to return null, but we don't have a zval there. :/

And I wonder about:

$rb = (new ResourceBundle('', NULL))->get('calendar')->get('buddhist');
var_dump($rb->get(3));
var_dump($rb->getErrorMessage());
foreach ($rb as $key => $value) {
    var_dump($key);
    var_dump($rb->getErrorMessage());
}

That reports Cannot load resource element 3: U_MISSING_RESOURCE_ERROR after the ::get() call, but U_ZERO_ERROR when traversing.

Given that value only traversal so far "worked" but returned undef values (and apparently nobody complained), maybe as bugfix we should just set the key to undef (instead of null), and try a proper fix in the master branch only.

Copy link
Member Author

Choose a reason for hiding this comment

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

Given that value only traversal so far "worked" but returned undef values (and apparently nobody complained), maybe as bugfix we should just set the key to undef (instead of null), and try a proper fix in the master branch only.

I wanted to this at first but it seemed to me the more spread fashion was to set to NULL.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, exposing undef values is certainly uncommon, while having null as iterator key is not.

Copy link
Member Author

Choose a reason for hiding this comment

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

it seems to cause issue with JIT.

Copy link
Member

Choose a reason for hiding this comment

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

Undef values should never get exposed to userland.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, but how to solve that for the values (they are already undef)?

Copy link
Member

Choose a reason for hiding this comment

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

I will take a look in this issue in a few hours. In general undef should be transformed into null but this is weird in this case. Possibly the undef values should be skipped, which is conceptually similar to arrays. But anyway, I will first try to understand how and why sometime later today.

}
} else {
ZVAL_LONG(key, iterator->i);
}
Expand Down
48 changes: 48 additions & 0 deletions ext/intl/tests/gh17317.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
GH-17319 (ResourceBundle iterator crash on NULL key)
--EXTENSIONS--
intl
--CREDITS--
KidFlo
Comment on lines +5 to +6
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should add a CREDIT section to new tests; see also https://qa.php.net/phpt_details.php#credits_section.

Copy link
Member

Choose a reason for hiding this comment

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

I disagree; I often add CREDITS section. Co-authored-by doesn't allow fine-grained tracking of what a person did, so that's a downgrade in comparison to the CREDITS section.

Copy link
Member

Choose a reason for hiding this comment

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

Then we should probably change the description accordingly. :)

--FILE--
<?php
foreach ((new ResourceBundle('', NULL))->get('calendar')->get('buddhist') as $key => $value) {
echo "KEY: "; var_dump($key);
echo "VALUE: "; var_dump($value);
}
?>
--EXPECT--
Copy link
Member

Choose a reason for hiding this comment

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

Should use --EXPECTF-- here to be able to use placeholders for the specifc filenames (and line numbers) of the warnings below.

And maybe the test is overspecified; I have no idea how likely it is that the details of this resource bundle will change in ICU versions.

KEY: string(15) "AmPmMarkersAbbr"
VALUE: object(ResourceBundle)#3 (0) {
}
KEY: string(15) "AmPmMarkersAbbr"
VALUE: object(ResourceBundle)#4 (0) {
}
KEY: string(16) "DateTimePatterns"
VALUE: object(ResourceBundle)#3 (0) {
}
KEY: NULL
VALUE: NULL
KEY: NULL
VALUE: NULL
KEY: string(11) "appendItems"
VALUE: object(ResourceBundle)#3 (0) {
}
KEY: string(16) "availableFormats"
VALUE: object(ResourceBundle)#4 (0) {
}
KEY: string(8) "dayNames"
VALUE: object(ResourceBundle)#3 (0) {
}
KEY: string(4) "eras"
VALUE: object(ResourceBundle)#4 (0) {
}
KEY: string(15) "intervalFormats"
VALUE: object(ResourceBundle)#3 (0) {
}
KEY: string(10) "monthNames"
VALUE: object(ResourceBundle)#4 (0) {
}
KEY: string(8) "quarters"
VALUE: object(ResourceBundle)#3 (0) {
}
Loading