Skip to content

Commit 0555a58

Browse files
committed
Get rid of memory leak, improve default connection handling, fix a few tests
1 parent 106c799 commit 0555a58

9 files changed

+25
-21
lines changed

ext/pgsql/pgsql.c

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

8686
#define CHECK_PGSQL_LINK(link_handle) \
8787
if (link_handle->conn == NULL) { \
@@ -293,10 +293,8 @@ static inline char * _php_pgsql_trim_result(PGconn * pgsql, char **buf)
293293
efree(msgbuf); \
294294
} \
295295

296-
static void php_pgsql_set_default_link(zval *link)
296+
static void php_pgsql_set_default_link(pgsql_link_handle *link)
297297
{
298-
GC_ADDREF(Z_OBJ_P(link));
299-
300298
if (PGG(default_link) != NULL) {
301299
pgsql_link_free(FETCH_DEFAULT_LINK());
302300
}
@@ -736,7 +734,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
736734
*/
737735
if (!(connect_type & PGSQL_CONNECT_FORCE_NEW)
738736
&& (index_ptr = zend_hash_find_ptr(&PGG(regular_list), str.s)) != NULL) {
739-
php_pgsql_set_default_link(index_ptr);
737+
php_pgsql_set_default_link(pgsql_link_from_obj(Z_OBJ_P(index_ptr)));
740738
GC_ADDREF(Z_OBJ_P(index_ptr));
741739
ZVAL_COPY(return_value, index_ptr);
742740

@@ -774,7 +772,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
774772

775773
/* add it to the hash */
776774
ZVAL_COPY(&new_index_ptr, return_value);
777-
zend_hash_update_mem(&PGG(regular_list), str.s, (void *) &new_index_ptr, sizeof(zval));
775+
zend_hash_update(&PGG(regular_list), str.s, &new_index_ptr);
778776

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

796794
cleanup:
797795
smart_str_free(&str);
@@ -852,8 +850,8 @@ PHP_FUNCTION(pg_close)
852850
if (!pgsql_link) {
853851
link = FETCH_DEFAULT_LINK();
854852
CHECK_DEFAULT_LINK(link);
855-
pgsql_link_free(link);
856853
PGG(default_link) = NULL;
854+
pgsql_link_free(link);
857855
RETURN_TRUE;
858856
}
859857

@@ -2379,12 +2377,13 @@ PHP_FUNCTION(pg_lo_create)
23792377
} else if ((Z_TYPE_P(pgsql_link) == IS_OBJECT && instanceof_function(Z_OBJCE_P(pgsql_link), pgsql_link_ce))) {
23802378
link = Z_PGSQL_LINK_P(pgsql_link);
23812379
CHECK_PGSQL_LINK(link);
2382-
pgsql = link->conn;
23832380
} else {
23842381
zend_argument_type_error(1, "must be of type PgSql when the connection is provided");
23852382
RETURN_THROWS();
23862383
}
23872384

2385+
pgsql = link->conn;
2386+
23882387
if (oid) {
23892388
switch (Z_TYPE_P(oid)) {
23902389
case IS_STRING:
@@ -3344,7 +3343,7 @@ PHP_FUNCTION(pg_escape_string)
33443343
RETURN_THROWS();
33453344
}
33463345
link = Z_PGSQL_LINK_P(pgsql_link);
3347-
CHECK_PGSQL_LINK(link);
3346+
CHECK_PGSQL_LINK(link);
33483347
break;
33493348
}
33503349

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/bug46408.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ Bug #46408 (Locale number format settings can cause pg_query_params to break wit
33
--SKIPIF--
44
<?php
55
require_once('skipif.inc');
6-
if (false === setlocale(LC_ALL, 'hr_HR.utf-8', 'hr_HR')) {
7-
echo "skip Locale hr_HR.utf-8 not present";
6+
if (false === setlocale(LC_ALL, 'de_DE.utf-8', 'de_DE')) {
7+
echo "skip Locale de_DE.utf-8 not present";
88
}
99
?>
1010
--FILE--
@@ -13,7 +13,7 @@ if (false === setlocale(LC_ALL, 'hr_HR.utf-8', 'hr_HR')) {
1313
require_once('config.inc');
1414

1515
$dbh = pg_connect($conn_str);
16-
setlocale(LC_ALL, 'hr_HR.utf-8', 'hr_HR');
16+
setlocale(LC_ALL, 'de_DE.utf-8', 'de_DE');
1717
echo 3.5 , "\n";
1818
pg_query_params("SELECT $1::numeric", array(3.5));
1919
pg_close($dbh);

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)