Skip to content

Fix: Fixed crash when manipulating invalid images #14258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Files.App/Helpers/Application/AppLifecycleHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public static void HandleAppUnhandledException(Exception? ex, bool showToastNoti
{
await Launcher.LaunchUriAsync(new Uri("files-uwp:"));
})
.Wait(1000);
.Wait(100);
}
Process.GetCurrentProcess().Kill();
}
Expand Down
73 changes: 46 additions & 27 deletions src/Files.App/Helpers/BitmapHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media.Imaging;
using System.IO;
using Windows.Foundation.Metadata;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
Expand Down Expand Up @@ -47,43 +49,60 @@ internal static class BitmapHelper
/// </remarks>
public static async Task RotateAsync(string filePath, BitmapRotation rotation)
{
if (string.IsNullOrEmpty(filePath))
try
{
return;
}
if (string.IsNullOrEmpty(filePath))
{
return;
}

var file = await StorageHelpers.ToStorageItem<IStorageFile>(filePath);
if (file is null)
{
return;
}
var file = await StorageHelpers.ToStorageItem<IStorageFile>(filePath);
if (file is null)
{
return;
}

var fileStreamRes = await FilesystemTasks.Wrap(() => file.OpenAsync(FileAccessMode.ReadWrite).AsTask());
using IRandomAccessStream fileStream = fileStreamRes.Result;
if (fileStream is null)
{
return;
}
var fileStreamRes = await FilesystemTasks.Wrap(() => file.OpenAsync(FileAccessMode.ReadWrite).AsTask());
using IRandomAccessStream fileStream = fileStreamRes.Result;
if (fileStream is null)
{
return;
}

BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
using var memStream = new InMemoryRandomAccessStream();
BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
using var memStream = new InMemoryRandomAccessStream();
BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder);

for (int i = 0; i < decoder.FrameCount - 1; i++)
{
encoder.BitmapTransform.Rotation = rotation;
await encoder.GoToNextFrameAsync();
}

for (int i = 0; i < decoder.FrameCount - 1; i++)
{
encoder.BitmapTransform.Rotation = rotation;
await encoder.GoToNextFrameAsync();
}

encoder.BitmapTransform.Rotation = rotation;
await encoder.FlushAsync();

await encoder.FlushAsync();
memStream.Seek(0);
fileStream.Seek(0);
fileStream.Size = 0;

memStream.Seek(0);
fileStream.Seek(0);
fileStream.Size = 0;
await RandomAccessStream.CopyAsync(memStream, fileStream);
}
catch (Exception ex)
{
var errorDialog = new ContentDialog()
{
Title = "FailedToRotateImage".GetLocalizedResource(),
Content = ex.Message,
PrimaryButtonText = "OK".GetLocalizedResource(),
};

await RandomAccessStream.CopyAsync(memStream, fileStream);
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
errorDialog.XamlRoot = MainWindow.Instance.Content.XamlRoot;

await errorDialog.TryShowAsync();
}
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -3659,4 +3659,7 @@
<data name="SortPriority" xml:space="preserve">
<value>Sort priority</value>
</data>
<data name="FailedToRotateImage" xml:space="preserve">
<value>Failed to rotate the image</value>
</data>
</root>
17 changes: 8 additions & 9 deletions src/Files.App/Utils/Global/WallpaperHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,25 @@ public static class WallpaperHelpers
{
public static async Task SetAsBackgroundAsync(WallpaperType type, string filePath)
{

if (type == WallpaperType.Desktop)
try
{
try
if (type == WallpaperType.Desktop)
{
// Set the desktop background
var wallpaper = (Shell32.IDesktopWallpaper)new Shell32.DesktopWallpaper();
wallpaper.GetMonitorDevicePathAt(0, out var monitorId);
wallpaper.SetWallpaper(monitorId, filePath);
}
catch (Exception ex)
else if (type == WallpaperType.LockScreen)
{
ShowErrorPrompt(ex.Message);
// Set the lockscreen background
IStorageFile sourceFile = await StorageFile.GetFileFromPathAsync(filePath);
await LockScreen.SetImageFileAsync(sourceFile);
}
}
else if (type == WallpaperType.LockScreen)
catch (Exception ex)
{
// Set the lockscreen background
IStorageFile sourceFile = await StorageFile.GetFileFromPathAsync(filePath);
await LockScreen.SetImageFileAsync(sourceFile);
ShowErrorPrompt(ex.Message);
}
}

Expand Down