diff --git a/src/wp-includes/wp-db.php b/src/wp-includes/wp-db.php index c300e240f2..9d125d008d 100644 --- a/src/wp-includes/wp-db.php +++ b/src/wp-includes/wp-db.php @@ -1252,6 +1252,7 @@ class wpdb { $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting $query = preg_replace( '|(?prepare( "CONVERT( LEFT( CONVERT( %s USING $charset ), %.0f ) USING $connection_charset )", $value['value'], $value['length']['length'] ); + $length = sprintf( '%.0f', $value['length']['length'] ); + $queries[ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING $charset ), $length ) USING $connection_charset )", $value['value'] ); } else if ( 'binary' !== $charset ) { // If we don't have a length, there's no need to convert binary - it will always return the same result. $queries[ $col ] = $this->prepare( "CONVERT( CONVERT( %s USING $charset ) USING $connection_charset )", $value['value'] ); diff --git a/tests/phpunit/tests/db.php b/tests/phpunit/tests/db.php index fef24cbcd2..a2626a8236 100644 --- a/tests/phpunit/tests/db.php +++ b/tests/phpunit/tests/db.php @@ -273,6 +273,7 @@ class Tests_DB extends WP_UnitTestCase { $this->assertEquals( "UPDATE test_table SET string_column = '%f is a float, %d is an int 3, %s is a string', field = '4'", $sql ); } + /** * Test that SQL modes are set correctly * @ticket 26847 @@ -1115,4 +1116,14 @@ class Tests_DB extends WP_UnitTestCase { $this->assertSame( 'utf8', $result['charset'] ); $this->assertSame( 'utf8_general_ci', $result['collate'] ); } + + /** + * + */ + function test_prepare_with_unescaped_percents() { + global $wpdb; + + $sql = $wpdb->prepare( '%d %1$d %%% %', 1 ); + $this->assertEquals( '1 %1$d %% %', $sql ); + } }