Page MenuHomePhabricator

Implicit conversion to int in #replace parser function
Closed, ResolvedPublicBUG REPORT

Description

We're getting constant errors from the #replace parser function:

Implicit conversion from float 23.727272727272727 to int loses precision in preg_replace called at /path/to/extensions/ParserFunctions/includes/ParserFunctions.php (833)

Not sure exactly how to reproduce it but I think it started when we updated from PHP 7.4 to 8.1. We're using MediaWiki 1.39.3 and the bundled version of ParserFunctions (1.6.0).

Event Timeline

I think the issue is that a few lines before (line 817) there's the following:

$limit = ( ( $wgPFStringLengthLimit - mb_strlen( $inStr ) ) / $diff ) + 1;

In most cases, this results in a non-integer, that is then passed to preg_replace and thus implicitly converted to an integer. I was able to fix the issue by simply adding ceil to the above line, like so:

$limit = ceil( ( $wgPFStringLengthLimit - mb_strlen( $inStr ) ) / $diff ) + 1;

I'll send a patch for review. If merged, I'd like to back-port it to REL1_39

Change 974544 had a related patch set uploaded (by Sophivorus; author: Sophivorus):

[mediawiki/extensions/ParserFunctions@master] Fix implicit conversion to int

https://gerrit.wikimedia.org/r/974544

Change 974544 merged by jenkins-bot:

[mediawiki/extensions/ParserFunctions@master] Fix implicit conversion to int

https://gerrit.wikimedia.org/r/974544

Change 976691 had a related patch set uploaded (by Sophivorus; author: Sophivorus):

[mediawiki/extensions/ParserFunctions@REL1_39] Backport fix of implicit conversion to int

https://gerrit.wikimedia.org/r/976691

Change 976815 had a related patch set uploaded (by Umherirrender; author: Sophivorus):

[mediawiki/extensions/ParserFunctions@REL1_41] Fix implicit conversion to int

https://gerrit.wikimedia.org/r/976815

Change 976816 had a related patch set uploaded (by Umherirrender; author: Sophivorus):

[mediawiki/extensions/ParserFunctions@REL1_40] Fix implicit conversion to int

https://gerrit.wikimedia.org/r/976816

Change 976817 had a related patch set uploaded (by Umherirrender; author: Sophivorus):

[mediawiki/extensions/ParserFunctions@REL1_39] Fix implicit conversion to int

https://gerrit.wikimedia.org/r/976817

Change 976817 merged by jenkins-bot:

[mediawiki/extensions/ParserFunctions@REL1_39] Fix implicit conversion to int

https://gerrit.wikimedia.org/r/976817

Change 976815 merged by jenkins-bot:

[mediawiki/extensions/ParserFunctions@REL1_41] Fix implicit conversion to int

https://gerrit.wikimedia.org/r/976815

Change 976816 merged by jenkins-bot:

[mediawiki/extensions/ParserFunctions@REL1_40] Fix implicit conversion to int

https://gerrit.wikimedia.org/r/976816

Change 976691 abandoned by Umherirrender:

[mediawiki/extensions/ParserFunctions@REL1_39] Backport fix of implicit conversion to int

Reason:

Now empty patch set

https://gerrit.wikimedia.org/r/976691

Change 978543 had a related patch set uploaded (by Sophivorus; author: Sophivorus):

[mediawiki/extensions/Quiz@master] Type cast to prevent error in PHP 8

https://gerrit.wikimedia.org/r/978543