Skip to content

Commit

Permalink
Formatting: Check the result of preg_split() in convert_smilies().
Browse files Browse the repository at this point in the history
This aims to avoid a fatal error from `count()` when `preg_split()` fails on large input.

Includes:
* Optimizing the regular expression used to split the input by tags to avoid unlimited backtracking for better performance.
* Adjusting the function logic for better readability.

Follow-up to [340], [4380], [26191].

Props podpirate, nathkrill, rajinsharwar, dmsnell, bjorsch, q0rban, audrasjb, rupw, Ov3rfly, jorbin, nhrrob, chaion07, mcqueen22, azaozz, narenin, roybellingan, SergeyBiryukov.
See #51019.

git-svn-id: https://develop.svn.wordpress.org/trunk@59515 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Dec 14, 2024
1 parent b03c9f5 commit c3c319e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
61 changes: 35 additions & 26 deletions src/wp-includes/formatting.php
Original file line number Diff line number Diff line change
Expand Up @@ -3473,40 +3473,49 @@ function translate_smiley( $matches ) {
*/
function convert_smilies( $text ) {
global $wp_smiliessearch;
$output = '';
if ( get_option( 'use_smilies' ) && ! empty( $wp_smiliessearch ) ) {
// HTML loop taken from texturize function, could possible be consolidated.
$textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // Capture the tags as well as in between.
$stop = count( $textarr ); // Loop stuff.

// Ignore processing of specific tags.
$tags_to_ignore = 'code|pre|style|script|textarea';
$ignore_block_element = '';
if ( ! get_option( 'use_smilies' ) || empty( $wp_smiliessearch ) ) {
// Return default text.
return $text;
}

for ( $i = 0; $i < $stop; $i++ ) {
$content = $textarr[ $i ];
// HTML loop taken from texturize function, could possible be consolidated.
$textarr = preg_split( '/(<[^>]*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // Capture the tags as well as in between.

// If we're in an ignore block, wait until we find its closing tag.
if ( '' === $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')[^>]*>/', $content, $matches ) ) {
$ignore_block_element = $matches[1];
}
if ( false === $textarr ) {
// Return default text.
return $text;
}

// If it's not a tag and not in ignore block.
if ( '' === $ignore_block_element && strlen( $content ) > 0 && '<' !== $content[0] ) {
$content = preg_replace_callback( $wp_smiliessearch, 'translate_smiley', $content );
}
// Loop stuff.
$stop = count( $textarr );
$output = '';

// Did we exit ignore block?
if ( '' !== $ignore_block_element && '</' . $ignore_block_element . '>' === $content ) {
$ignore_block_element = '';
}
// Ignore processing of specific tags.
$tags_to_ignore = 'code|pre|style|script|textarea';
$ignore_block_element = '';

for ( $i = 0; $i < $stop; $i++ ) {
$content = $textarr[ $i ];

$output .= $content;
// If we're in an ignore block, wait until we find its closing tag.
if ( '' === $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')[^>]*>/', $content, $matches ) ) {
$ignore_block_element = $matches[1];
}
} else {
// Return default text.
$output = $text;

// If it's not a tag and not in ignore block.
if ( '' === $ignore_block_element && strlen( $content ) > 0 && '<' !== $content[0] ) {
$content = preg_replace_callback( $wp_smiliessearch, 'translate_smiley', $content );
}

// Did we exit ignore block?
if ( '' !== $ignore_block_element && '</' . $ignore_block_element . '>' === $content ) {
$ignore_block_element = '';
}

$output .= $content;
}

return $output;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/phpunit/tests/formatting/convertSmilies.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,16 @@ public function _filter_add_smilies( $wpsmiliestrans ) {
$wpsmiliestrans['<3'] = '\xe2\x9d\xa4';
return $wpsmiliestrans;
}


/**
* Tests that the function does not throw a fatal error from count()
* when preg_split() fails on large input.
*
* @ticket 51019
*/
public function test_smilies_with_large_text_input() {
$text = '<p><img alt="" src="data:image/png;base64,' . str_repeat( 'iVBORw0KGgoAAAAN', 65536 ) . '="></p> :)';
$this->assertStringContainsString( "\xf0\x9f\x99\x82", convert_smilies( $text ) );
}
}

0 comments on commit c3c319e

Please sign in to comment.