Skip to content

Commit

Permalink
Fix excess checksum bytes bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
notbdu committed Feb 28, 2019
1 parent 53cb7e6 commit 21bcf6e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 4 additions & 4 deletions digest/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ func Checksum(buf []byte) uint32 {

// Validate validates the data in the buffer against its checksum.
// The checksum is at the end of the buffer occupying `numChecksumBytes` bytes.
func Validate(b []byte) error {
func Validate(b []byte) ([]byte, error) {
if len(b) < numChecksumBytes {
return errChecksumMismatch
return nil, errChecksumMismatch
}
checksumStart := len(b) - numChecksumBytes
expectedChecksum := ToBuffer(b[checksumStart:]).ReadDigest()
if Checksum(b[:checksumStart]) != expectedChecksum {
return errChecksumMismatch
return nil, errChecksumMismatch
}
return nil
return b[:checksumStart], nil
}
5 changes: 3 additions & 2 deletions persist/fs/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,12 @@ func (r *reader) mmapReadAllAndValidateChecksum(fd *os.File) ([]byte, error) {
if res.Warning != nil {
r.logger.Warnf("warning during memory mapping info file %s: %s", fd.Name(), res.Warning)
}
if err := digest.Validate(res.Result); err != nil {
validatedBytes, err := digest.Validate(res.Result)
if err != nil {
mmap.Munmap(res.Result)
return nil, err
}
return res.Result, nil
return validatedBytes, nil
}

func (r *reader) readDocIDSet(data []byte) (index.DocIDSet, []byte, error) {
Expand Down

0 comments on commit 21bcf6e

Please sign in to comment.