unosquare / ffmediaelement

FFME: The Advanced WPF MediaElement (based on FFmpeg)
https://unosquare.github.io/ffmediaelement/
Other
1.18k stars 244 forks source link

Multiple threats of 'NullReferenceException' [bug] #407

Closed Neocriss closed 4 years ago

Neocriss commented 5 years ago

Multiple threats of 'NullReferenceException'

Please, install a trial version of 'Resharper' then review the code. Go "Extensions -> ReSharper -> Inspect -> Code Issues in Solution". There is a multiple dangerous points causing 'NullReferenceException', for example. See attachments.

Issue Categories

Version Information

Samples of Issues

await null in CaptureBitmapAsync()

A null propagation always returns a value and await operator always expect the value. If the null propagation returns null then will be done something like "((Task)null).GetAwaiter().GetResult()". https://github.com/unosquare/ffmediaelement/blob/master/Unosquare.FFME.Windows/MediaElement.cs

await VideoView?.InvokeAsync(() =>
{
});

'default' keyword in non-generic type 'Subtitles'

The 'GetFontSize' method returns a value-type 'double', but 'default' keyword returns 'object' that is reference type. The null-coalescing operand returns default reference-type value, which is 'null'. You cannot cast 'null' to 'double'. Use 'default(double)' instead. https://github.com/unosquare/ffmediaelement/blob/master/Unosquare.FFME.Windows/Subtitles.cs

public static double GetFontSize(MediaElement element) => (double)(element?.GetValue(FontSizeProperty) ?? default);

There is the same problem in 'GetFontWeight' and 'GetOutlineWidth' methods.

Other recommendations

https://github.com/unosquare/ffmediaelement/blob/master/Unosquare.FFME.Windows.Sample/MainWindow.MediaEvents.cs

private void OnMediaOpening(object sender, MediaOpeningEventArgs e)
{
    // use 'as' only if you not sure
    var media = sender as MediaElement;   // <- bad
    // cast directly to emphasize your intention
    var media = (MediaElement)sender;   // <- good
}

Attachments

https://www.dropbox.com/s/6a7y587x6o1zxob/Unosquare_code_issues.zip?dl=0

mariodivece commented 4 years ago

Thanks for reporting. These issues have been addressed. on the casting matter I understand. The use of as is much less expensive than direct casting and that's why I prefer it. The other items I have changed.