Skip to content

Do not require an open connection for MySQL string escaping #1

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

Open
wants to merge 1 commit into
base: use-vio-is-connected
Choose a base branch
from

Conversation

csfrancis
Copy link
Owner

@csfrancis csfrancis commented Mar 12, 2019

The detection of closed connections was made more robust by brianmario#1022. As a result, in situations where the connection was actually closed, calls to rb_mysql_client_real_escape are now raising an exception when they would not have before.

This modifies the behaviour of the mysql2 gem to not require an open connection to perform an underlying call to mysql_real_escape_string. This is effectively emulating the same behaviour as before brianmario#1022.

I tried to write a test for the negative case that would raise when we haven't connected at least once. I could not come up with a way to create a Mysql2::Client instance without performing a successful connect.

Copy link

@jeremycole jeremycole left a comment

Choose a reason for hiding this comment

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

👍 🥇

@@ -887,11 +887,11 @@ def run_gc
end.not_to raise_error
end

it "should require an open connection" do
it "should not require an open connection" do
Copy link

Choose a reason for hiding this comment

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

Should we change this to allows to escape on a closed connection?

Copy link
Owner Author

Choose a reason for hiding this comment

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

I feel like it's basically the same thing. It's running in the #escape context.

@@ -50,6 +52,12 @@ static ID intern_brackets, intern_merge, intern_merge_bang, intern_new_with_args
rb_raise(cMysql2Error, "MySQL connection is already open"); \
}

#define REQUIRE_CONNECTED_ONCE(wrapper) \
REQUIRE_INITIALIZED(wrapper) \
if (!wrapper->client->server_version) { \
Copy link

Choose a reason for hiding this comment

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

Some others places in client.c use wrapper->server_version, is there a difference with wrapper->client->server_version?

Copy link
Owner Author

Choose a reason for hiding this comment

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

From my precursory look, it appears that wrapper->server_version is used to cache the response of mysql_get_server_version, which is an integer. wrapper->client->server_version should be a string representation of the server version that is set by the mysql client library upon connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants