Skip to content

Commit a8024a0

Browse files
committed
Refactor php_pgsql_add_quotes()
1 parent 3a2aae6 commit a8024a0

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
@@ -4511,24 +4511,9 @@ static int php_pgsql_convert_match(const zend_string *str, const char *regex , s
45114511
/* {{{ php_pgsql_add_quote
45124512
* add quotes around string.
45134513
*/
4514-
static int php_pgsql_add_quotes(zval *src, bool should_free)
4514+
static zend_string *php_pgsql_add_quotes(zend_string *src)
45154515
{
4516-
smart_str str = {0};
4517-
4518-
ZEND_ASSERT(Z_TYPE_P(src) == IS_STRING);
4519-
4520-
smart_str_appendc(&str, 'E');
4521-
smart_str_appendc(&str, '\'');
4522-
smart_str_appendl(&str, Z_STRVAL_P(src), Z_STRLEN_P(src));
4523-
smart_str_appendc(&str, '\'');
4524-
smart_str_0(&str);
4525-
4526-
if (should_free) {
4527-
zval_ptr_dtor(src);
4528-
}
4529-
ZVAL_NEW_STR(src, str.s);
4530-
4531-
return SUCCESS;
4516+
return zend_string_concat3("E'", strlen("E'"), ZSTR_VAL(src), ZSTR_LEN(src), "'", strlen("'"));
45324517
}
45334518
/* }}} */
45344519

@@ -4741,8 +4726,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
47414726
if (php_pgsql_convert_match(Z_STR_P(val), REGEX1, sizeof(REGEX1)-1, 1) == FAILURE) {
47424727
err = 1;
47434728
} else {
4744-
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
4745-
php_pgsql_add_quotes(&new_val, 1);
4729+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
47464730
}
47474731
}
47484732
else {
@@ -4809,8 +4793,8 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
48094793
/* better to use PGSQLescapeLiteral since PGescapeStringConn does not handle special \ */
48104794
ZSTR_LEN(str) = PQescapeStringConn(pg_link, ZSTR_VAL(str), Z_STRVAL_P(val), Z_STRLEN_P(val), NULL);
48114795
str = zend_string_truncate(str, ZSTR_LEN(str), 0);
4812-
ZVAL_NEW_STR(&new_val, str);
4813-
php_pgsql_add_quotes(&new_val, 1);
4796+
ZVAL_STR(&new_val, php_pgsql_add_quotes(str));
4797+
zend_string_release_ex(str, false);
48144798
}
48154799
break;
48164800

@@ -4896,8 +4880,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
48964880
err = 1;
48974881
}
48984882
else {
4899-
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
4900-
php_pgsql_add_quotes(&new_val, 1);
4883+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
49014884
}
49024885
#undef REGEX0
49034886
#undef REGEX1
@@ -4932,8 +4915,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
49324915
if (php_pgsql_convert_match(Z_STR_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
49334916
err = 1;
49344917
} else {
4935-
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
4936-
php_pgsql_add_quotes(&new_val, 1);
4918+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
49374919
}
49384920
#undef REGEX0
49394921
}
@@ -4965,8 +4947,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
49654947
err = 1;
49664948
}
49674949
else {
4968-
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
4969-
php_pgsql_add_quotes(&new_val, 1);
4950+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
49704951
}
49714952
#undef REGEX0
49724953
}
@@ -4998,8 +4979,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
49984979
err = 1;
49994980
}
50004981
else {
5001-
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
5002-
php_pgsql_add_quotes(&new_val, 1);
4982+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
50034983
}
50044984
#undef REGEX0
50054985
}
@@ -5077,8 +5057,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
50775057
err = 1;
50785058
}
50795059
else {
5080-
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
5081-
php_pgsql_add_quotes(&new_val, 1);
5060+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
50825061
}
50835062
#undef REGEX0
50845063
}
@@ -5105,15 +5084,14 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
51055084
else {
51065085
unsigned char *tmp;
51075086
size_t to_len;
5108-
smart_str s = {0};
5087+
zend_string *tmp_zstr;
5088+
51095089
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' */
5090+
tmp_zstr = zend_string_init((char *)tmp, to_len - 1, false); /* PQescapeBytea's to_len includes additional '\0' */
51115091
PQfreemem(tmp);
5112-
php_pgsql_add_quotes(&new_val, 1);
5113-
smart_str_appendl(&s, Z_STRVAL(new_val), Z_STRLEN(new_val));
5114-
smart_str_0(&s);
5115-
zval_ptr_dtor(&new_val);
5116-
ZVAL_NEW_STR(&new_val, s.s);
5092+
5093+
ZVAL_STR(&new_val, php_pgsql_add_quotes(tmp_zstr));
5094+
zend_string_release_ex(tmp_zstr, false);
51175095
}
51185096
break;
51195097

@@ -5151,8 +5129,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
51515129
err = 1;
51525130
}
51535131
else {
5154-
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
5155-
php_pgsql_add_quotes(&new_val, 1);
5132+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
51565133
}
51575134
#undef REGEX0
51585135
}

0 commit comments

Comments
 (0)