From 08bcb235bdd0572cb2f24fd463101216f916f0fc Mon Sep 17 00:00:00 2001 From: tangenta Date: Wed, 24 Nov 2021 10:35:51 +0800 Subject: [PATCH 1/3] cherry pick #29925 to release-5.1 Signed-off-by: ti-srebot --- ddl/backfilling.go | 7 +++++++ tablecodec/tablecodec_test.go | 14 ++++++++++++++ util/codec/codec.go | 6 ++++++ 3 files changed, 27 insertions(+) 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..36d75debc9727 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 TestTableCodecInvalid(t *testing.T) { + 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) + require.NotNil(t, err) + require.Equal(t, "invalid encoded key", err.Error()) +} + // 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..3bbb658f90b60 100644 --- a/util/codec/codec.go +++ b/util/codec/codec.go @@ -961,7 +961,13 @@ func peek(b []byte) (length int, err error) { return 0, errors.Trace(err) } length += l +<<<<<<< HEAD if length > originLength { +======= + if length <= 0 { + return 0, errors.New("invalid encoded key") + } else if length > originLength { +>>>>>>> d04a1285a... ddl, util/codec: fix add index OOM and prevent panic in logging (#29925) return 0, errors.Errorf("invalid encoded key, "+ "expected length: %d, actual length: %d", length, originLength) } From e074b6b123b4ea7ae86b0f19377a0ba2c2b52596 Mon Sep 17 00:00:00 2001 From: tangenta Date: Wed, 24 Nov 2021 21:09:56 +0800 Subject: [PATCH 2/3] resolve conflict --- util/codec/codec.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/util/codec/codec.go b/util/codec/codec.go index 3bbb658f90b60..4d16470d56537 100644 --- a/util/codec/codec.go +++ b/util/codec/codec.go @@ -961,13 +961,9 @@ func peek(b []byte) (length int, err error) { return 0, errors.Trace(err) } length += l -<<<<<<< HEAD - if length > originLength { -======= if length <= 0 { return 0, errors.New("invalid encoded key") } else if length > originLength { ->>>>>>> d04a1285a... ddl, util/codec: fix add index OOM and prevent panic in logging (#29925) return 0, errors.Errorf("invalid encoded key, "+ "expected length: %d, actual length: %d", length, originLength) } From 618d3decd090319da78b30a9c49ade59c0f14c44 Mon Sep 17 00:00:00 2001 From: bb7133 Date: Mon, 7 Feb 2022 13:37:56 +0800 Subject: [PATCH 3/3] fix conflicts --- tablecodec/tablecodec_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tablecodec/tablecodec_test.go b/tablecodec/tablecodec_test.go index 36d75debc9727..cf461438cf82b 100644 --- a/tablecodec/tablecodec_test.go +++ b/tablecodec/tablecodec_test.go @@ -55,7 +55,7 @@ func (s *testTableCodecSuite) TestTableCodec(c *C) { } // https://github.com/pingcap/tidb/issues/27687. -func TestTableCodecInvalid(t *testing.T) { +func (s *testTableCodecSuite) TestTableCodecInvalid(c *C) { tableID := int64(100) buf := make([]byte, 0, 11) buf = append(buf, 't') @@ -64,8 +64,8 @@ func TestTableCodecInvalid(t *testing.T) { buf = codec.EncodeInt(buf, -9078412423848787968) buf = append(buf, '0') _, err := DecodeRowKey(buf) - require.NotNil(t, err) - require.Equal(t, "invalid encoded key", err.Error()) + c.Assert(err, NotNil) + c.Assert(err.Error(), Equals, "invalid encoded key") } // column is a structure used for test