Skip to content

Commit 8a283f7

Browse files
committed
Store pgsql le_string as zend_string
1 parent fff93b6 commit 8a283f7

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

ext/pgsql/pgsql.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ static void _free_result(zend_resource *rsrc)
278278
}
279279
/* }}} */
280280

281+
static void release_string(zend_resource *rsrc)
282+
{
283+
zend_string_release((zend_string *) rsrc->ptr);
284+
}
285+
281286
static bool _php_pgsql_identifier_is_escaped(const char *identifier, size_t len) /* {{{ */
282287
{
283288
/* Handle edge case. Cannot be a escaped string */
@@ -351,7 +356,7 @@ PHP_MINIT_FUNCTION(pgsql)
351356
le_plink = zend_register_list_destructors_ex(NULL, _close_pgsql_plink, "pgsql link persistent", module_number);
352357
le_result = zend_register_list_destructors_ex(_free_result, NULL, "pgsql result", module_number);
353358
le_lofp = zend_register_list_destructors_ex(_free_ptr, NULL, "pgsql large object", module_number);
354-
le_string = zend_register_list_destructors_ex(_free_ptr, NULL, "pgsql string", module_number);
359+
le_string = zend_register_list_destructors_ex(release_string, NULL, "pgsql string", module_number);
355360
/* libpq version */
356361
php_libpq_version(buf, sizeof(buf));
357362
REGISTER_STRING_CONSTANT("PGSQL_LIBPQ_VERSION", buf, CONST_CS | CONST_PERSISTENT);
@@ -1476,19 +1481,19 @@ static inline bool is_valid_oid_string(zend_string *oid, Oid *return_oid)
14761481
}
14771482

14781483
/* {{{ get_field_name */
1479-
static char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list)
1484+
static zend_string *get_field_name(PGconn *pgsql, Oid oid, HashTable *list)
14801485
{
14811486
smart_str str = {0};
14821487
zend_resource *field_type;
1483-
char *ret=NULL;
1488+
zend_string *ret = NULL;
14841489

14851490
/* try to lookup the type in the resource list */
14861491
smart_str_appends(&str, "pgsql_oid_");
14871492
smart_str_append_unsigned(&str, oid);
14881493
smart_str_0(&str);
14891494

14901495
if ((field_type = zend_hash_find_ptr(list, str.s)) != NULL) {
1491-
ret = estrdup((char *)field_type->ptr);
1496+
ret = zend_string_copy((zend_string *) field_type->ptr);
14921497
} else { /* hash all oid's */
14931498
int i, num_rows;
14941499
int oid_offset,name_offset;
@@ -1501,7 +1506,7 @@ static char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list)
15011506
PQclear(result);
15021507
}
15031508
smart_str_free(&str);
1504-
return estrndup("", sizeof("") - 1);
1509+
return ZSTR_EMPTY_ALLOC();
15051510
}
15061511
num_rows = PQntuples(result);
15071512
oid_offset = PQfnumber(result,"oid");
@@ -1521,10 +1526,10 @@ static char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list)
15211526
continue;
15221527
}
15231528
new_oid_entry.type = le_string;
1524-
new_oid_entry.ptr = estrdup(tmp_name);
1529+
new_oid_entry.ptr = zend_string_init(tmp_name, strlen(tmp_name), 0);
15251530
zend_hash_update_mem(list, str.s, (void *) &new_oid_entry, sizeof(zend_resource));
15261531
if (!ret && strtoul(tmp_oid, &end_ptr, 10)==oid) {
1527-
ret = estrdup(tmp_name);
1532+
ret = zend_string_copy(new_oid_entry.ptr);
15281533
}
15291534
}
15301535
PQclear(result);
@@ -1582,7 +1587,7 @@ PHP_FUNCTION(pg_field_table)
15821587

15831588
if ((field_table = zend_hash_find_ptr(&EG(regular_list), hash_key.s)) != NULL) {
15841589
smart_str_free(&hash_key);
1585-
RETURN_STRING((char *)field_table->ptr);
1590+
RETURN_STR_COPY((zend_string *)field_table->ptr);
15861591
} else { /* Not found, lookup by querying PostgreSQL system tables */
15871592
PGresult *tmp_res;
15881593
smart_str querystr = {0};
@@ -1610,12 +1615,12 @@ PHP_FUNCTION(pg_field_table)
16101615
}
16111616

16121617
new_field_table.type = le_string;
1613-
new_field_table.ptr = estrdup(table_name);
1618+
new_field_table.ptr = zend_string_init(table_name, strlen(table_name), 0);
16141619
zend_hash_update_mem(&EG(regular_list), hash_key.s, (void *)&new_field_table, sizeof(zend_resource));
16151620

16161621
smart_str_free(&hash_key);
16171622
PQclear(tmp_res);
1618-
RETURN_STRING(table_name);
1623+
RETURN_STR_COPY(new_field_table.ptr);
16191624
}
16201625

16211626
}
@@ -1662,11 +1667,9 @@ static void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_typ
16621667
case PHP_PG_FIELD_SIZE:
16631668
RETURN_LONG(PQfsize(pgsql_result, (int)field));
16641669
break;
1665-
case PHP_PG_FIELD_TYPE: {
1666-
char *name = get_field_name(pg_result->conn, PQftype(pgsql_result, (int)field), &EG(regular_list));
1667-
RETVAL_STRING(name);
1668-
efree(name);
1669-
}
1670+
case PHP_PG_FIELD_TYPE:
1671+
RETURN_STR(get_field_name(
1672+
pg_result->conn, PQftype(pgsql_result, (int)field), &EG(regular_list)));
16701673
break;
16711674
case PHP_PG_FIELD_TYPE_OID:
16721675

ext/pgsql/php_pgsql.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ PHP_PGSQL_API void php_pgsql_result2array(PGresult *pg_result, zval *ret_array,
188188
static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS,int persistent);
189189
static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type);
190190
static void php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type);
191-
static char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list);
192191
static void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type);
193192
static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type);
194193
static void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS,int entry_type);

0 commit comments

Comments
 (0)