Catch more uninitialized file call stack paths

Closes #4223
This commit is contained in:
Kurt 2024-03-18 19:05:22 -05:00
parent 8a3a338c0b
commit dca76f50f7
4 changed files with 23 additions and 8 deletions

View file

@ -183,7 +183,7 @@ public static class FileUtil
/// <returns>True if file object reference is valid, false if none found.</returns> /// <returns>True if file object reference is valid, false if none found.</returns>
public static bool TryGetMemoryCard(byte[] data, [NotNullWhen(true)] out SAV3GCMemoryCard? memcard) public static bool TryGetMemoryCard(byte[] data, [NotNullWhen(true)] out SAV3GCMemoryCard? memcard)
{ {
if (!SAV3GCMemoryCard.IsMemoryCardSize(data)) if (!SAV3GCMemoryCard.IsMemoryCardSize(data) || IsNoDataPresent(data))
{ {
memcard = null; memcard = null;
return false; return false;
@ -233,7 +233,7 @@ public static class FileUtil
/// <returns>True if file object reference is valid, false if none found.</returns> /// <returns>True if file object reference is valid, false if none found.</returns>
public static bool TryGetPCBoxBin(byte[] data, out IEnumerable<byte[]> pkms, SaveFile? sav) public static bool TryGetPCBoxBin(byte[] data, out IEnumerable<byte[]> pkms, SaveFile? sav)
{ {
if (sav == null) if (sav == null || IsNoDataPresent(data))
{ {
pkms = []; pkms = [];
return false; return false;
@ -248,6 +248,15 @@ public static class FileUtil
return false; return false;
} }
private static bool IsNoDataPresent(ReadOnlySpan<byte> data)
{
if (!data.ContainsAnyExcept<byte>(0xFF))
return true;
if (!data.ContainsAnyExcept<byte>(0x00))
return true;
return false;
}
/// <summary> /// <summary>
/// Tries to get a <see cref="BattleVideo"/> object from the input parameters. /// Tries to get a <see cref="BattleVideo"/> object from the input parameters.
/// </summary> /// </summary>

View file

@ -736,7 +736,7 @@ public partial class Main : Form
case GCMemoryCardState.SaveGameRSBOX: memCard.SelectSaveGame(GameVersion.RSBOX); break; case GCMemoryCardState.SaveGameRSBOX: memCard.SelectSaveGame(GameVersion.RSBOX); break;
default: default:
WinFormsUtil.Error(!SaveUtil.IsSizeValid(memCard.Data.Length) ? MsgFileGameCubeBad : MsgFileLoadSaveLoadFail, path); WinFormsUtil.Error(!SaveUtil.IsSizeValid(memCard.Data.Length) ? MsgFileGameCubeBad : GetHintInvalidFile(memCard.Data, path), path);
return false; return false;
} }
return true; return true;

View file

@ -37,10 +37,7 @@ public partial class SettingsEditor : Form
if (Last is not null && tabControl1.Controls[Last] is TabPage tab) if (Last is not null && tabControl1.Controls[Last] is TabPage tab)
tabControl1.SelectedTab = tab; tabControl1.SelectedTab = tab;
tabControl1.SelectedIndexChanged += (_, _) => tabControl1.SelectedIndexChanged += (_, _) => Last = tabControl1.SelectedTab?.Name;
{
Last = tabControl1.SelectedTab?.Name;
};
this.CenterToForm(FindForm()); this.CenterToForm(FindForm());
} }

View file

@ -241,7 +241,16 @@ public static class WinFormsUtil
"SaveRAM", // BizHawk "SaveRAM", // BizHawk
]; ];
public static bool IsFileExtensionSAV(string file) => CustomSaveExtensions.Contains(Path.GetExtension(file)); public static bool IsFileExtensionSAV(ReadOnlySpan<char> file)
{
var ext = Path.GetExtension(file);
foreach (var other in CustomSaveExtensions)
{
if (ext.EndsWith(other))
return true;
}
return false;
}
private static string ExtraSaveExtensions => ";" + string.Join(";", CustomSaveExtensions.Select(z => $"*.{z}")); private static string ExtraSaveExtensions => ";" + string.Join(";", CustomSaveExtensions.Select(z => $"*.{z}"));