Skip to content

Commit b1979c3

Browse files
committed
Get rid of memory leak, improve default connection handling, fix a few tests
1 parent 483db41 commit b1979c3

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) { \
@@ -290,10 +290,8 @@ static inline char * _php_pgsql_trim_result(PGconn * pgsql, char **buf)
290290
efree(msgbuf); \
291291
} \
292292

293-
static void php_pgsql_set_default_link(zval *link)
293+
static void php_pgsql_set_default_link(pgsql_link_handle *link)
294294
{
295-
GC_ADDREF(Z_OBJ_P(link));
296-
297295
if (PGG(default_link) != NULL) {
298296
pgsql_link_free(FETCH_DEFAULT_LINK());
299297
}
@@ -733,7 +731,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
733731
*/
734732
if (!(connect_type & PGSQL_CONNECT_FORCE_NEW)
735733
&& (index_ptr = zend_hash_find_ptr(&PGG(regular_list), str.s)) != NULL) {
736-
php_pgsql_set_default_link(index_ptr);
734+
php_pgsql_set_default_link(pgsql_link_from_obj(Z_OBJ_P(index_ptr)));
737735
GC_ADDREF(Z_OBJ_P(index_ptr));
738736
ZVAL_COPY(return_value, index_ptr);
739737

@@ -771,7 +769,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
771769

772770
/* add it to the hash */
773771
ZVAL_COPY(&new_index_ptr, return_value);
774-
zend_hash_update_mem(&PGG(regular_list), str.s, (void *) &new_index_ptr, sizeof(zval));
772+
zend_hash_update(&PGG(regular_list), str.s, &new_index_ptr);
775773

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

793791
cleanup:
794792
smart_str_free(&str);
@@ -849,8 +847,8 @@ PHP_FUNCTION(pg_close)
849847
if (!pgsql_link) {
850848
link = FETCH_DEFAULT_LINK();
851849
CHECK_DEFAULT_LINK(link);
852-
pgsql_link_free(link);
853850
PGG(default_link) = NULL;
851+
pgsql_link_free(link);
854852
RETURN_TRUE;
855853
}
856854

@@ -2371,12 +2369,13 @@ PHP_FUNCTION(pg_lo_create)
23712369
} else if ((Z_TYPE_P(pgsql_link) == IS_OBJECT && instanceof_function(Z_OBJCE_P(pgsql_link), pgsql_link_ce))) {
23722370
link = Z_PGSQL_LINK_P(pgsql_link);
23732371
CHECK_PGSQL_LINK(link);
2374-
pgsql = link->conn;
23752372
} else {
23762373
zend_argument_type_error(1, "must be of type PgSql when the connection is provided");
23772374
RETURN_THROWS();
23782375
}
23792376

2377+
pgsql = link->conn;
2378+
23802379
if (oid) {
23812380
switch (Z_TYPE_P(oid)) {
23822381
case IS_STRING:
@@ -3341,7 +3340,7 @@ PHP_FUNCTION(pg_escape_string)
33413340
RETURN_THROWS();
33423341
}
33433342
link = Z_PGSQL_LINK_P(pgsql_link);
3344-
CHECK_PGSQL_LINK(link);
3343+
CHECK_PGSQL_LINK(link);
33453344
break;
33463345
}
33473346

ext/pgsql/php_pgsql.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ ZEND_BEGIN_MODULE_GLOBALS(pgsql)
186186
zend_long auto_reset_persistent;
187187
int ignore_notices,log_notices;
188188
HashTable notices; /* notice message for each connection */
189-
zval *default_link; /* default link when connection is omitted */
189+
pgsql_link_handle *default_link; /* default link when connection is omitted */
190190
HashTable hashes; /* hashes for each connection */
191191
HashTable regular_list; /* connection list */
192192
ZEND_END_MODULE_GLOBALS(pgsql)

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)