Skip to content
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

ext/phar: Use HASH_FOREACH macros and other refactorings #16073

Merged
merged 6 commits into from
Sep 26, 2024

Conversation

Girgias
Copy link
Member

@Girgias Girgias commented Sep 26, 2024

Commits should be reviewed individually.

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

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

Looks fairly good; only small remarks

if (ZSTR_LEN(str_key) > path_len &&
memcmp(ZSTR_VAL(str_key), ZSTR_VAL(resource->path)+1, path_len) == 0 &&
IS_SLASH(ZSTR_VAL(str_key)[path_len])) {
ZEND_HASH_FOREACH_STR_KEY(&phar->manifest, str_key) {
Copy link
Member

Choose a reason for hiding this comment

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

Can't this be ZEND_HASH_MAP_FOREACH_STR_KEY ? I see there's already other code that assumes it's a map type array, which makes sense to me as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

TIL about this macro

Copy link
Member

Choose a reason for hiding this comment

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

Just gonna say also: You may only use this if you know 100% sure it's not a packed array

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, it seems to be passing all the tests locally, and I don't see how it could be packed.

IS_SLASH(ZSTR_VAL(str_key)[path_len])) {
} ZEND_HASH_FOREACH_END();

ZEND_HASH_FOREACH_STR_KEY(&phar->virtual_dirs, str_key) {
Copy link
Member

Choose a reason for hiding this comment

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

Ditto here


if (SUCCESS != zend_hash_move_forward(&phar->manifest)) {
break;
ZEND_HASH_FOREACH_STR_KEY(&phar->manifest, str_key) {
Copy link
Member

Choose a reason for hiding this comment

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

Same ZEND_HASH_MAP_FOREACH_STR_KEY remark

break;
}
zend_string *str_key;
ZEND_HASH_FOREACH_STR_KEY(manifest, str_key) {
Copy link
Member

Choose a reason for hiding this comment

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

Same ZEND_HASH_MAP_FOREACH_STR_KEY remark

}
entry = (char *) safe_emalloc(keylen, 1, 1);
Copy link
Member

Choose a reason for hiding this comment

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

You don't need to cast this to char *.

Copy link
Member Author

Choose a reason for hiding this comment

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

This was copied and pasted from the original code but will remove.

@@ -260,17 +223,22 @@ static php_stream *phar_make_dirstream(char *dir, HashTable *manifest) /* {{{ */
entry[keylen - dirlen - 1] = '\0';
keylen = keylen - dirlen - 1;
}
PHAR_ADD_ENTRY:
PHAR_ADD_ENTRY:
Copy link
Member

Choose a reason for hiding this comment

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

We should make a rule for labels, AFAIK Tim you and I all indent differently.

Copy link
Member Author

Choose a reason for hiding this comment

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

To be fair, this was CLion autocorrecting it. But yes a rule would be good.

Also simplify the inner if condition
This makes it more obvious that the host_len is that of resource->host
Also simplify and refactor the inside of the loop
…am()

And also pass in the known string length
@Girgias
Copy link
Member Author

Girgias commented Sep 26, 2024

I have fixed the commits in place and forced pushed.

Comment on lines 170 to 171
if (keylen <= dirlen) {
if (keylen == 0 || keylen < dirlen || !strncmp(ZSTR_VAL(str_key), dir, dirlen)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't exactly understand what these conditions are doing, but it feels like it can be refactored to use one of the zend_string APIs

Copy link
Member

Choose a reason for hiding this comment

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

I would have to dig deeper to understand how this works, but let's cut the PR here.

}
entry = safe_emalloc(keylen, 1, 1);
memcpy(entry, ZSTR_VAL(str_key), keylen);
entry[keylen] = '\0';

goto PHAR_ADD_ENTRY;
} else {
if (0 != memcmp(ZSTR_VAL(str_key), dir, dirlen)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

And ditto here

@Girgias Girgias merged commit 1f14d58 into php:master Sep 26, 2024
10 checks passed
@Girgias Girgias deleted the phar-foreach branch September 26, 2024 20:36
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.

2 participants