Skip to content
This repository has been archived by the owner on Mar 9, 2019. It is now read-only.

Commit

Permalink
Merge pull request #40 from benbjohnson/sequence-overflow
Browse files Browse the repository at this point in the history
Check for sequence overflow.
  • Loading branch information
benbjohnson committed Feb 20, 2014
2 parents 0752480 + a857b45 commit 44579ac
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions rwtransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++

Expand Down
15 changes: 15 additions & 0 deletions rwtransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 44579ac

Please sign in to comment.