Skip to content

Commit 837c06c

Browse files
authored
apicodec: do not decode empty key for codec v2 (#1734)
Signed-off-by: tangenta <[email protected]>
1 parent 2e05e88 commit 837c06c

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

internal/apicodec/codec_v2.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/pkg/errors"
1414
"github.com/tikv/client-go/v2/internal/logutil"
1515
"github.com/tikv/client-go/v2/tikvrpc"
16+
"github.com/tikv/client-go/v2/util/intest"
1617
"github.com/tikv/client-go/v2/util/redact"
1718
"go.uber.org/zap"
1819
)
@@ -705,6 +706,14 @@ func (c *codecV2) EncodeKey(key []byte) []byte {
705706
}
706707

707708
func (c *codecV2) DecodeKey(encodedKey []byte) ([]byte, error) {
709+
if len(encodedKey) == 0 {
710+
if !intest.InTest {
711+
logutil.BgLogger().Warn(
712+
"codecV2.DecodeKey called with empty key. This shouldn't happen in prod",
713+
zap.Stack("stack"))
714+
}
715+
return nil, nil
716+
}
708717
// If the given key does not start with the correct prefix,
709718
// return out of bound error.
710719
if !bytes.HasPrefix(encodedKey, c.prefix) {

util/intest/in_unittest.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2025 TiKV Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build intest
16+
17+
package intest
18+
19+
// InTest checks if the code is running in test.
20+
var InTest = true

util/intest/not_in_unittest.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2025 TiKV Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build !intest
16+
17+
package intest
18+
19+
// InTest checks if the code is running in test.
20+
var InTest = false

0 commit comments

Comments
 (0)