Skip to content

Commit c246c52

Browse files
committed
Refactor php_pgsql_add_quotes()
1 parent 79cb3c8 commit c246c52

File tree

1 file changed

+17
-41
lines changed

1 file changed

+17
-41
lines changed

ext/pgsql/pgsql.c

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4521,24 +4521,9 @@ static int php_pgsql_convert_match(const zend_string *str, const char *regex , s
45214521
/* {{{ php_pgsql_add_quote
45224522
* add quotes around string.
45234523
*/
4524-
static int php_pgsql_add_quotes(zval *src, bool should_free)
4524+
static zend_string *php_pgsql_add_quotes(zend_string *src)
45254525
{
4526-
smart_str str = {0};
4527-
4528-
ZEND_ASSERT(Z_TYPE_P(src) == IS_STRING);
4529-
4530-
smart_str_appendc(&str, 'E');
4531-
smart_str_appendc(&str, '\'');
4532-
smart_str_appendl(&str, Z_STRVAL_P(src), Z_STRLEN_P(src));
4533-
smart_str_appendc(&str, '\'');
4534-
smart_str_0(&str);
4535-
4536-
if (should_free) {
4537-
zval_ptr_dtor(src);
4538-
}
4539-
ZVAL_NEW_STR(src, str.s);
4540-
4541-
return SUCCESS;
4526+
return zend_string_concat3("E'", strlen("E'"), ZSTR_VAL(src), ZSTR_LEN(src), "'", strlen("'"));
45424527
}
45434528
/* }}} */
45444529

@@ -4751,8 +4736,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
47514736
if (php_pgsql_convert_match(Z_STR_P(val), REGEX1, sizeof(REGEX1)-1, 1) == FAILURE) {
47524737
err = 1;
47534738
} else {
4754-
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
4755-
php_pgsql_add_quotes(&new_val, 1);
4739+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
47564740
}
47574741
}
47584742
else {
@@ -4818,9 +4802,8 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
48184802
str = zend_string_alloc(Z_STRLEN_P(val) * 2, 0);
48194803
/* better to use PGSQLescapeLiteral since PGescapeStringConn does not handle special \ */
48204804
ZSTR_LEN(str) = PQescapeStringConn(pg_link, ZSTR_VAL(str), Z_STRVAL_P(val), Z_STRLEN_P(val), NULL);
4821-
str = zend_string_truncate(str, ZSTR_LEN(str), 0);
4822-
ZVAL_NEW_STR(&new_val, str);
4823-
php_pgsql_add_quotes(&new_val, 1);
4805+
ZVAL_STR(&new_val, php_pgsql_add_quotes(str));
4806+
zend_string_release_ex(str, false);
48244807
}
48254808
break;
48264809

@@ -4906,8 +4889,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
49064889
err = 1;
49074890
}
49084891
else {
4909-
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
4910-
php_pgsql_add_quotes(&new_val, 1);
4892+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
49114893
}
49124894
#undef REGEX0
49134895
#undef REGEX1
@@ -4942,8 +4924,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
49424924
if (php_pgsql_convert_match(Z_STR_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
49434925
err = 1;
49444926
} else {
4945-
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
4946-
php_pgsql_add_quotes(&new_val, 1);
4927+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
49474928
}
49484929
#undef REGEX0
49494930
}
@@ -4975,8 +4956,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
49754956
err = 1;
49764957
}
49774958
else {
4978-
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
4979-
php_pgsql_add_quotes(&new_val, 1);
4959+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
49804960
}
49814961
#undef REGEX0
49824962
}
@@ -5008,8 +4988,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
50084988
err = 1;
50094989
}
50104990
else {
5011-
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
5012-
php_pgsql_add_quotes(&new_val, 1);
4991+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
50134992
}
50144993
#undef REGEX0
50154994
}
@@ -5087,8 +5066,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
50875066
err = 1;
50885067
}
50895068
else {
5090-
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
5091-
php_pgsql_add_quotes(&new_val, 1);
5069+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
50925070
}
50935071
#undef REGEX0
50945072
}
@@ -5115,15 +5093,14 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
51155093
else {
51165094
unsigned char *tmp;
51175095
size_t to_len;
5118-
smart_str s = {0};
5096+
zend_string *tmp_zstr;
5097+
51195098
tmp = PQescapeByteaConn(pg_link, (unsigned char *)Z_STRVAL_P(val), Z_STRLEN_P(val), &to_len);
5120-
ZVAL_STRINGL(&new_val, (char *)tmp, to_len - 1); /* PQescapeBytea's to_len includes additional '\0' */
5099+
tmp_zstr = zend_string_init((char *)tmp, to_len - 1, false); /* PQescapeBytea's to_len includes additional '\0' */
51215100
PQfreemem(tmp);
5122-
php_pgsql_add_quotes(&new_val, 1);
5123-
smart_str_appendl(&s, Z_STRVAL(new_val), Z_STRLEN(new_val));
5124-
smart_str_0(&s);
5125-
zval_ptr_dtor(&new_val);
5126-
ZVAL_NEW_STR(&new_val, s.s);
5101+
5102+
ZVAL_STR(&new_val, php_pgsql_add_quotes(tmp_zstr));
5103+
zend_string_release_ex(tmp_zstr, false);
51275104
}
51285105
break;
51295106

@@ -5161,8 +5138,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
51615138
err = 1;
51625139
}
51635140
else {
5164-
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
5165-
php_pgsql_add_quotes(&new_val, 1);
5141+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
51665142
}
51675143
#undef REGEX0
51685144
}

0 commit comments

Comments
 (0)