Skip to content

Commit 4a1b2e9

Browse files
authored
ddl, util/codec: fix add index OOM and prevent panic in logging (#29925) (#30124)
1 parent 79e237d commit 4a1b2e9

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

ddl/backfilling.go

+7
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,13 @@ func (w *worker) handleReorgTasks(reorgInfo *reorgInfo, totalAddedCount *int64,
425425
}
426426

427427
func tryDecodeToHandleString(key kv.Key) string {
428+
defer func() {
429+
if r := recover(); r != nil {
430+
logutil.BgLogger().Warn("tryDecodeToHandleString panic",
431+
zap.Any("recover()", r),
432+
zap.Binary("key", key))
433+
}
434+
}()
428435
handle, err := tablecodec.DecodeRowKey(key)
429436
if err != nil {
430437
recordPrefixIdx := bytes.Index(key, []byte("_r"))

tablecodec/tablecodec_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ func TestTableCodec(t *testing.T) {
4646
require.Equal(t, int64(2), h.IntValue())
4747
}
4848

49+
// https://github.com/pingcap/tidb/issues/27687.
50+
func TestTableCodecInvalid(t *testing.T) {
51+
tableID := int64(100)
52+
buf := make([]byte, 0, 11)
53+
buf = append(buf, 't')
54+
buf = codec.EncodeInt(buf, tableID)
55+
buf = append(buf, '_', 'r')
56+
buf = codec.EncodeInt(buf, -9078412423848787968)
57+
buf = append(buf, '0')
58+
_, err := DecodeRowKey(buf)
59+
require.NotNil(t, err)
60+
require.Equal(t, "invalid encoded key", err.Error())
61+
}
62+
4963
// column is a structure used for test
5064
type column struct {
5165
id int64

util/codec/codec.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ func peek(b []byte) (length int, err error) {
962962
return 0, errors.Trace(err)
963963
}
964964
length += l
965-
if length < 0 {
965+
if length <= 0 {
966966
return 0, errors.New("invalid encoded key")
967967
} else if length > originLength {
968968
return 0, errors.Errorf("invalid encoded key, "+

0 commit comments

Comments
 (0)