Skip to content

Commit ba33757

Browse files
committed
Use zend_string for pgsql_trim_message
1 parent 50f5877 commit ba33757

File tree

1 file changed

+23
-43
lines changed

1 file changed

+23
-43
lines changed

ext/pgsql/pgsql.c

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -120,37 +120,23 @@ static int le_link, le_plink, le_result, le_lofp;
120120
#define pg_encoding_to_char(x) "SQL_ASCII"
121121
#endif
122122

123-
/* {{{ _php_pgsql_trim_message */
124-
static char * _php_pgsql_trim_message(const char *message, size_t *len)
123+
static zend_string *_php_pgsql_trim_message(const char *message)
125124
{
126-
register size_t i = strlen(message);
125+
size_t i = strlen(message);
127126

128127
if (i>2 && (message[i-2] == '\r' || message[i-2] == '\n') && message[i-1] == '.') {
129128
--i;
130129
}
131130
while (i>1 && (message[i-1] == '\r' || message[i-1] == '\n')) {
132131
--i;
133132
}
134-
if (len) {
135-
*len = i;
136-
}
137-
return estrndup(message, i);
138-
}
139-
/* }}} */
140-
141-
/* {{{ _php_pgsql_trim_result */
142-
static inline char * _php_pgsql_trim_result(PGconn * pgsql, char **buf)
143-
{
144-
return *buf = _php_pgsql_trim_message(PQerrorMessage(pgsql), NULL);
133+
return zend_string_init(message, i, 0);
145134
}
146-
/* }}} */
147135

148-
#define PQErrorMessageTrim(pgsql, buf) _php_pgsql_trim_result(pgsql, buf)
149-
150-
#define PHP_PQ_ERROR(text, pgsql) { \
151-
char *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql), NULL); \
152-
php_error_docref(NULL, E_WARNING, text, msgbuf); \
153-
efree(msgbuf); \
136+
#define PHP_PQ_ERROR(text, pgsql) { \
137+
zend_string *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql)); \
138+
php_error_docref(NULL, E_WARNING, text, ZSTR_VAL(msgbuf)); \
139+
zend_string_release(msgbuf); \
154140
} \
155141

156142
/* {{{ php_pgsql_set_default_link */
@@ -206,25 +192,23 @@ static void _close_pgsql_plink(zend_resource *rsrc)
206192
/* {{{ _php_pgsql_notice_handler */
207193
static void _php_pgsql_notice_handler(void *resource_id, const char *message)
208194
{
209-
zval *notices;
195+
if (PGG(ignore_notices)) {
196+
return;
197+
}
198+
210199
zval tmp;
211-
char *trimed_message;
212-
size_t trimed_message_len;
213-
214-
if (! PGG(ignore_notices)) {
215-
notices = zend_hash_index_find(&PGG(notices), (zend_ulong)resource_id);
216-
if (!notices) {
217-
array_init(&tmp);
218-
notices = &tmp;
219-
zend_hash_index_update(&PGG(notices), (zend_ulong)resource_id, notices);
220-
}
221-
trimed_message = _php_pgsql_trim_message(message, &trimed_message_len);
222-
if (PGG(log_notices)) {
223-
php_error_docref(NULL, E_NOTICE, "%s", trimed_message);
224-
}
225-
add_next_index_stringl(notices, trimed_message, trimed_message_len);
226-
efree(trimed_message);
200+
zval *notices = zend_hash_index_find(&PGG(notices), (zend_ulong)resource_id);
201+
if (!notices) {
202+
array_init(&tmp);
203+
notices = &tmp;
204+
zend_hash_index_update(&PGG(notices), (zend_ulong)resource_id, notices);
227205
}
206+
207+
zend_string *trimmed_message = _php_pgsql_trim_message(message);
208+
if (PGG(log_notices)) {
209+
php_error_docref(NULL, E_NOTICE, "%s", ZSTR_VAL(trimmed_message));
210+
}
211+
add_next_index_str(notices, trimmed_message);
228212
}
229213
/* }}} */
230214

@@ -776,7 +760,6 @@ static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type
776760
zend_resource *link;
777761
zval *pgsql_link = NULL;
778762
PGconn *pgsql;
779-
char *msgbuf;
780763
char *result;
781764

782765
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &pgsql_link) == FAILURE) {
@@ -799,10 +782,7 @@ static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type
799782
result = PQdb(pgsql);
800783
break;
801784
case PHP_PG_ERROR_MESSAGE:
802-
result = PQErrorMessageTrim(pgsql, &msgbuf);
803-
RETVAL_STRING(result);
804-
efree(result);
805-
return;
785+
RETURN_STR(_php_pgsql_trim_message(PQerrorMessage(pgsql)));
806786
case PHP_PG_OPTIONS:
807787
result = PQoptions(pgsql);
808788
break;

0 commit comments

Comments
 (0)