Skip to content

Commit c577bb8

Browse files
committed
Refactor php_pgsql_add_quotes()
1 parent 4b60eac commit c577bb8

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

ext/pgsql/pgsql.c

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4510,25 +4510,17 @@ 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};
45164516

4517-
assert(Z_TYPE_P(src) == IS_STRING);
4518-
assert(should_free == 1 || should_free == 0);
4519-
45204517
smart_str_appendc(&str, 'E');
45214518
smart_str_appendc(&str, '\'');
4522-
smart_str_appendl(&str, Z_STRVAL_P(src), Z_STRLEN_P(src));
4519+
smart_str_append(&str, src);
45234520
smart_str_appendc(&str, '\'');
45244521
smart_str_0(&str);
45254522

4526-
if (should_free) {
4527-
zval_ptr_dtor(src);
4528-
}
4529-
ZVAL_NEW_STR(src, str.s);
4530-
4531-
return SUCCESS;
4523+
return str.s;
45324524
}
45334525
/* }}} */
45344526

@@ -4741,8 +4733,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
47414733
if (php_pgsql_convert_match(Z_STR_P(val), REGEX1, sizeof(REGEX1)-1, 1) == FAILURE) {
47424734
err = 1;
47434735
} else {
4744-
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
4745-
php_pgsql_add_quotes(&new_val, 1);
4736+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
47464737
}
47474738
}
47484739
else {
@@ -4809,8 +4800,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
48094800
/* better to use PGSQLescapeLiteral since PGescapeStringConn does not handle special \ */
48104801
ZSTR_LEN(str) = PQescapeStringConn(pg_link, ZSTR_VAL(str), Z_STRVAL_P(val), Z_STRLEN_P(val), NULL);
48114802
str = zend_string_truncate(str, ZSTR_LEN(str), 0);
4812-
ZVAL_NEW_STR(&new_val, str);
4813-
php_pgsql_add_quotes(&new_val, 1);
4803+
ZVAL_STR(&new_val, php_pgsql_add_quotes(str));
48144804
}
48154805
break;
48164806

@@ -4896,8 +4886,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
48964886
err = 1;
48974887
}
48984888
else {
4899-
ZVAL_STR(&new_val, Z_STR_P(val));
4900-
php_pgsql_add_quotes(&new_val, 1);
4889+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
49014890
}
49024891
#undef REGEX0
49034892
#undef REGEX1
@@ -4932,8 +4921,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
49324921
if (php_pgsql_convert_match(Z_STR_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
49334922
err = 1;
49344923
} else {
4935-
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
4936-
php_pgsql_add_quotes(&new_val, 1);
4924+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
49374925
}
49384926
#undef REGEX0
49394927
}
@@ -4965,8 +4953,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
49654953
err = 1;
49664954
}
49674955
else {
4968-
ZVAL_STR(&new_val, Z_STR_P(val));
4969-
php_pgsql_add_quotes(&new_val, 1);
4956+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
49704957
}
49714958
#undef REGEX0
49724959
}
@@ -4998,8 +4985,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
49984985
err = 1;
49994986
}
50004987
else {
5001-
ZVAL_STR(&new_val, Z_STR_P(val));
5002-
php_pgsql_add_quotes(&new_val, 1);
4988+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
50034989
}
50044990
#undef REGEX0
50054991
}
@@ -5077,8 +5063,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
50775063
err = 1;
50785064
}
50795065
else {
5080-
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
5081-
php_pgsql_add_quotes(&new_val, 1);
5066+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
50825067
}
50835068
#undef REGEX0
50845069
}
@@ -5105,14 +5090,14 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
51055090
else {
51065091
unsigned char *tmp;
51075092
size_t to_len;
5093+
zend_string *tmp_zstr;
51085094
smart_str s = {0};
51095095
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' */
5096+
tmp_zstr = zend_string_init((char *)tmp, to_len - 1, false); /* PQescapeBytea's to_len includes additional '\0' */
51115097
PQfreemem(tmp);
5112-
php_pgsql_add_quotes(&new_val, 1);
5113-
smart_str_appendl(&s, Z_STRVAL(new_val), Z_STRLEN(new_val));
5098+
smart_str_append(&s, php_pgsql_add_quotes(tmp_zstr));
51145099
smart_str_0(&s);
5115-
zval_ptr_dtor(&new_val);
5100+
zend_string_release(tmp_zstr);
51165101
ZVAL_NEW_STR(&new_val, s.s);
51175102
}
51185103
break;
@@ -5151,8 +5136,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
51515136
err = 1;
51525137
}
51535138
else {
5154-
ZVAL_STR(&new_val, Z_STR_P(val));
5155-
php_pgsql_add_quotes(&new_val, 1);
5139+
ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
51565140
}
51575141
#undef REGEX0
51585142
}

0 commit comments

Comments
 (0)