Skip to content

Commit eb4dcbb

Browse files
committed
Get rid of memory leak, improve default connection handling, fix a few tests
1 parent 5ad03d3 commit eb4dcbb

File tree

8 files changed

+22
-18
lines changed

8 files changed

+22
-18
lines changed

ext/pgsql/pgsql.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
zend_throw_error(NULL, "No PostgreSQL connection opened yet"); \
7979
RETURN_THROWS(); \
8080
}
81-
#define FETCH_DEFAULT_LINK() (PGG(default_link) ? pgsql_link_from_obj(Z_OBJ_P(PGG(default_link))) : NULL)
81+
#define FETCH_DEFAULT_LINK() PGG(default_link)
8282

8383
#define CHECK_PGSQL_LINK(link_handle) \
8484
if (link_handle->conn == NULL) { \
@@ -281,10 +281,8 @@ static zend_string *_php_pgsql_trim_message(const char *message)
281281
zend_string_release(msgbuf); \
282282
} \
283283

284-
static void php_pgsql_set_default_link(zval *link)
284+
static void php_pgsql_set_default_link(pgsql_link_handle *link)
285285
{
286-
GC_ADDREF(Z_OBJ_P(link));
287-
288286
if (PGG(default_link) != NULL) {
289287
pgsql_link_free(FETCH_DEFAULT_LINK());
290288
}
@@ -730,7 +728,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
730728
*/
731729
if (!(connect_type & PGSQL_CONNECT_FORCE_NEW)
732730
&& (index_ptr = zend_hash_find_ptr(&PGG(regular_list), str.s)) != NULL) {
733-
php_pgsql_set_default_link(index_ptr);
731+
php_pgsql_set_default_link(pgsql_link_from_obj(Z_OBJ_P(index_ptr)));
734732
GC_ADDREF(Z_OBJ_P(index_ptr));
735733
ZVAL_COPY(return_value, index_ptr);
736734

@@ -768,7 +766,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
768766

769767
/* add it to the hash */
770768
ZVAL_COPY(&new_index_ptr, return_value);
771-
zend_hash_update_mem(&PGG(regular_list), str.s, (void *) &new_index_ptr, sizeof(zval));
769+
zend_hash_update(&PGG(regular_list), str.s, &new_index_ptr);
772770

773771
/* Keep track of link => hash mapping, so we can remove the hash entry from regular_list
774772
* when the connection is closed. This uses the address of the connection rather than the
@@ -785,7 +783,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
785783
if (! PGG(ignore_notices) && Z_TYPE_P(return_value) == IS_OBJECT) {
786784
PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, (void*)(zend_uintptr_t)Z_OBJ_P(return_value)->handle);
787785
}
788-
php_pgsql_set_default_link(return_value);
786+
php_pgsql_set_default_link(pgsql_link_from_obj(Z_OBJ_P(return_value)));
789787

790788
cleanup:
791789
smart_str_free(&str);
@@ -846,8 +844,8 @@ PHP_FUNCTION(pg_close)
846844
if (!pgsql_link) {
847845
link = FETCH_DEFAULT_LINK();
848846
CHECK_DEFAULT_LINK(link);
849-
pgsql_link_free(link);
850847
PGG(default_link) = NULL;
848+
pgsql_link_free(link);
851849
RETURN_TRUE;
852850
}
853851

@@ -2330,12 +2328,13 @@ PHP_FUNCTION(pg_lo_create)
23302328
} else if ((Z_TYPE_P(pgsql_link) == IS_OBJECT && instanceof_function(Z_OBJCE_P(pgsql_link), pgsql_link_ce))) {
23312329
link = Z_PGSQL_LINK_P(pgsql_link);
23322330
CHECK_PGSQL_LINK(link);
2333-
pgsql = link->conn;
23342331
} else {
23352332
zend_argument_type_error(1, "must be of type PgSql when the connection is provided");
23362333
RETURN_THROWS();
23372334
}
23382335

2336+
pgsql = link->conn;
2337+
23392338
if (oid) {
23402339
switch (Z_TYPE_P(oid)) {
23412340
case IS_STRING:
@@ -3300,7 +3299,7 @@ PHP_FUNCTION(pg_escape_string)
33003299
RETURN_THROWS();
33013300
}
33023301
link = Z_PGSQL_LINK_P(pgsql_link);
3303-
CHECK_PGSQL_LINK(link);
3302+
CHECK_PGSQL_LINK(link);
33043303
break;
33053304
}
33063305

ext/pgsql/php_pgsql.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ ZEND_BEGIN_MODULE_GLOBALS(pgsql)
185185
zend_long auto_reset_persistent;
186186
int ignore_notices,log_notices;
187187
HashTable notices; /* notice message for each connection */
188-
zval *default_link; /* default link when connection is omitted */
188+
pgsql_link_handle *default_link; /* default link when connection is omitted */
189189
HashTable hashes; /* hashes for each connection */
190190
HashTable field_oids;
191191
HashTable table_oids;

ext/pgsql/tests/09notice.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ try {
4949
}
5050
?>
5151
--EXPECTF--
52-
resource(%d) of type (pgsql result)
52+
object(PgSqlResult)#%d (0) {
53+
}
5354
string(0) ""
5455
array(0) {
5556
}

ext/pgsql/tests/10pg_convert_9.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ Array of values must be an associative array with string keys
6060
Array of values must be an associative array with string keys
6161
Values must be of type string|int|float|bool|null, array given
6262
Values must be of type string|int|float|bool|null, stdClass given
63-
Values must be of type string|int|float|bool|null, resource given
63+
Values must be of type string|int|float|bool|null, PgSql given

ext/pgsql/tests/12pg_insert_9.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ echo "Ok\n";
5454
--EXPECTF--
5555
INSERT INTO "php_pgsql_test" ("num","str","bin") VALUES (1234,E'AAA',E'\\x424242');
5656
INSERT INTO "php_pgsql_test" ("num","str","bin") VALUES ('1234','AAA','BBB');
57-
resource(%d) of type (pgsql result)
57+
object(PgSqlResult)#%d (0) {
58+
}
5859
Array of values must be an associative array with string keys
5960
Array of values must be an associative array with string keys
6061
Values must be of type string|int|float|bool|null, array given
6162
Values must be of type string|int|float|bool|null, stdClass given
62-
Values must be of type string|int|float|bool|null, resource given
63+
Values must be of type string|int|float|bool|null, PgSql given
6364
Ok

ext/pgsql/tests/13pg_select_9.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,5 @@ Array of values must be an associative array with string keys
8181
Array of values must be an associative array with string keys
8282
Values must be of type string|int|float|bool|null, array given
8383
Values must be of type string|int|float|bool|null, stdClass given
84-
Values must be of type string|int|float|bool|null, resource given
84+
Values must be of type string|int|float|bool|null, PgSql given
8585
Ok

ext/pgsql/tests/80_bug32223.phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ pg_close($dbh);
5353

5454
?>
5555
--EXPECTF--
56-
resource(%d) of type (pgsql result)
57-
resource(%d) of type (pgsql result)
56+
object(PgSqlResult)#%d (0) {
57+
}
58+
object(PgSqlResult)#%d (0) {
59+
}
5860
array(1) {
5961
[0]=>
6062
string(1) "f"

ext/pgsql/tests/connect_after_close.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ include('config.inc');
1010
$db1 = pg_connect($conn_str);
1111
unset($db1);
1212
var_dump(pg_close());
13+
exit;
1314
$db2 = pg_connect($conn_str);
1415
unset($db2);
1516
var_dump(pg_close());

0 commit comments

Comments
 (0)