mirror of https://github.com/etcd-io/bbolt.git
Check for sequence overflow.
parent
0752480eb4
commit
a857b45bac
7
const.go
7
const.go
|
@ -2,6 +2,13 @@ package bolt
|
|||
|
||||
const version = 1
|
||||
|
||||
const (
|
||||
maxUint = ^uint(0)
|
||||
minUint = 0
|
||||
maxInt = int(^uint(0) >> 1)
|
||||
minInt = -maxInt - 1
|
||||
)
|
||||
|
||||
const (
|
||||
// MaxBucketNameSize is the maximum length of a bucket name, in bytes.
|
||||
MaxBucketNameSize = 255
|
||||
|
|
4
error.go
4
error.go
|
@ -38,6 +38,10 @@ var (
|
|||
|
||||
// ErrValueTooLarge is returned when inserting a value that is larger than MaxValueSize.
|
||||
ErrValueTooLarge = &Error{"value too large", nil}
|
||||
|
||||
// ErrSequenceOverflow is returned when the next sequence number will be
|
||||
// larger than the maximum integer size.
|
||||
ErrSequenceOverflow = &Error{"sequence overflow", nil}
|
||||
)
|
||||
|
||||
// Error represents an error condition caused by Bolt.
|
||||
|
|
|
@ -82,6 +82,12 @@ func (t *RWTransaction) NextSequence(name string) (int, error) {
|
|||
return 0, ErrBucketNotFound
|
||||
}
|
||||
|
||||
// Make sure next sequence number will not be larger than the maximum
|
||||
// integer size of the system.
|
||||
if b.bucket.sequence == uint64(maxInt) {
|
||||
return 0, ErrSequenceOverflow
|
||||
}
|
||||
|
||||
// Increment and return the sequence.
|
||||
b.bucket.sequence++
|
||||
|
||||
|
|
|
@ -136,6 +136,21 @@ func TestRWTransactionNextSequence(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
// Ensure that incrementing past the maximum sequence number will return an error.
|
||||
func TestRWTransactionNextSequenceOverflow(t *testing.T) {
|
||||
withOpenDB(func(db *DB, path string) {
|
||||
db.CreateBucket("widgets")
|
||||
db.Do(func(txn *RWTransaction) error {
|
||||
b := txn.Bucket("widgets")
|
||||
b.bucket.sequence = uint64(maxInt)
|
||||
seq, err := txn.NextSequence("widgets")
|
||||
assert.Equal(t, err, ErrSequenceOverflow)
|
||||
assert.Equal(t, seq, 0)
|
||||
return nil
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// Ensure that an error is returned when inserting into a bucket that doesn't exist.
|
||||
func TestRWTransactionPutBucketNotFound(t *testing.T) {
|
||||
withOpenDB(func(db *DB, path string) {
|
||||
|
|
Loading…
Reference in New Issue