Ignore invalid replay files instead of crashing.

This commit is contained in:
Paul Chote
2016-11-06 16:04:24 +00:00
parent 7ba0c22346
commit ab344b390d

View File

@@ -79,45 +79,27 @@ namespace OpenRA.FileFormats
public static ReplayMetadata Read(string path) public static ReplayMetadata Read(string path)
{ {
using (var fs = new FileStream(path, FileMode.Open))
return Read(fs, path);
}
static ReplayMetadata Read(FileStream fs, string path)
{
if (!fs.CanSeek)
return null;
if (fs.Length < 20)
return null;
try try
{ {
fs.Seek(-(4 + 4), SeekOrigin.End); using (var fs = new FileStream(path, FileMode.Open))
var dataLength = fs.ReadInt32();
if (fs.ReadInt32() == MetaEndMarker)
{ {
// go back by (end marker + length storage + data + version + start marker) bytes if (!fs.CanSeek)
fs.Seek(-(4 + 4 + dataLength + 4 + 4), SeekOrigin.Current); return null;
try
if (fs.Length < 20)
return null;
fs.Seek(-(4 + 4), SeekOrigin.End);
var dataLength = fs.ReadInt32();
if (fs.ReadInt32() == MetaEndMarker)
{ {
// Go back by (end marker + length storage + data + version + start marker) bytes
fs.Seek(-(4 + 4 + dataLength + 4 + 4), SeekOrigin.Current);
return new ReplayMetadata(fs, path); return new ReplayMetadata(fs, path);
} }
catch (YamlException ex)
{
Log.Write("debug", ex.ToString());
}
catch (InvalidOperationException ex)
{
Log.Write("debug", ex.ToString());
}
catch (NotSupportedException ex)
{
Log.Write("debug", ex.ToString());
}
} }
} }
catch (IOException ex) catch (Exception ex)
{ {
Log.Write("debug", ex.ToString()); Log.Write("debug", ex.ToString());
} }