From 2e28e1215ab8cf48cee837b88426589cab7d38b9 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Wed, 9 Feb 2022 12:27:36 +0800 Subject: [PATCH] ddl, util/codec: fix add index OOM and prevent panic in logging (#29925) (#30123) close pingcap/tidb#27687 --- ddl/backfilling.go | 7 +++++++ tablecodec/tablecodec_test.go | 14 ++++++++++++++ util/codec/codec.go | 4 +++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/ddl/backfilling.go b/ddl/backfilling.go index 8a5b7a837f552..c6f1b28c42f22 100644 --- a/ddl/backfilling.go +++ b/ddl/backfilling.go @@ -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")) diff --git a/tablecodec/tablecodec_test.go b/tablecodec/tablecodec_test.go index ada284edc0bca..cf461438cf82b 100644 --- a/tablecodec/tablecodec_test.go +++ b/tablecodec/tablecodec_test.go @@ -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 diff --git a/util/codec/codec.go b/util/codec/codec.go index 08ce5a527b192..4d16470d56537 100644 --- a/util/codec/codec.go +++ b/util/codec/codec.go @@ -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) }