Skip to content

Commit 7a588ee

Browse files
committed
Refactor php_pgsql_add_quotes()
1 parent 06c293b commit 7a588ee

File tree

1 file changed

+17
-40
lines changed

1 file changed

+17
-40
lines changed

ext/pgsql/pgsql.c

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4510,24 +4510,9 @@ 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
{
4515-
smart_str str = {0};
4516-
4517-
ZEND_ASSERT(Z_TYPE_P(src) == IS_STRING);
4518-
4519-
smart_str_appendc(&str, 'E');
4520-
smart_str_appendc(&str, '\'');
4521-
smart_str_appendl(&str, Z_STRVAL_P(src), Z_STRLEN_P(src));
4522-
smart_str_appendc(&str, '\'');
4523-
smart_str_0(&str);
4524-
4525-
if (should_free) {
4526-
zval_ptr_dtor(src);
4527-
}
4528-
ZVAL_NEW_STR(src, str.s);
4529-
4530-
return SUCCESS;
4515+
return zend_string_concat3("E'", strlen("E'"), ZSTR_VAL(src), ZSTR_LEN(src), "'", strlen("'"));
45314516
}
45324517
/* }}} */
45334518

@@ -4740,8 +4725,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
47404725
if (php_pgsql_convert_match(Z_STR_P(val), REGEX1, sizeof(REGEX1)-1, 1) == FAILURE) {
47414726
err = 1;
47424727
} else {
4743-
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
4744-
php_pgsql_add_quotes(&new_val, 1);
4728+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
47454729
}
47464730
}
47474731
else {
@@ -4808,8 +4792,8 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
48084792
/* better to use PGSQLescapeLiteral since PGescapeStringConn does not handle special \ */
48094793
ZSTR_LEN(str) = PQescapeStringConn(pg_link, ZSTR_VAL(str), Z_STRVAL_P(val), Z_STRLEN_P(val), NULL);
48104794
str = zend_string_truncate(str, ZSTR_LEN(str), 0);
4811-
ZVAL_NEW_STR(&new_val, str);
4812-
php_pgsql_add_quotes(&new_val, 1);
4795+
ZVAL_STR(&new_val, php_pgsql_add_quotes(str));
4796+
zend_string_release_ex(str, false);
48134797
}
48144798
break;
48154799

@@ -4895,8 +4879,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
48954879
err = 1;
48964880
}
48974881
else {
4898-
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
4899-
php_pgsql_add_quotes(&new_val, 1);
4882+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
49004883
}
49014884
#undef REGEX0
49024885
#undef REGEX1
@@ -4931,8 +4914,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
49314914
if (php_pgsql_convert_match(Z_STR_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
49324915
err = 1;
49334916
} else {
4934-
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
4935-
php_pgsql_add_quotes(&new_val, 1);
4917+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
49364918
}
49374919
#undef REGEX0
49384920
}
@@ -4964,8 +4946,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
49644946
err = 1;
49654947
}
49664948
else {
4967-
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
4968-
php_pgsql_add_quotes(&new_val, 1);
4949+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
49694950
}
49704951
#undef REGEX0
49714952
}
@@ -4997,8 +4978,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
49974978
err = 1;
49984979
}
49994980
else {
5000-
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
5001-
php_pgsql_add_quotes(&new_val, 1);
4981+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
50024982
}
50034983
#undef REGEX0
50044984
}
@@ -5076,8 +5056,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
50765056
err = 1;
50775057
}
50785058
else {
5079-
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
5080-
php_pgsql_add_quotes(&new_val, 1);
5059+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
50815060
}
50825061
#undef REGEX0
50835062
}
@@ -5104,15 +5083,14 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
51045083
else {
51055084
unsigned char *tmp;
51065085
size_t to_len;
5107-
smart_str s = {0};
5086+
zend_string *tmp_zstr;
5087+
51085088
tmp = PQescapeByteaConn(pg_link, (unsigned char *)Z_STRVAL_P(val), Z_STRLEN_P(val), &to_len);
5109-
ZVAL_STRINGL(&new_val, (char *)tmp, to_len - 1); /* PQescapeBytea's to_len includes additional '\0' */
5089+
tmp_zstr = zend_string_init((char *)tmp, to_len - 1, false); /* PQescapeBytea's to_len includes additional '\0' */
51105090
PQfreemem(tmp);
5111-
php_pgsql_add_quotes(&new_val, 1);
5112-
smart_str_appendl(&s, Z_STRVAL(new_val), Z_STRLEN(new_val));
5113-
smart_str_0(&s);
5114-
zval_ptr_dtor(&new_val);
5115-
ZVAL_NEW_STR(&new_val, s.s);
5091+
5092+
ZVAL_STR(&new_val, php_pgsql_add_quotes(tmp_zstr));
5093+
zend_string_release_ex(tmp_zstr, false);
51165094
}
51175095
break;
51185096

@@ -5150,8 +5128,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
51505128
err = 1;
51515129
}
51525130
else {
5153-
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
5154-
php_pgsql_add_quotes(&new_val, 1);
5131+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
51555132
}
51565133
#undef REGEX0
51575134
}

0 commit comments

Comments
 (0)