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

Fix error handling in mysqli driver #752

Merged
merged 1 commit into from
Dec 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,15 @@ public function __construct(array $params, $username, $password, array $driverOp

$this->setDriverOptions($driverOptions);

$previousHandler = set_error_handler(function () {
});
set_error_handler(function () {});

if ( ! $this->_conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) {
set_error_handler($previousHandler);
restore_error_handler();

$sqlState = 'HY000';
if (@$this->_conn->sqlstate) {
$sqlState = $this->_conn->sqlstate;
}

throw new MysqliException($this->_conn->connect_error, $sqlState, $this->_conn->connect_errno);
throw new MysqliException($this->_conn->connect_error, @$this->_conn->sqlstate ?: 'HY000', $this->_conn->connect_errno);
}

set_error_handler($previousHandler);
restore_error_handler();

if (isset($params['charset'])) {
$this->_conn->set_charset($params['charset']);
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public function fetchColumn($columnIndex = 0)
return false;
}

return $row[$columnIndex];
return isset($row[$columnIndex]) ? $row[$columnIndex] : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This other changed code-path requires a test case

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is one: the one that was failing before the patch :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it was explicitly testing for non-existing $columnIndex?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright then :-)

@nicolas-grekas I'm always this paranoid just to make sure ;-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ocramius @nicolas-grekas is right, there were tests introduced in #709. What is weird is that the tests were not failing in that PR already oO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deeky666 if the error handler kicked in, then of course the tests passed :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ocramius but temporarily disabling the error handler is just for grabbing connection errors, no? It is restored afterwards anyways so failing queries are not affected by this or am I getting something wrong here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was not restored correctly before the patch. The side effect of this bug was that the silencing error handler was still in place. Thus, all later errors were silenced.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas I see. Thought it was just a "cleaner" implementation. So merging then. Thanks a lot! :)

}

/**
Expand Down