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: (*RowsEvent).handleUnsigned() panic #856

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions canal/rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ func (r *RowsEvent) handleUnsigned() {

for i := 0; i < len(r.Rows); i++ {
for _, columnIdx := range r.Table.UnsignedColumns {
// When Canal.delay is big, after call Canal.StartFromGTID(),
// we will get the newest table schema (for example, after DDL "alter table add column xxx unsigned..."),
// but the binlog data can be very old (before DDL "alter table add column xxx unsigned..."),
// results in max(columnIdx) >= len(r.Rows[i]), then r.Rows[i][columnIdx] panic.
if columnIdx >= len(r.Rows[i]) {
continue
}
switch value := r.Rows[i][columnIdx].(type) {
case int8:
r.Rows[i][columnIdx] = uint8(value)
Expand Down
61 changes: 61 additions & 0 deletions canal/rows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package canal

import (
lance6716 marked this conversation as resolved.
Show resolved Hide resolved
"github.com/go-mysql-org/go-mysql/replication"

Check failure on line 4 in canal/rows_test.go

View workflow job for this annotation

GitHub Actions / golangci

File is not `goimports`-ed (goimports)
"github.com/go-mysql-org/go-mysql/schema"
"reflect"
"testing"
)

func TestRowsEvent_handleUnsigned(t *testing.T) {
type fields struct {
Table *schema.Table
Action string
Rows [][]interface{}
Header *replication.EventHeader
}
tests := []struct {
name string
fields fields
wantRows [][]interface{}
}{
{
name: "rows_event_handle_unsigned",
fields: fields{
Table: &schema.Table{
// columns 1,3,5,7,9 should be converted from signed to unsigned,
// column 10 is out of range and should be ignored, don't panic.
UnsignedColumns: []int{1, 3, 5, 7, 9, 10},
},
Rows: [][]interface{}{{
int8(8), int8(8),
int16(16), int16(16),
int32(32), int32(32),
int64(64), int64(64),
int(128), int(128)},
},
},
wantRows: [][]interface{}{{
int8(8), uint8(8),
int16(16), uint16(16),
int32(32), uint32(32),
int64(64), uint64(64),
int(128), uint(128)},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &RowsEvent{
Table: tt.fields.Table,
Action: tt.fields.Action,
Rows: tt.fields.Rows,
Header: tt.fields.Header,
}
r.handleUnsigned()
if !reflect.DeepEqual(tt.fields.Rows, tt.wantRows) {
t.Errorf("rows=%+v, wantRows=%+v, not equal", tt.fields.Rows, tt.wantRows)
}
lance6716 marked this conversation as resolved.
Show resolved Hide resolved
})
}
}
Loading