Skip to content

Protocol.authenticate expects @charset to be defined #30

@slowjoe007

Description

@slowjoe007

If an error occurs during authentication (in my case the host I connect from is not permitted), the error message is attempted to be decoded.
However, at that time @charset is not yet defined:

Uncaught exception: undefined method `encoding' for nil:NilClass
	C:/Ruby/2.3.0/lib/ruby/gems/2.3.0/gems/ruby-mysql-2.9.14/lib/mysql/protocol.rb:502:in `read'
	C:/Ruby/2.3.0/lib/ruby/gems/2.3.0/gems/ruby-mysql-2.9.14/lib/mysql/protocol.rb:178:in `authenticate'
	C:/Ruby/2.3.0/lib/ruby/gems/2.3.0/gems/ruby-mysql-2.9.14/lib/mysql.rb:116:in `connect'
	C:/Ruby/2.3.0/lib/ruby/gems/2.3.0/gems/ruby-mysql-2.9.14/lib/mysql.rb:50:in `new'

message.force_encoding(@charset.encoding)

As you can see from the stacktrace the line in method authenticate is 178. However, @charset is not defined before line 185:

init_packet = InitialPacket.parse read
@server_info = init_packet.server_version
@server_version = init_packet.server_version.split(/\D/)[0,3].inject{|a,b|a.to_i*100+b.to_i}
@thread_id = init_packet.thread_id
client_flags = CLIENT_LONG_PASSWORD | CLIENT_LONG_FLAG | CLIENT_TRANSACTIONS | CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION
client_flags |= CLIENT_CONNECT_WITH_DB if db
client_flags |= flag
@charset = charset
unless @charset
@charset = Charset.by_number(init_packet.server_charset)
@charset.encoding # raise error if unsupported charset
end

I was able to help myself by modifying line 502 to:

message.force_encoding(Mysql::Charset.by_number(33).encoding)

where 33 is the numeric representation of UTF-8:

[ 33, "utf8", "utf8_general_ci", true ],

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions