Skip to content

[5.x]: StringHelper::replaceMb4 can return null despite being type hinted to string #17202

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
xorbdev opened this issue Apr 30, 2025 · 2 comments
Labels

Comments

@xorbdev
Copy link

xorbdev commented Apr 30, 2025

What happened?

Description

If you pass an invalid UTF-8 byte sequence such as "\xC3\x28" to StringHelper::replaceMb4, the preg_replace_callback call will return null and cause an error since the method it is type hinted to string.

Steps to reproduce

  1. Call StringHelper::replaceMb4("\xC3\x28", '');

Craft CMS version

5.7.4

PHP version

8.4

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

@brandonkelly
Copy link
Member

Thanks for reporting that! The core issue is that \xC3\x28 is considered invalid UTF-8 data. I’ve fixed this for the next Craft 4 and 5 releases by catching the null response and throwing an InvalidArgumentException instead.

@brandonkelly
Copy link
Member

Craft 4.15.3 and 5.7.5 are out with that fix. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants