Skip to content

Commit 47e5687

Browse files
committed
Refactor php_pgsql_add_quotes()
1 parent 726b34c commit 47e5687

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

ext/pgsql/pgsql.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4510,25 +4510,23 @@ static int php_pgsql_convert_match(const zend_string *str, const char *regex , s
45104510
/* {{{ php_pgsql_add_quote
45114511
* add quotes around string.
45124512
*/
4513-
static int php_pgsql_add_quotes(zval *src, bool should_free)
4513+
static zend_string* php_pgsql_add_quotes(zend_string *src)
45144514
{
45154515
smart_str str = {0};
4516-
4517-
assert(Z_TYPE_P(src) == IS_STRING);
4518-
assert(should_free == 1 || should_free == 0);
4516+
zend_string *result;
45194517

45204518
smart_str_appendc(&str, 'E');
45214519
smart_str_appendc(&str, '\'');
4522-
smart_str_appendl(&str, Z_STRVAL_P(src), Z_STRLEN_P(src));
4520+
smart_str_append(&str, src);
45234521
smart_str_appendc(&str, '\'');
45244522
smart_str_0(&str);
45254523

4526-
if (should_free) {
4527-
zval_ptr_dtor(src);
4528-
}
4529-
ZVAL_NEW_STR(src, str.s);
4524+
result = zend_string_dup(str.s, false);
4525+
/* Free source and smart string */
4526+
zend_string_release(src);
4527+
smart_str_free(&str);
45304528

4531-
return SUCCESS;
4529+
return result;
45324530
}
45334531
/* }}} */
45344532

@@ -4741,8 +4739,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
47414739
if (php_pgsql_convert_match(Z_STR_P(val), REGEX1, sizeof(REGEX1)-1, 1) == FAILURE) {
47424740
err = 1;
47434741
} else {
4744-
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
4745-
php_pgsql_add_quotes(&new_val, 1);
4742+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
47464743
}
47474744
}
47484745
else {
@@ -4809,8 +4806,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
48094806
/* better to use PGSQLescapeLiteral since PGescapeStringConn does not handle special \ */
48104807
ZSTR_LEN(str) = PQescapeStringConn(pg_link, ZSTR_VAL(str), Z_STRVAL_P(val), Z_STRLEN_P(val), NULL);
48114808
str = zend_string_truncate(str, ZSTR_LEN(str), 0);
4812-
ZVAL_NEW_STR(&new_val, str);
4813-
php_pgsql_add_quotes(&new_val, 1);
4809+
ZVAL_STR(&new_val, php_pgsql_add_quotes(str));
48144810
}
48154811
break;
48164812

@@ -4896,8 +4892,9 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
48964892
err = 1;
48974893
}
48984894
else {
4899-
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
4900-
php_pgsql_add_quotes(&new_val, 1);
4895+
zend_string *tmp_zstr = php_pgsql_add_quotes(Z_STR_P(val));
4896+
ZVAL_STRINGL(&new_val, ZSTR_VAL(tmp_zstr), ZSTR_LEN(tmp_zstr));
4897+
zend_string_release(tmp_zstr);
49014898
}
49024899
#undef REGEX0
49034900
#undef REGEX1
@@ -4932,8 +4929,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
49324929
if (php_pgsql_convert_match(Z_STR_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
49334930
err = 1;
49344931
} else {
4935-
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
4936-
php_pgsql_add_quotes(&new_val, 1);
4932+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
49374933
}
49384934
#undef REGEX0
49394935
}
@@ -4965,8 +4961,9 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
49654961
err = 1;
49664962
}
49674963
else {
4968-
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
4969-
php_pgsql_add_quotes(&new_val, 1);
4964+
zend_string *tmp_zstr = php_pgsql_add_quotes(Z_STR_P(val));
4965+
ZVAL_STRINGL(&new_val, ZSTR_VAL(tmp_zstr), ZSTR_LEN(tmp_zstr));
4966+
zend_string_release(tmp_zstr);
49704967
}
49714968
#undef REGEX0
49724969
}
@@ -4998,8 +4995,9 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
49984995
err = 1;
49994996
}
50004997
else {
5001-
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
5002-
php_pgsql_add_quotes(&new_val, 1);
4998+
zend_string *tmp_zstr = php_pgsql_add_quotes(Z_STR_P(val));
4999+
ZVAL_STRINGL(&new_val, ZSTR_VAL(tmp_zstr), ZSTR_LEN(tmp_zstr));
5000+
zend_string_release(tmp_zstr);
50035001
}
50045002
#undef REGEX0
50055003
}
@@ -5077,8 +5075,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
50775075
err = 1;
50785076
}
50795077
else {
5080-
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
5081-
php_pgsql_add_quotes(&new_val, 1);
5078+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
50825079
}
50835080
#undef REGEX0
50845081
}
@@ -5105,14 +5102,16 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
51055102
else {
51065103
unsigned char *tmp;
51075104
size_t to_len;
5105+
zend_string *tmp_zstr;
51085106
smart_str s = {0};
51095107
tmp = PQescapeByteaConn(pg_link, (unsigned char *)Z_STRVAL_P(val), Z_STRLEN_P(val), &to_len);
5110-
ZVAL_STRINGL(&new_val, (char *)tmp, to_len - 1); /* PQescapeBytea's to_len includes additional '\0' */
5108+
tmp_zstr = zend_string_init((char *)tmp, to_len - 1, false); /* PQescapeBytea's to_len includes additional '\0' */
51115109
PQfreemem(tmp);
5112-
php_pgsql_add_quotes(&new_val, 1);
5113-
smart_str_appendl(&s, Z_STRVAL(new_val), Z_STRLEN(new_val));
5110+
/* tmp_zstr is freed by php_pgsql_add_quotes() */
5111+
tmp_zstr = php_pgsql_add_quotes(tmp_zstr);
5112+
smart_str_append(&s, tmp_zstr);
51145113
smart_str_0(&s);
5115-
zval_ptr_dtor(&new_val);
5114+
zend_string_release(tmp_zstr);
51165115
ZVAL_NEW_STR(&new_val, s.s);
51175116
}
51185117
break;
@@ -5151,8 +5150,9 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
51515150
err = 1;
51525151
}
51535152
else {
5154-
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
5155-
php_pgsql_add_quotes(&new_val, 1);
5153+
zend_string *tmp_zstr = php_pgsql_add_quotes(Z_STR_P(val));
5154+
ZVAL_STRINGL(&new_val, ZSTR_VAL(tmp_zstr), ZSTR_LEN(tmp_zstr));
5155+
zend_string_release(tmp_zstr);
51565156
}
51575157
#undef REGEX0
51585158
}

0 commit comments

Comments
 (0)