Skip to content

Commit c05c20b

Browse files
committed
Some fixes
1 parent 7a2c758 commit c05c20b

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

ext/pgsql/pgsql.c

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

8384
#define CHECK_PGSQL_LINK(link_handle) \
8485
if (link_handle->conn == NULL) { \
@@ -283,11 +284,15 @@ static zend_string *_php_pgsql_trim_message(const char *message)
283284
zend_string_release(msgbuf); \
284285
} \
285286

286-
static void php_pgsql_set_default_link(pgsql_link_handle *link)
287+
static void php_pgsql_set_default_link(zend_object *obj)
287288
{
288-
GC_ADDREF(res);
289+
GC_ADDREF(obj);
289290

290-
PGG(default_link) = link;
291+
if (PGG(default_link) != NULL) {
292+
GC_DELREF(obj);
293+
}
294+
295+
PGG(default_link) = obj;
291296
}
292297

293298
static void _close_pgsql_plink(zend_resource *rsrc)
@@ -715,15 +720,14 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
715720
} else { /* Non persistent connection */
716721
zval *index_ptr, new_index_ptr;
717722

718-
/* first we check the hash for the hashed_details key. if it exists,
723+
/* first we check the hash for the hashed_details key. If it exists,
719724
* it should point us to the right offset where the actual pgsql link sits.
720725
* if it doesn't, open a new pgsql link, add it to the resource list,
721726
* and add a pointer to it with hashed_details as the key.
722727
*/
723728
if (!(connect_type & PGSQL_CONNECT_FORCE_NEW)
724729
&& (index_ptr = zend_hash_find_ptr(&PGG(connections), str.s)) != NULL) {
725-
php_pgsql_set_default_link(pgsql_link_from_obj(Z_OBJ_P(index_ptr)));
726-
GC_ADDREF(Z_OBJ_P(index_ptr));
730+
php_pgsql_set_default_link(Z_OBJ_P(index_ptr));
727731
ZVAL_COPY(return_value, index_ptr);
728732

729733
goto cleanup;
@@ -776,7 +780,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
776780
if (! PGG(ignore_notices) && Z_TYPE_P(return_value) == IS_OBJECT) {
777781
PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, link);
778782
}
779-
php_pgsql_set_default_link(link);
783+
php_pgsql_set_default_link(Z_OBJ_P(return_value));
780784

781785
cleanup:
782786
smart_str_free(&str);
@@ -838,7 +842,9 @@ PHP_FUNCTION(pg_close)
838842
link = FETCH_DEFAULT_LINK();
839843
CHECK_DEFAULT_LINK(link);
840844
zend_hash_del(&PGG(connections), link->hash);
845+
GC_DELREF(PGG(default_link));
841846
PGG(default_link) = NULL;
847+
pgsql_link_free(link);
842848
RETURN_TRUE;
843849
}
844850

@@ -847,6 +853,7 @@ PHP_FUNCTION(pg_close)
847853

848854
if (link == FETCH_DEFAULT_LINK()) {
849855
zend_hash_del(&PGG(connections), link->hash);
856+
GC_DELREF(PGG(default_link));
850857
PGG(default_link) = NULL;
851858
}
852859
pgsql_link_free(link);

ext/pgsql/php_pgsql.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ ZEND_BEGIN_MODULE_GLOBALS(pgsql)
187187
zend_long allow_persistent;
188188
zend_long auto_reset_persistent;
189189
int ignore_notices,log_notices;
190-
pgsql_link_handle *default_link; /* default link when connection is omitted */
190+
zend_object *default_link; /* default link when connection is omitted */
191191
HashTable hashes; /* hashes for each connection */
192192
HashTable field_oids;
193193
HashTable table_oids;

0 commit comments

Comments
 (0)