Skip to content

ext/mbstring: Fix truncated encoding names resolving to full names - #23974

Open
kamil-tekiela wants to merge 3 commits into
php:PHP-8.4from
kamil-tekiela:fix-gh23958-mbstring
Open

kamil-tekiela wants to merge 3 commits into
php:PHP-8.4from
kamil-tekiela:fix-gh23958-mbstring

Conversation

@kamil-tekiela

@kamil-tekiela kamil-tekiela commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Introduced by 3ad422ebd0b

@ndossche

Fixes #23958

@kamil-tekiela kamil-tekiela changed the title fix-gh23958-mbstring-prefix ext/mbstring: Fix truncated encoding names resolving to full names Sep 28, 2026

@youkidearitai youkidearitai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, nice catch!
LGTM.

@alexdowad

Copy link
Copy Markdown
Contributor

I don't know if I would say that the problem was really caused by 3ad422ebd0b. That just moved the strlen call into the caller. Even before that commit, we were not checking that the provided name length matched the encoding name length.

@alexdowad

Copy link
Copy Markdown
Contributor

...But otherwise, the fix is good. :)

@ndossche Do you think we should cache strlen((*encoding)->name) in the mbfl_encoding struct, rather than deriving it with strlen every time?

@iliaal

iliaal commented Sep 29, 2026

Copy link
Copy Markdown
Member

php_mb_parse_encoding_list() has the same prefix match for "auto" (strncasecmp(p1, "auto", p1_length)), so mb_detect_order("aut") is accepted as auto. 8.3 rejected it.

@kamil-tekiela

kamil-tekiela commented Sep 29, 2026 •

Copy link
Copy Markdown
Member Author

php_mb_parse_encoding_list() has the same prefix match for "auto" (strncasecmp(p1, "auto", p1_length)), so mb_detect_order("aut") is accepted as auto. 8.3 rejected it.

@alexdowad Maybe you can help me with this one. https://3v4l.org/0qeOF#v I think neither "a", "au", nor "aut" should match, but it looks like this is a fallback mechanism that converts empty list into an "auto". I don't actually know how this should be fixed. What do you recommend?

There is a comment in gh15824.phpt that makes me think this was an intentional change.

@alexdowad

Copy link
Copy Markdown
Contributor

@kamil-tekiela Hmm. I just looked at gh15824.phpt, but I don't understand how it implies that "this was an intentional change".
There is a comment there which implies that accepting a trailing comma in a list of text encodings is intended. It doesn't seem to apply to the issue raised in this PR.

@ndossche

Copy link
Copy Markdown
Member

@ndossche Do you think we should cache strlen((*encoding)->name) in the mbfl_encoding struct, rather than deriving it with strlen every time?

Perhaps, should be simple enough for the master branch.

@kamil-tekiela Hmm. I just looked at gh15824.phpt, but I don't understand how it implies that "this was an intentional change". There is a comment there which implies that accepting a trailing comma in a list of text encodings is intended. It doesn't seem to apply to the issue raised in this PR.

Same here, this is about a trailing comma.

@kamil-tekiela

Copy link
Copy Markdown
Member Author

@kamil-tekiela Hmm. I just looked at gh15824.phpt, but I don't understand how it implies that "this was an intentional change". There is a comment there which implies that accepting a trailing comma in a list of text encodings is intended. It doesn't seem to apply to the issue raised in this PR.

Maybe I misunderstood the comment. So in your opinion what should be done about p1_length == 0 with auto?

@alexdowad

Copy link
Copy Markdown
Contributor

Maybe I misunderstood the comment. So in your opinion what should be done about p1_length == 0 with auto?

I think it does make sense to validate p1_length == strlen("auto").

For both that and the main bug fix which @kamil-tekiela kindly included in this PR, Hyrum's law does apply. It will almost certainly break some code written by someone, somewhere. But we can't keep unintended changes forever because of that. If this behavior was introduced in PHP 8.4, it's an unintended change and I think it should be fixed.

@kamil-tekiela

Copy link
Copy Markdown
Member Author

@alexdowad given your statement, I added another commit to fix the "auto" to pre-PHP 8.4. I think all of it is now back to PHP 8.3 behaviour https://3v4l.org/L5V1Y#v

I also removed the misleading trailing comma from the other test which was the easiest way to fix that test, but I still don't understand what that comment meant.

@alexdowad

Copy link
Copy Markdown
Contributor

I also removed the misleading trailing comma from the other test which was the easiest way to fix that test, but I still don't understand what that comment meant.

It looks like @youkidearitai added that test. I think he probably felt that it would be good to make mb_detect_encoding tolerate a trailing comma (i.e. don't break on poorly formatted input). If we agree that is desirable, the best way to do it would be to detect a trailing empty string in the list and just filter it out, not by interpreting that empty string as matching some random text encoding.

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.

5 participants