Skip to content

[BUGFIX] Fix TypeError in HeadlessUserInt::unwrap() when preg_replace_callback returns null#866

Open
Rhovan2223 wants to merge 1 commit intoTYPO3-Headless:masterfrom
Rhovan2223:bugfix/fix-unwrap-null-return-type
Open

[BUGFIX] Fix TypeError in HeadlessUserInt::unwrap() when preg_replace_callback returns null#866
Rhovan2223 wants to merge 1 commit intoTYPO3-Headless:masterfrom
Rhovan2223:bugfix/fix-unwrap-null-return-type

Conversation

@Rhovan2223
Copy link

Problem

HeadlessUserInt::unwrap() declares string as its return type, but preg_replace_callback() can return null on internal failure (as documented in the PHP manual).

This causes a sporadic TypeError in production environments:
TypeError: FriendsOfTYPO3\Headless\Utility\HeadlessUserInt::unwrap(): Return value must be of type string, null returned
The issue is intermittent and hard to reproduce, but occurs under real-world load.

Fix

Add the null coalescing operator (?? $content) to all four preg_replace_callback() calls in unwrap(), so the original content is preserved instead of returning null.

This is the minimal, non-breaking fix — no behavioral change when preg_replace_callback() succeeds.

Changes

  • Classes/Utility/HeadlessUserInt.php: Add ?? $content to all 4 preg_replace_callback() calls

preg_replace_callback() can return null on failure, but unwrap() declares string as return type. This causes a sporadic TypeError.

Add null coalescing operator ( ?? ) to all preg_replace_callback() calls so the original content is returned instead of null.
@twoldanski
Copy link
Collaborator

@Rhovan2223 Hi thank you for report and patch! FYI I incorporated your patch into mine #869, as fallback. (good catch!) We were not hitting these limits so bug was flying under the radar, but you are correct in some cases you can hit it, and then crash...

In my patch I added some optimizations which are trying to limit hitting php errors (and return nulls), if you can tests on your project if it helps you, it would be much appreciated

@Rhovan2223
Copy link
Author

Thanks, will test once merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants