Skip to content

Commit 1331a61

Browse files
authored
ext/pdo: Use zend_strpprintf() instead of spprintf() (#17743)
Most of the time this will end up as a zend_string in some way anyway, do it directly.
1 parent 5333afa commit 1331a61

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

ext/pdo/pdo_dbh.c

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ static bool pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value, u
4040
void pdo_throw_exception(unsigned int driver_errcode, char *driver_errmsg, pdo_error_type *pdo_error)
4141
{
4242
zval error_info,pdo_exception;
43-
char *pdo_exception_message;
4443

4544
object_init_ex(&pdo_exception, php_pdo_get_exception());
4645
array_init(&error_info);
@@ -49,17 +48,17 @@ void pdo_throw_exception(unsigned int driver_errcode, char *driver_errmsg, pdo_e
4948
add_next_index_long(&error_info, driver_errcode);
5049
add_next_index_string(&error_info, driver_errmsg);
5150

52-
spprintf(&pdo_exception_message, 0,"SQLSTATE[%s] [%d] %s",*pdo_error, driver_errcode, driver_errmsg);
51+
zend_string *pdo_exception_message = zend_strpprintf(0,"SQLSTATE[%s] [%d] %s",*pdo_error, driver_errcode, driver_errmsg);
5352
zend_update_property(php_pdo_get_exception(), Z_OBJ(pdo_exception), "errorInfo", sizeof("errorInfo")-1, &error_info);
5453
zend_update_property_long(php_pdo_get_exception(), Z_OBJ(pdo_exception), "code", sizeof("code")-1, driver_errcode);
55-
zend_update_property_string(
54+
zend_update_property_str(
5655
php_pdo_get_exception(),
5756
Z_OBJ(pdo_exception),
5857
"message",
5958
sizeof("message")-1,
6059
pdo_exception_message
6160
);
62-
efree(pdo_exception_message);
61+
zend_string_release_ex(pdo_exception_message, false);
6362
zval_ptr_dtor(&error_info);
6463
zend_throw_exception_object(&pdo_exception);
6564
}
@@ -74,7 +73,6 @@ PDO_API bool php_pdo_stmt_valid_db_obj_handle(const pdo_stmt_t *stmt)
7473
void pdo_raise_impl_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, pdo_error_type sqlstate, const char *supp) /* {{{ */
7574
{
7675
pdo_error_type *pdo_err = &dbh->error_code;
77-
char *message = NULL;
7876
const char *msg;
7977

8078
if (dbh->error_mode == PDO_ERRMODE_SILENT) {
@@ -98,21 +96,22 @@ void pdo_raise_impl_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, pdo_error_type sqlst
9896
msg = "<<Unknown error>>";
9997
}
10098

99+
zend_string *message = NULL;
101100
if (supp) {
102-
spprintf(&message, 0, "SQLSTATE[%s]: %s: %s", *pdo_err, msg, supp);
101+
message = zend_strpprintf(0, "SQLSTATE[%s]: %s: %s", *pdo_err, msg, supp);
103102
} else {
104-
spprintf(&message, 0, "SQLSTATE[%s]: %s", *pdo_err, msg);
103+
message = zend_strpprintf(0, "SQLSTATE[%s]: %s", *pdo_err, msg);
105104
}
106105

107106
if (dbh->error_mode != PDO_ERRMODE_EXCEPTION) {
108-
php_error_docref(NULL, E_WARNING, "%s", message);
107+
php_error_docref(NULL, E_WARNING, "%s", ZSTR_VAL(message));
109108
} else {
110109
zval ex, info;
111110
zend_class_entry *pdo_ex = php_pdo_get_exception();
112111

113112
object_init_ex(&ex, pdo_ex);
114113

115-
zend_update_property_string(zend_ce_exception, Z_OBJ(ex), "message", sizeof("message")-1, message);
114+
zend_update_property_str(zend_ce_exception, Z_OBJ(ex), "message", sizeof("message")-1, message);
116115
zend_update_property_string(zend_ce_exception, Z_OBJ(ex), "code", sizeof("code")-1, *pdo_err);
117116

118117
array_init(&info);
@@ -125,9 +124,7 @@ void pdo_raise_impl_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, pdo_error_type sqlst
125124
zend_throw_exception_object(&ex);
126125
}
127126

128-
if (message) {
129-
efree(message);
130-
}
127+
zend_string_release_ex(message, false);
131128
}
132129
/* }}} */
133130

@@ -386,8 +383,7 @@ PDO_API void php_pdo_internal_construct_driver(INTERNAL_FUNCTION_PARAMETERS, zen
386383

387384
/* is this supposed to be a persistent connection ? */
388385
if (options) {
389-
int plen = 0;
390-
char *hashkey = NULL;
386+
zend_string *hash_key = NULL;
391387
zend_resource *le;
392388
pdo_dbh_t *pdbh = NULL;
393389
zval *v;
@@ -396,22 +392,22 @@ PDO_API void php_pdo_internal_construct_driver(INTERNAL_FUNCTION_PARAMETERS, zen
396392
if (Z_TYPE_P(v) == IS_STRING &&
397393
!is_numeric_string(Z_STRVAL_P(v), Z_STRLEN_P(v), NULL, NULL, 0) && Z_STRLEN_P(v) > 0) {
398394
/* user specified key */
399-
plen = spprintf(&hashkey, 0, "PDO:DBH:DSN=%s:%s:%s:%s", data_source,
395+
hash_key = zend_strpprintf(0, "PDO:DBH:DSN=%s:%s:%s:%s", data_source,
400396
username ? username : "",
401397
password ? password : "",
402398
Z_STRVAL_P(v));
403399
is_persistent = 1;
404400
} else {
405401
is_persistent = zval_get_long(v) ? 1 : 0;
406-
plen = spprintf(&hashkey, 0, "PDO:DBH:DSN=%s:%s:%s", data_source,
402+
hash_key = zend_strpprintf(0, "PDO:DBH:DSN=%s:%s:%s", data_source,
407403
username ? username : "",
408404
password ? password : "");
409405
}
410406
}
411407

412408
if (is_persistent) {
413409
/* let's see if we have one cached.... */
414-
if ((le = zend_hash_str_find_ptr(&EG(persistent_list), hashkey, plen)) != NULL) {
410+
if ((le = zend_hash_find_ptr(&EG(persistent_list), hash_key)) != NULL) {
415411
if (le->type == php_pdo_list_entry()) {
416412
pdbh = (pdo_dbh_t*)le->ptr;
417413

@@ -433,9 +429,9 @@ PDO_API void php_pdo_internal_construct_driver(INTERNAL_FUNCTION_PARAMETERS, zen
433429

434430
pdbh->refcount = 1;
435431
pdbh->is_persistent = 1;
436-
pdbh->persistent_id = pemalloc(plen + 1, 1);
437-
memcpy((char *)pdbh->persistent_id, hashkey, plen+1);
438-
pdbh->persistent_id_len = plen;
432+
pdbh->persistent_id = pemalloc(ZSTR_LEN(hash_key) + 1, true);
433+
memcpy((char *)pdbh->persistent_id, ZSTR_VAL(hash_key), ZSTR_LEN(hash_key) + 1);
434+
pdbh->persistent_id_len = ZSTR_LEN(hash_key);
439435
pdbh->def_stmt_ce = dbh->def_stmt_ce;
440436
}
441437
}
@@ -456,8 +452,8 @@ PDO_API void php_pdo_internal_construct_driver(INTERNAL_FUNCTION_PARAMETERS, zen
456452
dbh = pdbh;
457453
}
458454

459-
if (hashkey) {
460-
efree(hashkey);
455+
if (hash_key) {
456+
zend_string_release_ex(hash_key, false);
461457
}
462458
}
463459

0 commit comments

Comments
 (0)