Fixing and optimizing the loop detection as suggested in:

https://github.com/AppleCommander/AppleCommander/pull/19#pullrequestreview-82712102
This commit is contained in:
Lisias 2017-12-14 01:08:23 -02:00
parent 7b729b9d0b
commit 5c77c57aa2
3 changed files with 28 additions and 13 deletions

View File

@ -20,9 +20,9 @@
package com.webcodepro.applecommander.storage.os.dos33;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import com.webcodepro.applecommander.storage.DirectoryEntry;
import com.webcodepro.applecommander.storage.DiskException;
@ -147,13 +147,13 @@ public class DosFormatDisk extends FormattedDisk {
byte[] vtoc = readVtoc();
int track = AppleUtil.getUnsignedByte(vtoc[1]);
int sector = AppleUtil.getUnsignedByte(vtoc[2]);
final Map<Integer,Map<Integer,Boolean>> visits = new HashMap<>();
final Set<DosSectorAddress> visits = new HashSet<>();
while (sector != 0) { // bug fix: iterate through all catalog _sectors_
// Prevents a recursive catalog crawling.
if ( !visits.containsKey(track) ) visits.put(track, new HashMap<Integer,Boolean>());
if ( visits.get(track).containsKey(sector)) throw new DiskCorruptException("Recursive Directory structure detected.", DiskCorruptException.Kind.RECURSIVE_DIRECTORY_STRUCTURE, new DosSectorAddress(track, sector));
else visits.get(track).put(sector, Boolean.TRUE);
final DosSectorAddress address = new DosSectorAddress(track, sector);
if ( visits.contains(address)) throw new DiskCorruptException("Recursive Directory structure detected.", DiskCorruptException.Kind.RECURSIVE_DIRECTORY_STRUCTURE, address);
else visits.add(address);
byte[] catalogSector = readSector(track, sector);
int offset = 0x0b;

View File

@ -29,13 +29,28 @@ public class DosSectorAddress {
public final Integer track;
public final Integer sector;
public DosSectorAddress(int track, int sector) {
this.track = track;
this.sector = sector;
}
public String toString() {
return "Track:" + this.track + ", Sector:" + this.sector;
}
public boolean equals(final Object other){
if(other == null) return false;
if(!(other instanceof DosSectorAddress)) return false;
final DosSectorAddress o = (DosSectorAddress) other;
return this.track == o.track && this.sector == o.sector;
}
public int hashCode() {
int result = 17;
result = 31 * result + this.track.hashCode();
result = 31 * result + this.sector.hashCode();
return result;
}
}

View File

@ -23,10 +23,10 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.HashSet;
import com.webcodepro.applecommander.storage.DirectoryEntry;
import com.webcodepro.applecommander.storage.DiskException;
@ -275,11 +275,11 @@ public class ProdosFormatDisk extends FormattedDisk {
*/
protected List<FileEntry> getFiles(int blockNumber) throws DiskException {
List<FileEntry> files = new ArrayList<>();
final Map<Integer,Boolean> visits = new HashMap<>();
final Set<Integer> visits = new HashSet<>();
while (blockNumber != 0) {
// Prevents a recursive catalog crawling.
if ( visits.containsKey(blockNumber)) throw new DiskCorruptException("Recursive Directory structure detected.", DiskCorruptException.Kind.RECURSIVE_DIRECTORY_STRUCTURE, new ProdosBlockAddress(blockNumber));
else visits.put(blockNumber, Boolean.TRUE);
if ( visits.contains(blockNumber)) throw new DiskCorruptException("Recursive Directory structure detected.", DiskCorruptException.Kind.RECURSIVE_DIRECTORY_STRUCTURE, new ProdosBlockAddress(blockNumber));
else visits.add(blockNumber);
byte[] block = readBlock(blockNumber);
int offset = 4;