错误现象
runtime error: slice bounds out of range [:7] with capacity 4 goroutine 116 [running]: github.com/hanchuanchuan/goInception/session.(*session).checkModifyColumn() /home/circleci/go/src/github.com/hanchuanchuan/goInception/session/session_inception.go:4318
触发场景
SQL操作:ALTER TABLE 修改字段类型
具体情况:将 text 类型字段改为 varchar 类型
技术原因分析
在 session/session_inception.go 的 checkModifyColumn 函数中:
// 问题代码(第4317行)
switch nc.Tp.Tp {
case mysql.TypeDecimal, mysql.TypeNewDecimal,
mysql.TypeVarchar, // varchar类型在这里处理
mysql.TypeVarString:
str := string([]byte(foundField.Type)[:7]) // 试图取前7个字符
// 进行类型比较...
case mysql.TypeString:
str := string([]byte(foundField.Type)[:4]) // 试图取前4个字符
// 进行类型比较...
}
处理顺序:varchar 类型在 switch 语句中排在前面,需要取前7个字符
字符串长度:当原字段类型是 "text"(4个字符)时,试图取前7个字符就会越界
对比其他类型:
int 类型走 default 分支,不涉及字符串切片
char 类型只需要前4个字符,相对安全
错误现象
runtime error: slice bounds out of range [:7] with capacity 4 goroutine 116 [running]: github.com/hanchuanchuan/goInception/session.(*session).checkModifyColumn() /home/circleci/go/src/github.com/hanchuanchuan/goInception/session/session_inception.go:4318触发场景
SQL操作:ALTER TABLE 修改字段类型
具体情况:将 text 类型字段改为 varchar 类型
技术原因分析
在 session/session_inception.go 的 checkModifyColumn 函数中:
// 问题代码(第4317行)
switch nc.Tp.Tp {
case mysql.TypeDecimal, mysql.TypeNewDecimal,
mysql.TypeVarchar, // varchar类型在这里处理
mysql.TypeVarString:
str := string([]byte(foundField.Type)[:7]) // 试图取前7个字符
// 进行类型比较...
case mysql.TypeString:
str := string([]byte(foundField.Type)[:4]) // 试图取前4个字符
// 进行类型比较...
}
处理顺序:varchar 类型在 switch 语句中排在前面,需要取前7个字符
字符串长度:当原字段类型是 "text"(4个字符)时,试图取前7个字符就会越界
对比其他类型:
int 类型走 default 分支,不涉及字符串切片
char 类型只需要前4个字符,相对安全