From 0426b11d8c8a901bee25150496bfa3eb50256f8c Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Sun, 22 Nov 2026 11:04:37 -0500 Subject: [PATCH] ext/pdo_sqlite: Avoid copying bound strings and LOBs Retain the zend_string for each binding and let SQLite release it when the value is no longer needed. Clear a rejected oversized binding so a later execute that omits the parameter still sees NULL. --- ext/pdo_sqlite/php_pdo_sqlite_int.h | 4 ++ ext/pdo_sqlite/sqlite_driver.c | 13 +++++- ext/pdo_sqlite/sqlite_statement.c | 43 +++++++++++++------ .../tests/bind_param_copy_on_write.phpt | 24 +++++++++++ 4 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 ext/pdo_sqlite/tests/bind_param_copy_on_write.phpt diff --git a/ext/pdo_sqlite/php_pdo_sqlite_int.h b/ext/pdo_sqlite/php_pdo_sqlite_int.h index 122f714040de..2110c593ad0f 100644 --- a/ext/pdo_sqlite/php_pdo_sqlite_int.h +++ b/ext/pdo_sqlite/php_pdo_sqlite_int.h @@ -64,8 +64,12 @@ extern const pdo_driver_t pdo_sqlite_driver; extern int pdo_sqlite_scanner(pdo_scanner_t *s); extern int _pdo_sqlite_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int line); +extern int _pdo_sqlite_error_code(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *errmsg, + const char *file, int line); #define pdo_sqlite_error(s) _pdo_sqlite_error(s, NULL, __FILE__, __LINE__) #define pdo_sqlite_error_stmt(s) _pdo_sqlite_error(stmt->dbh, stmt, __FILE__, __LINE__) +#define pdo_sqlite_error_stmt_code(s, code) \ + _pdo_sqlite_error_code((s)->dbh, (s), (code), sqlite3_errstr(code), __FILE__, __LINE__) extern const struct pdo_stmt_methods sqlite_stmt_methods; diff --git a/ext/pdo_sqlite/sqlite_driver.c b/ext/pdo_sqlite/sqlite_driver.c index 692bb8fd6c86..659269c38d58 100644 --- a/ext/pdo_sqlite/sqlite_driver.c +++ b/ext/pdo_sqlite/sqlite_driver.c @@ -27,12 +27,21 @@ #include "sqlite_driver_arginfo.h" int _pdo_sqlite_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int line) /* {{{ */ +{ + pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data; + int errcode = sqlite3_errcode(H->db); + const char *errmsg = errcode == SQLITE_OK ? NULL : sqlite3_errmsg(H->db); + return _pdo_sqlite_error_code(dbh, stmt, errcode, errmsg, file, line); +} + +int _pdo_sqlite_error_code(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *errmsg, + const char *file, int line) { pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data; pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code; pdo_sqlite_error_info *einfo = &H->einfo; - einfo->errcode = sqlite3_errcode(H->db); + einfo->errcode = errcode; einfo->file = file; einfo->line = line; @@ -40,7 +49,7 @@ int _pdo_sqlite_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int li if (einfo->errmsg) { pefree(einfo->errmsg, dbh->is_persistent); } - einfo->errmsg = pestrdup((char*)sqlite3_errmsg(H->db), dbh->is_persistent); + einfo->errmsg = pestrdup(errmsg, dbh->is_persistent); } else { /* no error */ strncpy(*pdo_err, PDO_ERR_NONE, sizeof(*pdo_err)); return 0; diff --git a/ext/pdo_sqlite/sqlite_statement.c b/ext/pdo_sqlite/sqlite_statement.c index 12d98000ae93..07070dfc5aaa 100644 --- a/ext/pdo_sqlite/sqlite_statement.c +++ b/ext/pdo_sqlite/sqlite_statement.c @@ -74,6 +74,33 @@ static int pdo_sqlite_stmt_execute(pdo_stmt_t *stmt) } } +static void pdo_sqlite_release_string(void *value) +{ + zend_string_release(ZEND_CONTAINER_OF((char *) value, zend_string, val[0])); +} + +static bool pdo_sqlite_bind_string(pdo_stmt_t *stmt, int index, zend_string *value, bool blob) +{ + pdo_sqlite_stmt *S = (pdo_sqlite_stmt *) stmt->driver_data; + int rc = SQLITE_TOOBIG; + if (EXPECTED(ZSTR_LEN(value) <= INT_MAX)) { + int length = (int) ZSTR_LEN(value); + zend_string_addref(value); + rc = blob + ? sqlite3_bind_blob(S->stmt, index, ZSTR_VAL(value), length, pdo_sqlite_release_string) + : sqlite3_bind_text(S->stmt, index, ZSTR_VAL(value), length, pdo_sqlite_release_string); + } + if (rc != SQLITE_OK) { + if (rc == SQLITE_TOOBIG && sqlite3_bind_null(S->stmt, index) == SQLITE_OK) { + pdo_sqlite_error_stmt_code(stmt, SQLITE_TOOBIG); + } else { + pdo_sqlite_error_stmt(stmt); + } + return false; + } + return true; +} + static int pdo_sqlite_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, enum pdo_param_event event_type) { @@ -159,14 +186,7 @@ static int pdo_sqlite_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_d } } - if (SQLITE_OK == sqlite3_bind_blob(S->stmt, param->paramno + 1, - Z_STRVAL_P(parameter), - Z_STRLEN_P(parameter), - SQLITE_TRANSIENT)) { - return 1; - } - pdo_sqlite_error_stmt(stmt); - return 0; + return pdo_sqlite_bind_string(stmt, param->paramno + 1, Z_STR_P(parameter), true); case PDO_PARAM_STR: default: @@ -183,12 +203,7 @@ static int pdo_sqlite_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_d if (!try_convert_to_string(parameter)) { return 0; } - if (SQLITE_OK == sqlite3_bind_text(S->stmt, param->paramno + 1, - Z_STRVAL_P(parameter), - Z_STRLEN_P(parameter), - SQLITE_TRANSIENT)) { - return 1; - } + return pdo_sqlite_bind_string(stmt, param->paramno + 1, Z_STR_P(parameter), false); } pdo_sqlite_error_stmt(stmt); return 0; diff --git a/ext/pdo_sqlite/tests/bind_param_copy_on_write.phpt b/ext/pdo_sqlite/tests/bind_param_copy_on_write.phpt new file mode 100644 index 000000000000..f5cc67084243 --- /dev/null +++ b/ext/pdo_sqlite/tests/bind_param_copy_on_write.phpt @@ -0,0 +1,24 @@ +--TEST-- +PDO SQLite keeps the bound value when a PHP string changes during fetch +--EXTENSIONS-- +pdo_sqlite +--FILE-- +prepare('SELECT :v UNION ALL SELECT :v'); + $stmt->bindParam(':v', $value, $type); + $stmt->execute(); + var_dump($stmt->fetchColumn()); + $value[0] = 'b'; + var_dump($stmt->fetchColumn(), $value); +} +?> +--EXPECT-- +string(3) "aaa" +string(3) "aaa" +string(3) "baa" +string(3) "aaa" +string(3) "aaa" +string(3) "baa"