From 38b9ce3aa28c1d62e293f5b5cec9f0612fdffe7d Mon Sep 17 00:00:00 2001 From: Seraphima Zykova Date: Tue, 30 Mar 2021 19:42:10 +0100 Subject: [PATCH] [FZ Editor] Crash with malformed layout data fix. (#10500) * check values on json parsing * comments * invalidate malformed canvas layouts * changed error message * simplify error message * removed exception message --- .../editor/FancyZonesEditor/App.xaml.cs | 10 +- .../Properties/Resources.Designer.cs | 15 +-- .../Properties/Resources.resx | 7 +- .../Utils/FancyZonesEditorIO.cs | 119 +++++++++++------- 4 files changed, 83 insertions(+), 68 deletions(-) diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs b/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs index c259daa011..8d505528af 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs @@ -135,15 +135,7 @@ namespace FancyZonesEditor sb.AppendLine(ParsingErrorDataTag); sb.AppendLine(parseResult.MalformedData); - string message = parseResult.Message + Environment.NewLine + Environment.NewLine + FancyZonesEditor.Properties.Resources.Error_Parsing_Zones_Settings_User_Choice; - if (MessageBox.Show(message, FancyZonesEditor.Properties.Resources.Error_Parsing_Zones_Settings_Title, MessageBoxButton.YesNo) == MessageBoxResult.No) - { - // TODO: log error - ShowExceptionReportMessageBox(sb.ToString()); - Environment.Exit(0); - } - - ShowExceptionReportMessageBox(sb.ToString()); + MessageBox.Show(parseResult.Message, FancyZonesEditor.Properties.Resources.Error_Parsing_Zones_Settings_Title, MessageBoxButton.OK); } MainWindowSettingsModel settings = ((App)Current).MainWindowSettings; diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Properties/Resources.Designer.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Properties/Resources.Designer.cs index 65f423c184..b0ed1cb6a0 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Properties/Resources.Designer.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Properties/Resources.Designer.cs @@ -376,11 +376,11 @@ namespace FancyZonesEditor.Properties { } /// - /// Looks up a localized string similar to 'zones-settings.json' contains malformed data.. + /// Looks up a localized string similar to A layout that contained invalid data has been removed.. /// - public static string Error_Parsing_Zones_Settings_Malformed_Data { + public static string Error_Parsing_Zones_Settings_Message { get { - return ResourceManager.GetString("Error_Parsing_Zones_Settings_Malformed_Data", resourceCulture); + return ResourceManager.GetString("Error_Parsing_Zones_Settings_Message", resourceCulture); } } @@ -393,15 +393,6 @@ namespace FancyZonesEditor.Properties { } } - /// - /// Looks up a localized string similar to Would you like to continue? Malformed data will be lost.. - /// - public static string Error_Parsing_Zones_Settings_User_Choice { - get { - return ResourceManager.GetString("Error_Parsing_Zones_Settings_User_Choice", resourceCulture); - } - } - /// /// Looks up a localized string similar to Error persisting custom layout. /// diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Properties/Resources.resx b/src/modules/fancyzones/editor/FancyZonesEditor/Properties/Resources.resx index 122af40493..58809d5097 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Properties/Resources.resx +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Properties/Resources.resx @@ -303,14 +303,11 @@ Delete zone A tooltip on a button that allows the user to delete a zone - - 'zones-settings.json' contains malformed data. - Editor settings parsing error. - - Would you like to continue? Malformed data will be lost. + + A layout that contained invalid data has been removed. Apply layout diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Utils/FancyZonesEditorIO.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Utils/FancyZonesEditorIO.cs index 439577e0f1..4a61063e03 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Utils/FancyZonesEditorIO.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Utils/FancyZonesEditorIO.cs @@ -549,7 +549,7 @@ namespace FancyZonesEditor.Utils if (!devicesParsingResult || !customZonesParsingResult) { - return new ParsingResult(false, Properties.Resources.Error_Parsing_Zones_Settings_Malformed_Data, settingsString); + return new ParsingResult(false, FancyZonesEditor.Properties.Resources.Error_Parsing_Zones_Settings_Message, settingsString); } } catch (Exception ex) @@ -810,55 +810,25 @@ namespace FancyZonesEditor.Utils continue; } - LayoutModel layout; - if (zoneSet.Type == CanvasLayoutModel.ModelTypeID) + LayoutModel layout = null; + try { - var info = JsonSerializer.Deserialize(zoneSet.Info.GetRawText(), _options); - - var zones = new List(); - foreach (var zone in info.Zones) + if (zoneSet.Type == CanvasLayoutModel.ModelTypeID) { - zones.Add(new Int32Rect { X = (int)zone.X, Y = (int)zone.Y, Width = (int)zone.Width, Height = (int)zone.Height }); + layout = ParseCanvasInfo(zoneSet); } - - try + else if (zoneSet.Type == GridLayoutModel.ModelTypeID) { - layout = new CanvasLayoutModel(zoneSet.Uuid, zoneSet.Name, LayoutType.Custom, zones, info.RefWidth, info.RefHeight); + layout = ParseGridInfo(zoneSet); } - catch (Exception) - { - continue; - } - - layout.SensitivityRadius = info.SensitivityRadius; } - else if (zoneSet.Type == GridLayoutModel.ModelTypeID) + catch (Exception) { - var info = JsonSerializer.Deserialize(zoneSet.Info.GetRawText(), _options); - - var cells = new int[info.Rows, info.Columns]; - for (int row = 0; row < info.Rows; row++) - { - for (int column = 0; column < info.Columns; column++) - { - cells[row, column] = info.CellChildMap[row][column]; - } - } - - try - { - layout = new GridLayoutModel(zoneSet.Uuid, zoneSet.Name, LayoutType.Custom, info.Rows, info.Columns, info.RowsPercentage, info.ColumnsPercentage, cells); - } - catch (Exception) - { - continue; - } - - layout.SensitivityRadius = info.SensitivityRadius; - (layout as GridLayoutModel).ShowSpacing = info.ShowSpacing; - (layout as GridLayoutModel).Spacing = info.Spacing; + result = false; + continue; } - else + + if (layout == null) { result = false; continue; @@ -913,6 +883,71 @@ namespace FancyZonesEditor.Utils return true; } + private CanvasLayoutModel ParseCanvasInfo(CustomLayoutWrapper wrapper) + { + var info = JsonSerializer.Deserialize(wrapper.Info.GetRawText(), _options); + + var zones = new List(); + foreach (var zone in info.Zones) + { + if (zone.Width < 0 || zone.Height < 0) + { + // Malformed data + return null; + } + + zones.Add(new Int32Rect { X = zone.X, Y = zone.Y, Width = zone.Width, Height = zone.Height }); + } + + var layout = new CanvasLayoutModel(wrapper.Uuid, wrapper.Name, LayoutType.Custom, zones, Math.Max(info.RefWidth, 0), Math.Max(info.RefHeight, 0)); + layout.SensitivityRadius = info.SensitivityRadius; + + return layout; + } + + private GridLayoutModel ParseGridInfo(CustomLayoutWrapper wrapper) + { + var info = JsonSerializer.Deserialize(wrapper.Info.GetRawText(), _options); + + // Check if rows and columns are valid + if (info.Rows <= 0 || info.Columns <= 0) + { + return null; + } + + // Check if percentage is valid. Otherwise, Editor could crash on layout rendering. + foreach (int percent in info.RowsPercentage) + { + if (percent < 0) + { + return null; + } + } + + foreach (int percent in info.ColumnsPercentage) + { + if (percent < 0) + { + return null; + } + } + + var cells = new int[info.Rows, info.Columns]; + for (int row = 0; row < info.Rows; row++) + { + for (int column = 0; column < info.Columns; column++) + { + cells[row, column] = info.CellChildMap[row][column]; + } + } + + var layout = new GridLayoutModel(wrapper.Uuid, wrapper.Name, LayoutType.Custom, info.Rows, info.Columns, info.RowsPercentage, info.ColumnsPercentage, cells); + layout.SensitivityRadius = info.SensitivityRadius; + layout.ShowSpacing = info.ShowSpacing; + layout.Spacing = info.Spacing; + return layout; + } + private LayoutType JsonTagToLayoutType(string tag) { switch (tag)