Skip to content

Commit

Permalink
planner/core: fix nil pointer dereference tidb_decode_key() (#33018)
Browse files Browse the repository at this point in the history
close #33015
  • Loading branch information
tangenta authored Mar 12, 2022
1 parent 5170f2f commit b4c4b83
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
11 changes: 8 additions & 3 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2816,7 +2816,7 @@ func TestTiDBDecodeKeyFunc(t *testing.T) {
result.Check(testkit.Rows("7480000000000000FF2E5F728000000011FFE1A3000000000000"))
warns := tk.Session().GetSessionVars().StmtCtx.GetWarnings()
require.Len(t, warns, 1)
require.Error(t, warns[0].Err, "invalid record/index key: 7480000000000000FF2E5F728000000011FFE1A3000000000000")
require.EqualError(t, warns[0].Err, "invalid key: 7480000000000000FF2E5F728000000011FFE1A3000000000000")

// Test in real tables.
tk.MustExec("use test;")
Expand Down Expand Up @@ -2879,10 +2879,15 @@ func TestTiDBDecodeKeyFunc(t *testing.T) {
result.Check(testkit.Rows(rs))

// https://github.com/pingcap/tidb/issues/27434.
hexKey = "7480000000000000375F69800000000000000103800000000001D4C1023B6458"
sql = fmt.Sprintf("select tidb_decode_key( '%s' )", hexKey)
hexKey = "7480000000000100375F69800000000000000103800000000001D4C1023B6458"
sql = fmt.Sprintf("select tidb_decode_key('%s')", hexKey)
tk.MustQuery(sql).Check(testkit.Rows(hexKey))

// https://github.com/pingcap/tidb/issues/33015.
hexKey = "74800000000000012B5F72800000000000A5D3"
sql = fmt.Sprintf("select tidb_decode_key('%s')", hexKey)
tk.MustQuery(sql).Check(testkit.Rows(`{"_tidb_rowid":42451,"table_id":"299"}`))

// Test the table with the nonclustered index.
const rowID = 10
tk.MustExec("drop table if exists t;")
Expand Down
28 changes: 17 additions & 11 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2043,9 +2043,10 @@ func hasCurrentDatetimeDefault(col *table.Column) bool {
}

func decodeKeyFromString(ctx sessionctx.Context, s string) string {
sc := ctx.GetSessionVars().StmtCtx
key, err := hex.DecodeString(s)
if err != nil {
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("invalid record/index key: %X", key))
sc.AppendWarning(errors.Errorf("invalid key: %X", key))
return s
}
// Auto decode byte if needed.
Expand All @@ -2055,39 +2056,44 @@ func decodeKeyFromString(ctx sessionctx.Context, s string) string {
}
tableID := tablecodec.DecodeTableID(key)
if tableID == 0 {
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("invalid record/index key: %X", key))
sc.AppendWarning(errors.Errorf("invalid key: %X", key))
return s
}
dm := domain.GetDomain(ctx)
if dm == nil {
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("domain not found when decoding record/index key: %X", key))
sc.AppendWarning(errors.Errorf("domain not found when decoding key: %X", key))
return s
}
tbl, _ := dm.InfoSchema().TableByID(tableID)
is := dm.InfoSchema()
if is == nil {
sc.AppendWarning(errors.Errorf("infoschema not found when decoding key: %X", key))
return s
}
tbl, _ := is.TableByID(tableID)
loc := ctx.GetSessionVars().Location()
if tablecodec.IsRecordKey(key) {
ret, err := decodeRecordKey(key, tableID, tbl, loc)
if err != nil {
ctx.GetSessionVars().StmtCtx.AppendWarning(err)
sc.AppendWarning(err)
return s
}
return ret
} else if tablecodec.IsIndexKey(key) {
ret, err := decodeIndexKey(key, tableID, tbl, loc)
if err != nil {
ctx.GetSessionVars().StmtCtx.AppendWarning(err)
sc.AppendWarning(err)
return s
}
return ret
} else if tablecodec.IsTableKey(key) {
ret, err := decodeTableKey(key, tableID, tbl, loc)
ret, err := decodeTableKey(key, tableID)
if err != nil {
ctx.GetSessionVars().StmtCtx.AppendWarning(err)
sc.AppendWarning(err)
return s
}
return ret
}
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("invalid record/index key: %X", key))
sc.AppendWarning(errors.Errorf("invalid key: %X", key))
return s
}

Expand All @@ -2100,7 +2106,7 @@ func decodeRecordKey(key []byte, tableID int64, tbl table.Table, loc *time.Locat
ret := make(map[string]interface{})
ret["table_id"] = strconv.FormatInt(tableID, 10)
// When the clustered index is enabled, we should show the PK name.
if tbl.Meta().HasClusteredIndex() {
if tbl != nil && tbl.Meta().HasClusteredIndex() {
ret[tbl.Meta().GetPkName().String()] = handle.IntValue()
} else {
ret["_tidb_rowid"] = handle.IntValue()
Expand Down Expand Up @@ -2235,7 +2241,7 @@ func decodeIndexKey(key []byte, tableID int64, tbl table.Table, loc *time.Locati
return string(retStr), nil
}

func decodeTableKey(key []byte, tableID int64, tbl table.Table, loc *time.Location) (string, error) {
func decodeTableKey(key []byte, tableID int64) (string, error) {
ret := map[string]int64{"table_id": tableID}
retStr, err := json.Marshal(ret)
if err != nil {
Expand Down

0 comments on commit b4c4b83

Please sign in to comment.