fix #25: possible NullReferenceException with Document.LargestThumbSize

fix undisposed FileStream on broken session file.
This commit is contained in:
Wizou 2022-01-26 11:08:33 +01:00
parent 582027e800
commit 12850182ff
2 changed files with 9 additions and 8 deletions

View file

@ -47,13 +47,13 @@ namespace WTelegram
{ {
var header = new byte[8]; var header = new byte[8];
var fileStream = new FileStream(pathname, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 1); // no buffering var fileStream = new FileStream(pathname, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 1); // no buffering
if (fileStream.Read(header, 0, 8) == 8) try
{ {
try if (fileStream.Read(header, 0, 8) == 8)
{ {
var position = BinaryPrimitives.ReadInt32LittleEndian(header); var position = BinaryPrimitives.ReadInt32LittleEndian(header);
var length = BinaryPrimitives.ReadInt32LittleEndian(header.AsSpan(4)); var length = BinaryPrimitives.ReadInt32LittleEndian(header.AsSpan(4));
if (position < 0 || length < 0 || position >= 65536 || length >= 32768){ position = 0; length = (int)fileStream.Length; } if (position < 0 || length < 0 || position >= 65536 || length >= 32768) { position = 0; length = (int)fileStream.Length; }
var input = new byte[length]; var input = new byte[length];
fileStream.Position = position; fileStream.Position = position;
if (fileStream.Read(input, 0, length) != length) if (fileStream.Read(input, 0, length) != length)
@ -65,10 +65,11 @@ namespace WTelegram
Helpers.Log(2, "Loaded previous session"); Helpers.Log(2, "Loaded previous session");
return session; return session;
} }
catch (Exception ex) }
{ catch (Exception ex)
throw new ApplicationException($"Exception while reading session file: {ex.Message}\nDelete the file to start a new session", ex); {
} fileStream.Dispose();
throw new ApplicationException($"Exception while reading session file: {ex.Message}\nDelete the file to start a new session", ex);
} }
return new Session { _fileStream = fileStream, _nextPosition = 8, _rgbKey = rgbKey }; return new Session { _fileStream = fileStream, _nextPosition = 8, _rgbKey = rgbKey };
} }

View file

@ -357,7 +357,7 @@ namespace TL
public override long ID => id; public override long ID => id;
protected override InputDocument ToInputDocument() => new() { id = id, access_hash = access_hash, file_reference = file_reference }; protected override InputDocument ToInputDocument() => new() { id = id, access_hash = access_hash, file_reference = file_reference };
public InputDocumentFileLocation ToFileLocation(PhotoSizeBase thumbSize = null) => new() { id = id, access_hash = access_hash, file_reference = file_reference, thumb_size = thumbSize?.Type }; public InputDocumentFileLocation ToFileLocation(PhotoSizeBase thumbSize = null) => new() { id = id, access_hash = access_hash, file_reference = file_reference, thumb_size = thumbSize?.Type };
public PhotoSizeBase LargestThumbSize => thumbs.Aggregate((agg, next) => (long)next.Width * next.Height > (long)agg.Width * agg.Height ? next : agg); public PhotoSizeBase LargestThumbSize => thumbs?.Aggregate((agg, next) => (long)next.Width * next.Height > (long)agg.Width * agg.Height ? next : agg);
} }
partial class SendMessageAction partial class SendMessageAction