From 380e5412d158028312b8e1ea53295162aea4b685 Mon Sep 17 00:00:00 2001 From: Torsten Landsiedel Date: Thu, 13 Jun 2024 17:04:07 +0200 Subject: [PATCH] Adding more inline comments and add a check for an empty pattern --- tests/WP_SQLite_Translator_Tests.php | 8 +++++++- .../sqlite/class-wp-sqlite-pdo-user-defined-functions.php | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/WP_SQLite_Translator_Tests.php b/tests/WP_SQLite_Translator_Tests.php index f989972..71070ec 100644 --- a/tests/WP_SQLite_Translator_Tests.php +++ b/tests/WP_SQLite_Translator_Tests.php @@ -119,16 +119,17 @@ public static function regexpOperators() { } public function testRegexpReplace() { + /* Testing if an actual replacment works correctly */ $this->assertQuery( "INSERT INTO _options (option_name, option_value) VALUES ('test-ignore', '1');" ); $this->assertQuery( "INSERT INTO _options (option_name, option_value) VALUES ('test-remove', '2');" ); - $this->assertQuery( "SELECT * FROM _options WHERE REGEXP_REPLACE(option_name, '(-ignore|-remove)', '') = 'test'" ); $this->assertCount( 2, $this->engine->get_query_results() ); + /* If one of the required parameters is null, the return value is null, copying the MYSQL/MariaDB behavior */ $this->assertQuery( "SELECT REGEXP_REPLACE( null, 'a', 'x') as result" ); $results = $this->engine->get_query_results(); $this->assertEquals( null, $results[0]->result ); @@ -140,6 +141,11 @@ public function testRegexpReplace() { $this->assertQuery( "SELECT REGEXP_REPLACE( 'abc', 'a', null) as result" ); $results = $this->engine->get_query_results(); $this->assertEquals( null, $results[0]->result ); + + /* Providing an empty pattern should produce an error - but we changed that to null to avoid breaking things */ + $this->assertQuery( "SELECT REGEXP_REPLACE( 'abc', '', 'x') as result" ); + $results = $this->engine->get_query_results(); + $this->assertEquals( null, $results[0]->result ); } public function testInsertDateNow() { diff --git a/wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php b/wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php index e8c00a0..816ccb8 100644 --- a/wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php +++ b/wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php @@ -524,6 +524,11 @@ public function regexp_replace( $field, $pattern, $replacement ) { return null; } + /* Return null if the pattern is empty - this changes MySQL/MariaDB behavior! */ + if ( empty( $pattern ) ) { + return null; + } + if ( "\x00" === $pattern[0] ) { $pattern = substr( $pattern, 1 ); $flags = '';