Skip to content

Commit d34fe2a

Browse files
authored
fix: parseDDL index out of range when characters take up more than one byte (#91)
1 parent e2e9a8a commit d34fe2a

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

ddlmod.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@ func parseDDL(strs ...string) (*ddl, error) {
3737
quote rune
3838
buf string
3939
)
40+
ddlBodyRunesLen := len(ddlBodyRunes)
4041

4142
result.head = sections[1]
4243

43-
for idx := 0; idx < len(ddlBodyRunes); idx++ {
44+
for idx := 0; idx < ddlBodyRunesLen; idx++ {
4445
var (
4546
next rune = 0
4647
c = ddlBodyRunes[idx]
4748
)
48-
if idx+1 < len(ddlBody) {
49-
next = []rune(ddlBody)[idx+1]
49+
if idx+1 < ddlBodyRunesLen {
50+
next = ddlBodyRunes[idx+1]
5051
}
5152

5253
if sc := string(c); separatorRegexp.MatchString(sc) {

ddlmod_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ func TestParseDDL(t *testing.T) {
3636
},
3737
},
3838
{"no brackets", []string{"create table test"}, 0, nil},
39+
{"with_special_characters", []string{
40+
"CREATE TABLE `test` (`text` varchar(10) DEFAULT \"测试,\")",
41+
}, 1, []migrator.ColumnType{
42+
{NameValue: sql.NullString{String: "text", Valid: true}, DataTypeValue: sql.NullString{String: "varchar(10)", Valid: true}, ColumnTypeValue: sql.NullString{String: "varchar(10)", Valid: true}, DefaultValueValue: sql.NullString{String: "测试,", Valid: true}, NullableValue: sql.NullBool{Valid: true}, UniqueValue: sql.NullBool{Valid: true}, PrimaryKeyValue: sql.NullBool{Valid: true}},
43+
},
44+
},
3945
}
4046

4147
for _, p := range params {

0 commit comments

Comments
 (0)