From e19fa67f7571388d3dc956f7c6b4547bfb635072 Mon Sep 17 00:00:00 2001 From: Richard Lehane Date: Mon, 25 Jan 2016 16:02:13 +1100 Subject: [PATCH] detect cycles that are longer than one node - 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. --- file.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/file.go b/file.go index 4dfcda8..f2d8de8 100644 --- a/file.go +++ b/file.go @@ -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 { @@ -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