Skip to content

Commit

Permalink
Prevent crashing in HasFileNames(), TryGetData() and TryGetValue() ex…
Browse files Browse the repository at this point in the history
…tension methods of IDataObject.

(cherry picked from commit af30158)
  • Loading branch information
hamster620 committed Mar 24, 2024
1 parent 4464ce1 commit c334745
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions Avalonia/Input/DataObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,22 @@ public static class DataObjectExtensions
/// <returns>True if at least one file name is contained in <see cref="IDataObject"/>.</returns>
public static bool HasFileNames(this IDataObject data) => Global.RunOrDefault(() =>
{
return data.GetFiles()?.Let(it =>
try
{
foreach (var item in it)
return data.GetFiles()?.Let(it =>
{
if (!string.IsNullOrEmpty(item.TryGetLocalPath()))
return true;
}
foreach (var item in it)
{
if (!string.IsNullOrEmpty(item.TryGetLocalPath()))
return true;
}
return false;
}) ?? false;
}
catch
{
return false;
}) ?? false;
}
});


Expand All @@ -53,11 +60,18 @@ public static bool HasFileNames(this IDataObject data) => Global.RunOrDefault(()
/// <returns>True if data got successfully.</returns>
public static bool TryGetData<T>(this IDataObject dataObject, string format, out T? data) where T : class
{
var rawData = dataObject.Get(format);
if (rawData is T dataT)
try
{
data = dataT;
return true;
var rawData = dataObject.Get(format);
if (rawData is T dataT)
{
data = dataT;
return true;
}
}
catch
{
// ignored
}
data = default;
return false;
Expand Down Expand Up @@ -105,11 +119,18 @@ public static bool TryGetSingleFileName(this IDataObject data, out string? fileN
/// <returns>True if value got successfully.</returns>
public static bool TryGetValue<T>(this IDataObject dataObject, string format, out T value) where T : struct
{
var rawData = dataObject.Get(format);
if (rawData is T targetValue)
try
{
var rawData = dataObject.Get(format);
if (rawData is T targetValue)
{
value = targetValue;
return true;
}
}
catch
{
value = targetValue;
return true;
// ignored
}
value = default;
return false;
Expand Down

0 comments on commit c334745

Please sign in to comment.