Commit d8ae78a4 authored by Nathan VanBenschoten's avatar Nathan VanBenschoten

config: properly handle decoding errors in GetLargestObjectID

This was wrong before. Adds testing.
parent a102b141
......@@ -222,11 +222,11 @@ func (s *SystemConfig) GetLargestObjectID(maxID uint32, pseudoIDs []uint32) (uin
searchSlice := s.Values[lowIndex:highIndex]
var err error
maxIdx := sort.Search(len(searchSlice), func(i int) bool {
var id uint32
id, err = keys.TODOSQLCodec.DecodeDescMetadataID(searchSlice[i].Key)
if err != nil {
return false
}
var id uint32
id, err = keys.TODOSQLCodec.DecodeDescMetadataID(searchSlice[i].Key)
return uint32(id) >= maxID
})
if err != nil {
......
......@@ -11,6 +11,7 @@
package config_test
import (
"math"
"sort"
"testing"
......@@ -147,6 +148,19 @@ func TestGetLargestID(t *testing.T) {
sqlKV(keys.UsersTableID, 1, 3),
}, 0, 0, nil, "descriptor table not found"},
// Decoding error, unbounded max.
{[]roachpb.KeyValue{
sqlKV(keys.DescriptorTableID, 1, 1),
sqlKV(keys.DescriptorTableID, 1, math.MaxUint64),
}, 0, 0, nil, "descriptor ID 18446744073709551615 exceeds uint32 bounds"},
// Decoding error, bounded max.
{[]roachpb.KeyValue{
sqlKV(keys.DescriptorTableID, 1, 1),
sqlKV(keys.DescriptorTableID, 1, math.MaxUint64),
sqlKV(keys.DescriptorTableID, 2, 1),
}, 0, 5, nil, "descriptor ID 18446744073709551615 exceeds uint32 bounds"},
// Single correct descriptor entry.
{[]roachpb.KeyValue{sqlKV(keys.DescriptorTableID, 1, 1)}, 1, 0, nil, ""},
......
......@@ -12,6 +12,7 @@ package keys
import (
"bytes"
"math"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
......@@ -231,5 +232,8 @@ func (d sqlDecoder) DecodeDescMetadataID(key roachpb.Key) (uint32, error) {
if err != nil {
return 0, err
}
if id > math.MaxUint32 {
return 0, errors.Errorf("descriptor ID %d exceeds uint32 bounds", id)
}
return uint32(id), nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment