Skip to content

Instantly share code, notes, and snippets.

@cmb69 cmb69/.patch Secret
Last active Nov 12, 2018

Embed
What would you like to do?
sqlite3_bind_*() return value checking
ext/sqlite3/sqlite3.c | 37 +++++++++++++++++++++++++++++--------
1 file changed, 29 insertions(+), 8 deletions(-)
diff --git a/ext/sqlite3/sqlite3.c b/ext/sqlite3/sqlite3.c
index 31f062dd22..9e0c80430f 100644
--- a/ext/sqlite3/sqlite3.c
+++ b/ext/sqlite3/sqlite3.c
@@ -1572,7 +1572,10 @@ PHP_METHOD(sqlite3stmt, execute)
/* If the ZVAL is null then it should be bound as that */
if (Z_TYPE_P(parameter) == IS_NULL) {
- sqlite3_bind_null(stmt_obj->stmt, param->param_number);
+ return_code = sqlite3_bind_null(stmt_obj->stmt, param->param_number);
+ if (return_code != SQLITE_OK) {
+ php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
+ }
continue;
}
@@ -1580,15 +1583,21 @@ PHP_METHOD(sqlite3stmt, execute)
case SQLITE_INTEGER:
convert_to_long(parameter);
#if ZEND_LONG_MAX > 2147483647
- sqlite3_bind_int64(stmt_obj->stmt, param->param_number, Z_LVAL_P(parameter));
+ return_code = sqlite3_bind_int64(stmt_obj->stmt, param->param_number, Z_LVAL_P(parameter));
#else
- sqlite3_bind_int(stmt_obj->stmt, param->param_number, Z_LVAL_P(parameter));
+ return_code = sqlite3_bind_int(stmt_obj->stmt, param->param_number, Z_LVAL_P(parameter));
#endif
+ if (return_code != SQLITE_OK) {
+ php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
+ }
break;
case SQLITE_FLOAT:
convert_to_double(parameter);
- sqlite3_bind_double(stmt_obj->stmt, param->param_number, Z_DVAL_P(parameter));
+ return_code = sqlite3_bind_double(stmt_obj->stmt, param->param_number, Z_DVAL_P(parameter));
+ if (return_code != SQLITE_OK) {
+ php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
+ }
break;
case SQLITE_BLOB:
@@ -1607,21 +1616,33 @@ PHP_METHOD(sqlite3stmt, execute)
}
if (buffer) {
- sqlite3_bind_blob(stmt_obj->stmt, param->param_number, ZSTR_VAL(buffer), ZSTR_LEN(buffer), SQLITE_TRANSIENT);
+ return_code = sqlite3_bind_blob(stmt_obj->stmt, param->param_number, ZSTR_VAL(buffer), ZSTR_LEN(buffer), SQLITE_TRANSIENT);
zend_string_release_ex(buffer, 0);
+ if (return_code != SQLITE_OK) {
+ php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
+ }
} else {
- sqlite3_bind_null(stmt_obj->stmt, param->param_number);
+ return_code = sqlite3_bind_null(stmt_obj->stmt, param->param_number);
+ if (return_code != SQLITE_OK) {
+ php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
+ }
}
break;
}
case SQLITE3_TEXT:
convert_to_string(parameter);
- sqlite3_bind_text(stmt_obj->stmt, param->param_number, Z_STRVAL_P(parameter), Z_STRLEN_P(parameter), SQLITE_STATIC);
+ return_code = sqlite3_bind_text(stmt_obj->stmt, param->param_number, Z_STRVAL_P(parameter), Z_STRLEN_P(parameter), SQLITE_STATIC);
+ if (return_code != SQLITE_OK) {
+ php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
+ }
break;
case SQLITE_NULL:
- sqlite3_bind_null(stmt_obj->stmt, param->param_number);
+ return_code = sqlite3_bind_null(stmt_obj->stmt, param->param_number);
+ if (return_code != SQLITE_OK) {
+ php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
+ }
break;
default:
Execute statement
object(SQLite3Result)#3 (0) {
}
Statement result
array(3) {
[0]=>
int(42)
[1]=>
string(3) "PHP"
[2]=>
int(424242)
}
Change binded values
Execute statement
Fatal error: Uncaught Exception: Unable to bind parameter number 1 (21) in /mnt/c/Users/cmb/php-dev/sqlite3.php:27
Stack trace:
#0 /mnt/c/Users/cmb/php-dev/sqlite3.php(27): SQLite3Stmt->execute()
#1 {main}
Next Exception: Unable to bind parameter number 2 (21) in /mnt/c/Users/cmb/php-dev/sqlite3.php:27
Stack trace:
#0 /mnt/c/Users/cmb/php-dev/sqlite3.php(27): SQLite3Stmt->execute()
#1 {main}
Next Exception: Unable to bind parameter number 3 (21) in /mnt/c/Users/cmb/php-dev/sqlite3.php:27
Stack trace:
#0 /mnt/c/Users/cmb/php-dev/sqlite3.php(27): SQLite3Stmt->execute()
#1 {main}
thrown in /mnt/c/Users/cmb/php-dev/sqlite3.php on line 27
Execute statement
object(SQLite3Result)#3 (0) {
}
Statement result
array(1) {
[0]=>
string(2) "42"
}
Change binded values
Execute statement
Fatal error: Uncaught Exception: Unable to bind parameter number 1 (21) in /mnt/c/Users/cmb/php-dev/sqlite3.php:23
Stack trace:
#0 /mnt/c/Users/cmb/php-dev/sqlite3.php(23): SQLite3Stmt->execute()
#1 {main}
thrown in /mnt/c/Users/cmb/php-dev/sqlite3.php on line 23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.