Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostgreSQL recordset close fails when query id == -1 #848

Closed
Mike-Benoit opened this issue Jul 19, 2022 · 4 comments · Fixed by #851
Closed

PostgreSQL recordset close fails when query id == -1 #848

Mike-Benoit opened this issue Jul 19, 2022 · 4 comments · Fixed by #851
Assignees
Labels
bug pgsql PostgreSQL (Tier 1)
Milestone

Comments

@Mike-Benoit
Copy link
Contributor

Mike-Benoit commented Jul 19, 2022

driver is broken due to regression introduced by #834, which has changed _queryId checks to be something like:

if ( $this->_queryID === false ) { ... }

However _queryId can be false, but it is also -1 quite often. See Line 392 of adodb.inc.php and Line 79 of adodb-csvlib.inc.php as a few examples. There are also several places where -1 might be used as a dummy ID so it still passes checks like:

if ( $this->_queryID )
@Mike-Benoit
Copy link
Contributor Author

The below IF statement in the _close() function at least works around a PHP fatal error (passing -1 to pg_free_result() ) in our testing, but more thought may need to go into how queryID handling is performed in regards to "-1" and being backwards compatible with PHP < v8.1

drivers/adodb-postgres64.inc.php

function _close()
	{
		if ($this->_queryID === false || $this->_queryID == -1 ) {
			return true;
		}
		return pg_free_result($this->_queryID);
	}

@dregad dregad added bug pgsql PostgreSQL (Tier 1) labels Jul 22, 2022
@dregad dregad added this to the v5.22.3 milestone Jul 22, 2022
@dregad dregad self-assigned this Jul 22, 2022
@dregad
Copy link
Member

dregad commented Jul 22, 2022

See Line 392 of adodb.inc.php

@Mike-Benoit I assume you actually meant 3932 here ?

ADOdb/adodb.inc.php

Lines 3931 to 3932 in a567820

/** @var resource result link identifier */
var $_queryID = -1;

For consistency, I think we should change that value to false, matching ADOConnection::$_queryID, although this is somewhat pointless since the property is always initialized in the class constructor anyway.

This normally happens after executing a query from the connection, when the recordset is created. The only 2 exceptions to this I can think of, are:

  • the csvlib example you mentioned
  • In _adodb_getinsertsql() (adodb-lib.inc.php) where we create a dummy recordset so we can call metaType()

Do you know about any other ?

In any case, considering that ADORecordSet::__destruct() calls close(), I think your suggestion is a correct way to fix this; alternatively we could perform the check in the destructor, and not attempt to close the RS if it's a "dummy" one

function __destruct() {
	if($this->_queryID != -1) {
		$this->Close();
	}
}

Also, to be clean and avoid future confusion with this use of -1 magic constant, I think we should define and use a DUMMY_QUERY_ID = -1 constant instead.

dregad added a commit that referenced this issue Jul 23, 2022
Replace hardcoded '-1' value when creating dummy RecordSets.

Issue #848
@dregad dregad linked a pull request Jul 23, 2022 that will close this issue
@dregad dregad changed the title PostgreSQL driver is broken due to 834 PostgreSQL recordset close fails when query id == -1 Jul 23, 2022
@dregad
Copy link
Member

dregad commented Jul 23, 2022

Please review and test PR #851.

@Mike-Benoit
Copy link
Contributor Author

Your PR looks like a more thorough fix, and is passing our own internal unit tests.

dregad added a commit that referenced this issue Aug 26, 2022
Replace hardcoded '-1' value when creating dummy RecordSets.

Issue #848
@dregad dregad closed this as completed in 721c314 Aug 26, 2022
dregad added a commit that referenced this issue Sep 9, 2022
Partial revert of 721c314.

This was an early attempt to fix #848; the actual fix was implemented in
the PostgreSQL driver, but the change to ADORecordSet::__destruct() was
not reverted.

Fixes #867
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug pgsql PostgreSQL (Tier 1)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants