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

SPMySQL: -[SPMySQLResult _stringWithBytes:length:] charset issue #2150

Closed
dmoagx opened this Issue Jun 18, 2015 · 1 comment

Comments

Projects
None yet
1 participant
@dmoagx
Member

dmoagx commented Jun 18, 2015

Above method can fail and return nil in a context where nil is not allowed.
This can only happen if the input contains invalid characters for the selected encoding.

Crash reports:
http://log.sequelpro.com/view/3771

Hypothesis:
a) The MySQL data contains invalid byte sequences (ie. a variant of the "UTF-8 via Latin1" problem)
b) Sequel Pro tries to convert the C string from a different encoding than what was actually used to retrieve the data

Interestingly this mostly affects users with CJK and Russian system locales.

@dmoagx dmoagx added the Bug label Jun 18, 2015

@dmoagx

This comment has been minimized.

Show comment
Hide comment
@dmoagx

dmoagx Jun 18, 2015

Member

The last user comment is actual helpful in reproducing:

Create the two tables (the database encoding should be latin1):

CREATE TABLE `question` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `userId` int(11) DEFAULT NULL,
  `setId` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `questionAnswer` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `setId` int(11) DEFAULT NULL,
  `partnerId` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Then paste the query into the query editor

SELECT (
#DELETE FROM question WHERE id IN (
            # 自分への回答があった質問のセットIDと、自分の最新の質問のセットID
            # を持つ質問レコードのID
            SELECT id FROM question WHERE userId=1 AND setId IN (
                # 自分への回答があった質問のセットID
                (SELECT setId FROM questionAnswer WHERE partnerId=1),
                # 自分の最新の質問のセットID
                (SELECT MAX(setId) FROM question WHERE userId=1)
            )
        )

AND manually select "View using Encoding" > "utf8".
Run query -> Exception.

Alternative: After having created the tables relaunch Sequel Pro, connect to the server and only select a database. Sequel Pro should still be in the "default" utf-8 mode.
Same issue when running the query.

The debugger shows:

(lldb) p (char *)bytes
(char *) $241 = 0x00000001019364b0 "(\n#DELETE FROM question WHERE id IN (\n\t\t\t# 自分への回答があった質問のセットIDと、自分の最新の質問のセットID\n\t\t\t# を持つ質問レコードのID\n\t\t\tSELECT id FROM question WHERE userId=1 AND setId IN (\n\t\t\t\t# 自分への回\347\255
(lldb) p length
(NSUInteger) $242 = 256

So mysql truncates the "column name" right in the middle of a UTF-8 character, creating an invalid sequence.

Member

dmoagx commented Jun 18, 2015

The last user comment is actual helpful in reproducing:

Create the two tables (the database encoding should be latin1):

CREATE TABLE `question` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `userId` int(11) DEFAULT NULL,
  `setId` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `questionAnswer` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `setId` int(11) DEFAULT NULL,
  `partnerId` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Then paste the query into the query editor

SELECT (
#DELETE FROM question WHERE id IN (
            # 自分への回答があった質問のセットIDと、自分の最新の質問のセットID
            # を持つ質問レコードのID
            SELECT id FROM question WHERE userId=1 AND setId IN (
                # 自分への回答があった質問のセットID
                (SELECT setId FROM questionAnswer WHERE partnerId=1),
                # 自分の最新の質問のセットID
                (SELECT MAX(setId) FROM question WHERE userId=1)
            )
        )

AND manually select "View using Encoding" > "utf8".
Run query -> Exception.

Alternative: After having created the tables relaunch Sequel Pro, connect to the server and only select a database. Sequel Pro should still be in the "default" utf-8 mode.
Same issue when running the query.

The debugger shows:

(lldb) p (char *)bytes
(char *) $241 = 0x00000001019364b0 "(\n#DELETE FROM question WHERE id IN (\n\t\t\t# 自分への回答があった質問のセットIDと、自分の最新の質問のセットID\n\t\t\t# を持つ質問レコードのID\n\t\t\tSELECT id FROM question WHERE userId=1 AND setId IN (\n\t\t\t\t# 自分への回\347\255
(lldb) p length
(NSUInteger) $242 = 256

So mysql truncates the "column name" right in the middle of a UTF-8 character, creating an invalid sequence.

@dmoagx dmoagx closed this in 2b52f76 Jun 19, 2015

dmoagx added a commit that referenced this issue Mar 3, 2016

@dmoagx dmoagx added this to the 1.1.2 milestone Apr 4, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment