Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix issue 1-10
  • Loading branch information
RidRisR committed Oct 22, 2025
commit d6b948eae65d076156c61471ce01c4fde91020e1
17 changes: 13 additions & 4 deletions br/pkg/storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,10 +953,19 @@ func (rs *S3Storage) open(
return nil, RangeInfo{}, errors.Annotatef(berrors.ErrStorageUnknown, "open file '%s' failed. The S3 object has no content length", path)
}
objectSize := *(result.ContentLength)
r = RangeInfo{
Start: 0,
End: objectSize - 1,
Size: objectSize,
// Handle empty objects (size=0) to avoid End=-1
if objectSize == 0 {
r = RangeInfo{
Start: 0,
End: 0,
Size: 0,
}
} else {
r = RangeInfo{
Start: 0,
End: objectSize - 1,
Size: objectSize,
}
}
} else {
r, err = ParseRangeInfo(result.ContentRange)
Expand Down
10 changes: 10 additions & 0 deletions br/pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,17 @@ func ReadDataInRange(
start int64,
p []byte,
) (n int, err error) {
// Sanity check: reject obviously invalid offsets
if start < 0 {
return 0, errors.Annotatef(berrors.ErrInvalidArgument,
"invalid negative start offset: %d", start)
}
end := start + int64(len(p))
// Detect overflow: if end wrapped around to negative, overflow occurred
if end < start {
return 0, errors.Annotatef(berrors.ErrInvalidArgument,
"range calculation overflow: start=%d, len=%d", start, len(p))
}
rd, err := storage.Open(ctx, name, &ReaderOption{
StartOffset: &start,
EndOffset: &end,
Expand Down
14 changes: 13 additions & 1 deletion br/pkg/stream/decode_kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package stream

import (
"encoding/binary"
"math"

"github.com/pingcap/errors"
berrors "github.com/pingcap/tidb/br/pkg/errors"
Expand Down Expand Up @@ -63,7 +64,18 @@ func (ei *EventIterator) Next() {

// Valid checks whether the iterator is valid.
func (ei *EventIterator) Valid() bool {
return ei.err == nil && ei.pos < uint32(len(ei.buff))
if ei.err != nil {
return false
}
buffLen := len(ei.buff)
// Check if buffer length exceeds uint32 range
// This prevents truncation when comparing with ei.pos (uint32)
if buffLen > math.MaxUint32 {
ei.err = errors.Annotatef(berrors.ErrInvalidArgument,
"buffer too large: %d bytes exceeds uint32 limit (%d bytes)", buffLen, math.MaxUint32)
return false
}
return ei.pos < uint32(buffLen)
}

// Key gets the key in kv-event if valid() == true
Expand Down
9 changes: 8 additions & 1 deletion br/pkg/stream/stream_metas.go
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,14 @@ func (m MigrationExt) applyMetaEditTo(ctx context.Context, medit *pb.MetaEdit, m
medit.DeleteLogicalFiles[idx].Spans,
dfi.RangeOffset,
func(s *pb.Span, u uint64) int {
return int(s.Offset - u)
// Use comparison instead of subtraction to avoid uint64 underflow
// and int overflow issues
if s.Offset < u {
return -1
} else if s.Offset > u {
return 1
}
return 0
})
if ok && medit.DeleteLogicalFiles[idx].Spans[received].Length != dfi.RangeLength {
err = errors.Annotatef(
Expand Down
8 changes: 8 additions & 0 deletions br/pkg/task/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"crypto/tls"
"encoding/hex"
"fmt"
"math"
"net/url"
"os"
"path"
Expand Down Expand Up @@ -631,6 +632,13 @@ func (cfg *Config) ParseFromFlags(flags *pflag.FlagSet) error {
if rateLimitUnit, err = flags.GetUint64(flagRateLimitUnit); err != nil {
return errors.Trace(err)
}
// Check for multiplication overflow when both values are non-zero
// This prevents silent wraparound that would cause incorrect rate limiting
if rateLimit > 0 && rateLimitUnit > 0 && rateLimit > math.MaxUint64/rateLimitUnit {
return errors.Annotatef(berrors.ErrInvalidArgument,
"rate limit calculation overflow: %d * %d exceeds uint64 max (consider max ~17PB/s)",
rateLimit, rateLimitUnit)
}
cfg.RateLimit = rateLimit * rateLimitUnit

cfg.Schemas = make(map[string]struct{})
Expand Down
Loading