Improved robustness of PesFile.OpenFile()

Branch_2.1.x
Nathan Crawford 2016-02-23 23:41:24 -05:00
rodzic 5cf44ec832
commit d0c2eb485a
1 zmienionych plików z 167 dodań i 177 usunięć

Wyświetl plik

@ -111,10 +111,6 @@ namespace PesFile
private void OpenFile(string filename) private void OpenFile(string filename)
{ {
#if !DEBUG
try
{
#endif
_filename = filename; _filename = filename;
fileIn = new System.IO.BinaryReader(System.IO.File.Open(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read)); fileIn = new System.IO.BinaryReader(System.IO.File.Open(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read));
@ -138,9 +134,25 @@ namespace PesFile
{ {
versionString += fileIn.ReadChar(); versionString += fileIn.ReadChar();
} }
pesVersion = Convert.ToUInt16(versionString); if(!UInt16.TryParse(versionString, out pesVersion))
{
// This is not a file that we can read
readyStatus = statusEnum.ParseError;
lastError = "PES version is not the correct format";
fileIn.Close();
return;
}
int pecstart = fileIn.ReadInt32(); int pecstart = fileIn.ReadInt32();
// Sanity check on PEC start position
if(fileIn.BaseStream.Length < (pecstart + 532))
{
// This file is probably truncated
readyStatus = statusEnum.ParseError;
lastError = "File appears to be truncated (PEC section is beyond end of file)";
fileIn.Close();
return;
}
// Read number of colors in this design // Read number of colors in this design
fileIn.BaseStream.Position = pecstart + 48; fileIn.BaseStream.Position = pecstart + 48;
@ -284,28 +296,6 @@ namespace PesFile
// Close the file // Close the file
fileIn.Close(); fileIn.Close();
#if !DEBUG
}
catch (System.IO.IOException ioex)
{
readyStatus = statusEnum.IOError;
lastError = ioex.Message;
if (fileIn != null)
{
fileIn.Close();
}
}
catch (Exception ex)
{
readyStatus = statusEnum.ParseError;
lastError = ex.Message;
if (fileIn != null)
{
fileIn.Close();
}
}
#endif
} }
public int GetWidth() public int GetWidth()