Skip to content

Commit aea31ca

Browse files
packets: fix inverted unknown collation check
The handshake only carries a one-byte collation ID. A collation that is not in the collations map can still be used when a charset is set, because it is applied afterwards with SET NAMES <charset> COLLATE <collation>. The check was inverted: it rejected exactly that case and silently ignored unknown collations when no charset was set.
1 parent 4b12a5b commit aea31ca

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎AUTHORS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Minh Quang <minhquang4334 at gmail.com>
9797
Morgan Tocker <tocker at gmail.com>
9898
Nao Yokotsuka <yokotukanao at gmail.com>
9999
Nathanial Murphy <nathanial.murphy at gmail.com>
100+
Ngo Quoc Viet <ngoquocviet2001 at gmail.com>
100101
Nicola Peduzzi <thenikso at gmail.com>
101102
Oliver Bone <owbone at github.com>
102103
Olivier Mengué <dolmen at cpan.org>

‎packets.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,9 @@ func (mc *mysqlConn) handshakeResponseHeader() ([]byte, error) {
342342
colID, ok := collations[cname]
343343
if ok {
344344
data[12] = colID
345-
} else if len(mc.cfg.charsets) > 0 {
346-
// When cfg.charset is set, the collation is set by `SET NAMES <charset> COLLATE <collation>`.
345+
} else if len(mc.cfg.charsets) == 0 {
346+
// Without cfg.charset, the collation can only be set by this 1-byte ID.
347+
// With cfg.charset, it is set by `SET NAMES <charset> COLLATE <collation>`.
347348
return nil, fmt.Errorf("unknown collation: %q", cname)
348349
}
349350
}

‎packets_test.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,3 +502,40 @@ func TestReadColumnsCollation(t *testing.T) {
502502
})
503503
}
504504
}
505+
506+
// The handshake carries a one-byte collation ID. With a charset, a collation
507+
// missing from the collations map is set later by SET NAMES ... COLLATE.
508+
func TestHandshakeCollation(t *testing.T) {
509+
for _, tc := range []struct {
510+
name string
511+
charsets []string
512+
collation string
513+
want byte
514+
wantErr bool
515+
}{
516+
{name: "default", want: defaultCollationID},
517+
{name: "known", collation: "utf8mb4_unicode_ci", want: 224},
518+
{name: "known with charset", charsets: []string{"utf8mb4"}, collation: "utf8mb4_unicode_ci", want: 224},
519+
{name: "unknown with charset", charsets: []string{"utf8mb4"}, collation: "utf8mb4_0900_as_ci", want: defaultCollationID},
520+
{name: "unknown", collation: "utf8mb4_0900_as_ci", wantErr: true},
521+
} {
522+
t.Run(tc.name, func(t *testing.T) {
523+
conn, mc := newRWMockConn(1)
524+
mc.cfg.charsets = tc.charsets
525+
mc.cfg.Collation = tc.collation
526+
err := mc.writeHandshakeResponsePacket(nil, defaultAuthPlugin)
527+
if tc.wantErr {
528+
if err == nil {
529+
t.Fatalf("expected an error for collation %q", tc.collation)
530+
}
531+
return
532+
}
533+
if err != nil {
534+
t.Fatal(err)
535+
}
536+
if got := conn.written[12]; got != tc.want {
537+
t.Errorf("collation ID = %d; want %d", got, tc.want)
538+
}
539+
})
540+
}
541+
}

0 commit comments

Comments
 (0)