Skip to content

Commit

Permalink
detect cycles that are longer than one node
Browse files Browse the repository at this point in the history
- previous cycle detection just did nsn == sn detection (same node cycle)
- this change adds a map to store sector numbers to detect longer cycles
- avoid much memory use by only storing sector numbers when we go backwards (less than). This means that a cycle isn't detected instantly, but on the second loop. This should be fine as in most cases cycles don't occur.
  • Loading branch information
richardlehane committed Jan 25, 2016
1 parent ed31f6d commit e19fa67
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (r *Reader) setDirEntries() error {
c = int(r.header.numDirectorySectors)
}
fs := make([]*File, 0, c)
cycles := make(map[uint32]bool)
num := int(sectorSize / 128)
sn := r.header.directorySectorLoc
for sn != endOfChain {
Expand All @@ -99,14 +100,17 @@ func (r *Reader) setDirEntries() error {
fs = append(fs, f)
}
}
if nsn, err := r.findNext(sn, false); err != nil || nsn == sn {
if err != nil {
return Error{ErrRead, "directory entries error finding sector (" + err.Error() + ")", int64(nsn)}
nsn, err := r.findNext(sn, false)
if err != nil {
return Error{ErrRead, "directory entries error finding sector (" + err.Error() + ")", int64(nsn)}
}
if nsn <= sn {
if nsn == sn || cycles[nsn] {
return Error{ErrRead, "directory entries sector cycle", int64(nsn)}
}
return Error{ErrRead, "directory entries sector cycle", int64(nsn)}
} else {
sn = nsn
cycles[nsn] = true
}
sn = nsn
}
r.File = fs
return nil
Expand Down

0 comments on commit e19fa67

Please sign in to comment.