Skip to content

Commit b35d758

Browse files
committed
Enforce existing UDF argument counts
Register explicit supported arities for every UDF, validate the minimum count for variadic functions, and allow WEEK() without an explicit mode. Shadow incompatible SQLite IF() and UNHEX() overloads so unsupported MySQL calls cannot fall through to built-in behavior.
1 parent 7a7f948 commit b35d758

2 files changed

Lines changed: 219 additions & 14 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php‎

Lines changed: 112 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,30 @@ class WP_SQLite_PDO_User_Defined_Functions {
3030
*/
3131
public static function register_for( $pdo ): self {
3232
$instance = new self();
33+
$register = static function ( $function_name, $callback, $arity ) use ( $pdo ) {
34+
if ( $pdo instanceof PDO\SQLite ) {
35+
$pdo->createFunction( $function_name, $callback, $arity );
36+
} else {
37+
$pdo->sqliteCreateFunction( $function_name, $callback, $arity );
38+
}
39+
};
40+
3341
foreach ( $instance->functions as $f => $t ) {
34-
$arities = $instance->function_arities[ $f ] ?? array( -1 );
42+
foreach ( $instance->function_arities[ $f ] as $arity ) {
43+
$register( $f, array( $instance, $t ), $arity );
44+
}
45+
}
46+
foreach ( $instance->unsupported_function_arities as $f => $arities ) {
47+
$reject = static function () use ( $f ) {
48+
throw new Exception(
49+
sprintf(
50+
"Incorrect parameter count in the call to native function '%s'",
51+
strtoupper( $f )
52+
)
53+
);
54+
};
3555
foreach ( $arities as $arity ) {
36-
if ( $pdo instanceof PDO\SQLite ) {
37-
$pdo->createFunction( $f, array( $instance, $t ), $arity );
38-
} else {
39-
$pdo->sqliteCreateFunction( $f, array( $instance, $t ), $arity );
40-
}
56+
$register( $f, $reject, $arity );
4157
}
4258
}
4359
return $instance;
@@ -104,17 +120,75 @@ public static function register_for( $pdo ): self {
104120
);
105121

106122
/**
107-
* Exact argument counts for functions with optional arguments.
123+
* Supported argument counts for each registered function.
108124
*
109-
* Functions absent from this array are registered as variadic.
125+
* An argument count of -1 registers a variadic function.
110126
*
111127
* @var array
112128
*/
113129
private $function_arities = array(
114-
'regexp_like' => array( 2, 3 ),
115-
'regexp_replace' => array( 3, 4, 5, 6 ),
116-
'regexp_substr' => array( 2, 3, 4, 5 ),
117-
'regexp_instr' => array( 2, 3, 4, 5, 6 ),
130+
'throw' => array( 1 ),
131+
'month' => array( 1 ),
132+
'monthnum' => array( 1 ),
133+
'year' => array( 1 ),
134+
'day' => array( 1 ),
135+
'hour' => array( 1 ),
136+
'minute' => array( 1 ),
137+
'second' => array( 1 ),
138+
'week' => array( 1, 2 ),
139+
'weekday' => array( 1 ),
140+
'dayofweek' => array( 1 ),
141+
'dayofmonth' => array( 1 ),
142+
'unix_timestamp' => array( 0, 1 ),
143+
'now' => array( 0, 1 ),
144+
'md5' => array( 1 ),
145+
'curdate' => array( 0 ),
146+
'rand' => array( 1 ),
147+
'from_unixtime' => array( 1, 2 ),
148+
'localtime' => array( 0, 1 ),
149+
'localtimestamp' => array( 0, 1 ),
150+
'isnull' => array( 1 ),
151+
'if' => array( 3 ),
152+
'regexp' => array( 2 ),
153+
'regexp_like' => array( 2, 3 ),
154+
'regexp_replace' => array( 3, 4, 5, 6 ),
155+
'regexp_substr' => array( 2, 3, 4, 5 ),
156+
'regexp_instr' => array( 2, 3, 4, 5, 6 ),
157+
'field' => array( -1 ),
158+
'log' => array( 1, 2 ),
159+
'least' => array( -1 ),
160+
'greatest' => array( -1 ),
161+
'get_lock' => array( 2 ),
162+
'release_lock' => array( 1 ),
163+
'ucase' => array( 1 ),
164+
'lcase' => array( 1 ),
165+
'unhex' => array( 1 ),
166+
'from_base64' => array( 1 ),
167+
'to_base64' => array( 1 ),
168+
'inet_ntoa' => array( 1 ),
169+
'inet_aton' => array( 1 ),
170+
'datediff' => array( 2 ),
171+
'locate' => array( 2, 3 ),
172+
'utc_date' => array( 0 ),
173+
'utc_time' => array( 0, 1 ),
174+
'utc_timestamp' => array( 0, 1 ),
175+
'version' => array( 0 ),
176+
'reverse' => array( 1 ),
177+
'_helper_like_to_glob_pattern' => array( 1 ),
178+
);
179+
180+
/**
181+
* Variadic registrations that shadow incompatible SQLite built-in overloads.
182+
*
183+
* SQLite has variadic IF() and two-argument UNHEX() implementations. These
184+
* catch-all registrations prevent unsupported MySQL calls from falling
185+
* through to those built-ins.
186+
*
187+
* @var array
188+
*/
189+
private $unsupported_function_arities = array(
190+
'if' => array( -1 ),
191+
'unhex' => array( -1 ),
118192
);
119193

120194
/** @var string|null Last validated regex pattern. */
@@ -510,7 +584,7 @@ public function hour( $time ) {
510584
* @param string $field Representing the date.
511585
* @param int $mode The mode argument.
512586
*/
513-
public function week( $field, $mode ) {
587+
public function week( $field, $mode = 1 ) {
514588
/*
515589
* From https://www.php.net/manual/en/datetime.format.php:
516590
*
@@ -917,7 +991,8 @@ public function regexp_instr( $expr, $pattern, $pos = 1, $occurrence = 1, $retur
917991
*/
918992
public function field() {
919993
$num_args = func_num_args();
920-
if ( $num_args < 2 || is_null( func_get_arg( 0 ) ) ) {
994+
$this->require_minimum_argument_count( 'FIELD', $num_args, 2 );
995+
if ( is_null( func_get_arg( 0 ) ) ) {
921996
return 0;
922997
}
923998
$arg_list = func_get_args();
@@ -974,6 +1049,7 @@ public function log() {
9741049
* @return mixed
9751050
*/
9761051
public function least() {
1052+
$this->require_minimum_argument_count( 'LEAST', func_num_args(), 2 );
9771053
$arg_list = func_get_args();
9781054

9791055
return min( $arg_list );
@@ -987,6 +1063,7 @@ public function least() {
9871063
* @return mixed
9881064
*/
9891065
public function greatest() {
1066+
$this->require_minimum_argument_count( 'GREATEST', func_num_args(), 2 );
9901067
$arg_list = func_get_args();
9911068

9921069
return max( $arg_list );
@@ -1283,6 +1360,27 @@ public function _helper_like_to_glob_pattern( $pattern ) {
12831360
return $pattern;
12841361
}
12851362

1363+
/**
1364+
* Reject a variadic function call with too few arguments.
1365+
*
1366+
* @param string $function_name Function name used in the error message.
1367+
* @param int $actual Actual argument count.
1368+
* @param int $minimum Minimum supported argument count.
1369+
*
1370+
* @throws Exception If the argument count is too low.
1371+
* @return void
1372+
*/
1373+
private function require_minimum_argument_count( $function_name, $actual, $minimum ): void {
1374+
if ( $actual < $minimum ) {
1375+
throw new Exception(
1376+
sprintf(
1377+
"Incorrect parameter count in the call to native function '%s'",
1378+
$function_name
1379+
)
1380+
);
1381+
}
1382+
}
1383+
12861384
/**
12871385
* Compile a MySQL-style regex into a PCRE pattern string.
12881386
*

‎packages/mysql-on-sqlite/tests/WP_SQLite_Driver_Tests.php‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,113 @@ public function testRegexpNegativePosErrors() {
10121012
);
10131013
}
10141014

1015+
/**
1016+
* @dataProvider existingUserDefinedFunctionUnsupportedArgumentCounts
1017+
*/
1018+
public function testExistingUserDefinedFunctionsRejectUnsupportedArgumentCounts( $function_name, $argument_count ) {
1019+
$args = implode( ', ', array_fill( 0, $argument_count, 'NULL' ) );
1020+
$exception = null;
1021+
try {
1022+
$this->sqlite->query( "SELECT `$function_name`($args)" );
1023+
} catch ( PDOException $e ) {
1024+
$exception = $e;
1025+
}
1026+
1027+
$this->assertNotNull( $exception, 'An exception was expected, but none was thrown.' );
1028+
$this->assertStringContainsString(
1029+
"wrong number of arguments to function $function_name()",
1030+
$exception->getMessage()
1031+
);
1032+
}
1033+
1034+
public static function existingUserDefinedFunctionUnsupportedArgumentCounts() {
1035+
return array(
1036+
'throw' => array( 'throw', 2 ),
1037+
'month' => array( 'month', 2 ),
1038+
'monthnum' => array( 'monthnum', 2 ),
1039+
'year' => array( 'year', 2 ),
1040+
'day' => array( 'day', 2 ),
1041+
'hour' => array( 'hour', 2 ),
1042+
'minute' => array( 'minute', 2 ),
1043+
'second' => array( 'second', 2 ),
1044+
'week' => array( 'week', 3 ),
1045+
'weekday' => array( 'weekday', 2 ),
1046+
'dayofweek' => array( 'dayofweek', 2 ),
1047+
'dayofmonth' => array( 'dayofmonth', 2 ),
1048+
'unix_timestamp' => array( 'unix_timestamp', 2 ),
1049+
'now' => array( 'now', 2 ),
1050+
'md5' => array( 'md5', 2 ),
1051+
'curdate' => array( 'curdate', 1 ),
1052+
'rand' => array( 'rand', 2 ),
1053+
'from_unixtime' => array( 'from_unixtime', 3 ),
1054+
'localtime' => array( 'localtime', 2 ),
1055+
'localtimestamp' => array( 'localtimestamp', 2 ),
1056+
'isnull' => array( 'isnull', 2 ),
1057+
'regexp' => array( 'regexp', 3 ),
1058+
'log' => array( 'log', 3 ),
1059+
'get_lock' => array( 'get_lock', 3 ),
1060+
'release_lock' => array( 'release_lock', 2 ),
1061+
'ucase' => array( 'ucase', 2 ),
1062+
'lcase' => array( 'lcase', 2 ),
1063+
'from_base64' => array( 'from_base64', 2 ),
1064+
'to_base64' => array( 'to_base64', 2 ),
1065+
'inet_ntoa' => array( 'inet_ntoa', 2 ),
1066+
'inet_aton' => array( 'inet_aton', 2 ),
1067+
'datediff' => array( 'datediff', 3 ),
1068+
'locate' => array( 'locate', 4 ),
1069+
'utc_date' => array( 'utc_date', 1 ),
1070+
'utc_time' => array( 'utc_time', 2 ),
1071+
'utc_timestamp' => array( 'utc_timestamp', 2 ),
1072+
'version' => array( 'version', 1 ),
1073+
'reverse' => array( 'reverse', 2 ),
1074+
'_helper_like_to_glob_pattern' => array( '_helper_like_to_glob_pattern', 2 ),
1075+
);
1076+
}
1077+
1078+
public function testSqliteBuiltInOverloadsDoNotBypassArgumentValidation() {
1079+
$this->assertQueryError(
1080+
"SELECT UNHEX('41', 'ignored')",
1081+
"Incorrect parameter count in the call to native function 'UNHEX'"
1082+
);
1083+
1084+
$exception = null;
1085+
try {
1086+
$this->sqlite->query( 'SELECT `if`(1, 2, 3, 4)' );
1087+
} catch ( Exception $e ) {
1088+
$exception = $e;
1089+
}
1090+
$this->assertNotNull( $exception, 'An exception was expected, but none was thrown.' );
1091+
$this->assertSame(
1092+
"Incorrect parameter count in the call to native function 'IF'",
1093+
$exception->getMessage()
1094+
);
1095+
}
1096+
1097+
public function testWeekSupportsOptionalModeArgument() {
1098+
$this->assertQuery( "SELECT WEEK('2000-05-27') AS implicit_mode, WEEK('2000-05-27', 1) AS explicit_mode" );
1099+
$result = $this->engine->get_query_results()[0];
1100+
$this->assertSame( '21', $result->implicit_mode );
1101+
$this->assertSame( '21', $result->explicit_mode );
1102+
}
1103+
1104+
/**
1105+
* @dataProvider variadicUserDefinedFunctions
1106+
*/
1107+
public function testVariadicUserDefinedFunctionsRequireTwoArguments( $function_name ) {
1108+
$this->assertQueryError(
1109+
"SELECT $function_name('value')",
1110+
"Incorrect parameter count in the call to native function '$function_name'"
1111+
);
1112+
}
1113+
1114+
public static function variadicUserDefinedFunctions() {
1115+
return array(
1116+
'field' => array( 'FIELD' ),
1117+
'least' => array( 'LEAST' ),
1118+
'greatest' => array( 'GREATEST' ),
1119+
);
1120+
}
1121+
10151122
public function testInsertDateNow() {
10161123
$this->assertQuery(
10171124
"INSERT INTO _dates (option_name, option_value) VALUES ('first', now());"

0 commit comments

Comments
 (0)