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

ddl, util/codec: fix add index OOM and prevent panic in logging (#29925) #30123

Merged
merged 9 commits into from
Feb 9, 2022
7 changes: 7 additions & 0 deletions ddl/backfilling.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,13 @@ func (w *worker) handleReorgTasks(reorgInfo *reorgInfo, totalAddedCount *int64,
}

func tryDecodeToHandleString(key kv.Key) string {
defer func() {
if r := recover(); r != nil {
logutil.BgLogger().Warn("tryDecodeToHandleString panic",
zap.Any("recover()", r),
zap.Binary("key", key))
}
}()
handle, err := tablecodec.DecodeRowKey(key)
if err != nil {
recordPrefixIdx := bytes.Index(key, []byte("_r"))
Expand Down
14 changes: 14 additions & 0 deletions tablecodec/tablecodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ func (s *testTableCodecSuite) TestTableCodec(c *C) {
c.Assert(h.IntValue(), Equals, int64(2))
}

// https://github.com/pingcap/tidb/issues/27687.
func (s *testTableCodecSuite) TestTableCodecInvalid(c *C) {
tableID := int64(100)
buf := make([]byte, 0, 11)
buf = append(buf, 't')
buf = codec.EncodeInt(buf, tableID)
buf = append(buf, '_', 'r')
buf = codec.EncodeInt(buf, -9078412423848787968)
buf = append(buf, '0')
_, err := DecodeRowKey(buf)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "invalid encoded key")
}

// column is a structure used for test
type column struct {
id int64
Expand Down
4 changes: 3 additions & 1 deletion util/codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,9 @@ func peek(b []byte) (length int, err error) {
return 0, errors.Trace(err)
}
length += l
if length > originLength {
if length <= 0 {
return 0, errors.New("invalid encoded key")
} else if length > originLength {
return 0, errors.Errorf("invalid encoded key, "+
"expected length: %d, actual length: %d", length, originLength)
}
Expand Down