mirror of
https://github.com/aaru-dps/Aaru.git
synced 2025-12-16 19:24:25 +00:00
[Aaru.Core] Improve null safety
Several files in the Aaru.Core project have been updated to improve null safety. The modification of these files specifically handled null occurrences. Nullable value types are now correctly handled and default values are set to be used where nulls were previously unhandled. This will help prevent null reference exceptions and improve the overall stability of the code.
This commit is contained in:
@@ -96,7 +96,8 @@ public partial class Dump
|
||||
|
||||
if(ataIdNullable != null)
|
||||
{
|
||||
Identify.IdentifyDevice ataId = ataIdNullable.Value;
|
||||
// Guaranteed to never fall into default
|
||||
Identify.IdentifyDevice ataId = ataIdNullable ?? default(Identify.IdentifyDevice);
|
||||
byte[] ataIdentify = cmdBuf;
|
||||
double totalDuration = 0;
|
||||
double currentSpeed = 0;
|
||||
|
||||
@@ -93,7 +93,7 @@ partial class Dump
|
||||
decMode = Modes.DecodeMode6(cmdBuf, _dev.ScsiType);
|
||||
|
||||
if(decMode.HasValue)
|
||||
scsiMediumType = (byte)decMode.Value.Header.MediumType;
|
||||
scsiMediumType = (byte)(decMode?.Header.MediumType ?? default(MediumTypes));
|
||||
|
||||
if(blockSize != 2048)
|
||||
{
|
||||
|
||||
@@ -78,23 +78,23 @@ partial class Dump
|
||||
|
||||
InitProgress?.Invoke();
|
||||
|
||||
if(decSense.HasValue && decSense.Value.SenseKey != SenseKeys.NoSense)
|
||||
if(decSense.HasValue && decSense?.SenseKey != SenseKeys.NoSense)
|
||||
{
|
||||
_dumpLog.WriteLine(Localization.Core.Device_not_ready_Sense, decSense.Value.SenseKey, decSense.Value.ASC,
|
||||
decSense.Value.ASCQ);
|
||||
_dumpLog.WriteLine(Localization.Core.Device_not_ready_Sense, decSense?.SenseKey, decSense?.ASC,
|
||||
decSense?.ASCQ);
|
||||
|
||||
StoppingErrorMessage?.Invoke(Localization.Core.Drive_has_status_error_please_correct_Sense_follows +
|
||||
Environment.NewLine +
|
||||
decSense.Value.Description);
|
||||
decSense?.Description);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Not in BOM/P
|
||||
if(decSense is { ASC: 0x00 } &&
|
||||
decSense.Value.ASCQ != 0x00 &&
|
||||
decSense.Value.ASCQ != 0x04 &&
|
||||
decSense.Value.SenseKey != SenseKeys.IllegalRequest)
|
||||
if(decSense is { ASC: 0x00 } &&
|
||||
decSense?.ASCQ != 0x00 &&
|
||||
decSense?.ASCQ != 0x04 &&
|
||||
decSense?.SenseKey != SenseKeys.IllegalRequest)
|
||||
{
|
||||
_dumpLog.WriteLine(Localization.Core.Rewinding_please_wait);
|
||||
PulseProgress?.Invoke(Localization.Core.Rewinding_please_wait);
|
||||
@@ -116,17 +116,16 @@ partial class Dump
|
||||
|
||||
// And yet, did not rewind!
|
||||
if(decSense.HasValue &&
|
||||
(decSense.Value.ASC == 0x00 && decSense.Value.ASCQ != 0x04 && decSense.Value.ASCQ != 0x00 ||
|
||||
decSense.Value.ASC != 0x00))
|
||||
(decSense?.ASC == 0x00 && decSense?.ASCQ != 0x04 && decSense?.ASCQ != 0x00 || decSense?.ASC != 0x00))
|
||||
{
|
||||
StoppingErrorMessage?.Invoke(Localization.Core.Drive_could_not_rewind_please_correct_Sense_follows +
|
||||
Environment.NewLine +
|
||||
decSense.Value.Description);
|
||||
decSense?.Description);
|
||||
|
||||
_dumpLog.WriteLine(Localization.Core.Drive_could_not_rewind_please_correct_Sense_follows);
|
||||
|
||||
_dumpLog.WriteLine(Localization.Core.Device_not_ready_Sense, decSense.Value.SenseKey,
|
||||
decSense.Value.ASC, decSense.Value.ASCQ);
|
||||
_dumpLog.WriteLine(Localization.Core.Device_not_ready_Sense, decSense?.SenseKey, decSense?.ASC,
|
||||
decSense?.ASCQ);
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -142,17 +141,17 @@ partial class Dump
|
||||
decSense = Sense.Decode(senseBuf);
|
||||
|
||||
if(decSense.HasValue &&
|
||||
(decSense.Value.ASC == 0x20 && decSense.Value.ASCQ != 0x00 ||
|
||||
decSense.Value.ASC != 0x20 && decSense.Value.SenseKey != SenseKeys.IllegalRequest))
|
||||
(decSense?.ASC == 0x20 && decSense?.ASCQ != 0x00 ||
|
||||
decSense?.ASC != 0x20 && decSense?.SenseKey != SenseKeys.IllegalRequest))
|
||||
{
|
||||
StoppingErrorMessage?.Invoke(Localization.Core.Could_not_get_position_Sense_follows +
|
||||
Environment.NewLine +
|
||||
decSense.Value.Description);
|
||||
decSense?.Description);
|
||||
|
||||
_dumpLog.WriteLine(Localization.Core.Could_not_get_position_Sense_follows);
|
||||
|
||||
_dumpLog.WriteLine(Localization.Core.Device_not_ready_Sense, decSense.Value.SenseKey,
|
||||
decSense.Value.ASC, decSense.Value.ASCQ);
|
||||
_dumpLog.WriteLine(Localization.Core.Device_not_ready_Sense, decSense?.SenseKey, decSense?.ASC,
|
||||
decSense?.ASCQ);
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -194,17 +193,16 @@ partial class Dump
|
||||
|
||||
// And yet, did not rewind!
|
||||
if(decSense.HasValue &&
|
||||
(decSense.Value.ASC == 0x00 && decSense.Value.ASCQ != 0x04 && decSense.Value.ASCQ != 0x00 ||
|
||||
decSense.Value.ASC != 0x00))
|
||||
(decSense?.ASC == 0x00 && decSense?.ASCQ != 0x04 && decSense?.ASCQ != 0x00 || decSense?.ASC != 0x00))
|
||||
{
|
||||
StoppingErrorMessage?.Invoke(Localization.Core.Drive_could_not_rewind_please_correct_Sense_follows +
|
||||
Environment.NewLine +
|
||||
decSense.Value.Description);
|
||||
decSense?.Description);
|
||||
|
||||
_dumpLog.WriteLine(Localization.Core.Drive_could_not_rewind_please_correct_Sense_follows);
|
||||
|
||||
_dumpLog.WriteLine(Localization.Core.Device_not_ready_Sense, decSense.Value.SenseKey,
|
||||
decSense.Value.ASC, decSense.Value.ASCQ);
|
||||
_dumpLog.WriteLine(Localization.Core.Device_not_ready_Sense, decSense?.SenseKey, decSense?.ASC,
|
||||
decSense?.ASCQ);
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -289,12 +287,12 @@ partial class Dump
|
||||
// TODO: Check partitions page
|
||||
if(decMode.HasValue)
|
||||
{
|
||||
scsiMediumTypeTape = (byte)decMode.Value.Header.MediumType;
|
||||
scsiMediumTypeTape = (byte)(decMode?.Header.MediumType ?? default(MediumTypes));
|
||||
|
||||
if(decMode.Value.Header.BlockDescriptors?.Length > 0)
|
||||
scsiDensityCodeTape = (byte)decMode.Value.Header.BlockDescriptors[0].Density;
|
||||
if(decMode?.Header.BlockDescriptors?.Length > 0)
|
||||
scsiDensityCodeTape = (byte)(decMode?.Header.BlockDescriptors[0].Density ?? default(DensityType));
|
||||
|
||||
blockSize = decMode.Value.Header.BlockDescriptors?[0].BlockLength ?? 0;
|
||||
blockSize = decMode?.Header.BlockDescriptors?[0].BlockLength ?? 0;
|
||||
|
||||
UpdateStatus?.Invoke(string.Format(Localization.Core.Device_reports_0_blocks, blocks));
|
||||
}
|
||||
|
||||
@@ -176,15 +176,15 @@ partial class Dump
|
||||
|
||||
if(decMode.HasValue)
|
||||
{
|
||||
scsiMediumType = (byte)decMode.Value.Header.MediumType;
|
||||
scsiMediumType = (byte)(decMode?.Header.MediumType ?? default(MediumTypes));
|
||||
|
||||
if(decMode.Value.Header.BlockDescriptors?.Length > 0)
|
||||
scsiDensityCode = (byte)decMode.Value.Header.BlockDescriptors[0].Density;
|
||||
if(decMode?.Header.BlockDescriptors?.Length > 0)
|
||||
scsiDensityCode = (byte)(decMode?.Header.BlockDescriptors[0].Density ?? default(DensityType));
|
||||
|
||||
// TODO: Fix this
|
||||
containsFloppyPage = decMode.Value.Pages?.Aggregate(containsFloppyPage,
|
||||
(current, modePage) =>
|
||||
current | modePage.Page == 0x05) ==
|
||||
containsFloppyPage = decMode?.Pages?.Aggregate(containsFloppyPage,
|
||||
(current, modePage) =>
|
||||
current | modePage.Page == 0x05) ==
|
||||
true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -356,7 +356,8 @@ partial class Dump
|
||||
return;
|
||||
}
|
||||
|
||||
PFI.PhysicalFormatInformation wxRipperPfi = wxRipperPfiNullable.Value;
|
||||
// Guaranteed to never fall into default
|
||||
PFI.PhysicalFormatInformation wxRipperPfi = wxRipperPfiNullable ?? default(PFI.PhysicalFormatInformation);
|
||||
|
||||
UpdateStatus?.Invoke(string.Format(Localization.Core.WxRipper_PFI_Data_Area_Start_PSN_0_sectors,
|
||||
wxRipperPfi.DataAreaStartPSN));
|
||||
|
||||
@@ -126,7 +126,8 @@ public sealed partial class DeviceReport
|
||||
return;
|
||||
}
|
||||
|
||||
FullTOC.CDFullTOC toc = decodedToc.Value;
|
||||
// Guaranteed to never fall into default
|
||||
FullTOC.CDFullTOC toc = decodedToc ?? default(FullTOC.CDFullTOC);
|
||||
|
||||
FullTOC.TrackDataDescriptor leadOutTrack = toc.TrackDescriptors.FirstOrDefault(t => t.POINT == 0xA2);
|
||||
|
||||
@@ -241,7 +242,8 @@ public sealed partial class DeviceReport
|
||||
return;
|
||||
}
|
||||
|
||||
toc = decodedToc.Value;
|
||||
// Guaranteed to never fall into default
|
||||
toc = decodedToc ?? default(FullTOC.CDFullTOC);
|
||||
|
||||
FullTOC.TrackDataDescriptor newLeadOutTrack = toc.TrackDescriptors.FirstOrDefault(t => t.POINT == 0xA2);
|
||||
|
||||
|
||||
@@ -1373,7 +1373,7 @@ public sealed partial class DeviceReport
|
||||
if(mediaType == "Audio CD")
|
||||
{
|
||||
mediaTest.CanReadLeadOut = !_dev.ReadCd(out buffer, out senseBuffer,
|
||||
(uint)(mediaTest.Blocks + 1), 2352, 1,
|
||||
(uint)((mediaTest.Blocks ?? 0) + 1), 2352, 1,
|
||||
MmcSectorTypes.Cdda, false, false, false,
|
||||
MmcHeaderCodes.None, true, false, MmcErrorField.None,
|
||||
MmcSubchannel.None, _dev.Timeout, out _);
|
||||
@@ -1381,7 +1381,7 @@ public sealed partial class DeviceReport
|
||||
else
|
||||
{
|
||||
mediaTest.CanReadLeadOut = !_dev.ReadCd(out buffer, out senseBuffer,
|
||||
(uint)(mediaTest.Blocks + 1), 2352, 1,
|
||||
(uint)((mediaTest.Blocks ?? 0) + 1), 2352, 1,
|
||||
MmcSectorTypes.AllTypes, false, false, true,
|
||||
MmcHeaderCodes.AllHeaders, true, true,
|
||||
MmcErrorField.None, MmcSubchannel.None, _dev.Timeout,
|
||||
|
||||
@@ -198,7 +198,7 @@ public sealed partial class DeviceReport
|
||||
seqTest.MediumType = (byte)decMode.Value.Header.MediumType;
|
||||
|
||||
if(decMode.Value.Header.BlockDescriptors?.Length > 0)
|
||||
seqTest.Density = (byte)decMode.Value.Header.BlockDescriptors?[0].Density;
|
||||
seqTest.Density = (byte)(decMode.Value.Header.BlockDescriptors?[0].Density ?? default(DensityType));
|
||||
}
|
||||
|
||||
Spectre.ProgressSingleSpinner(ctx =>
|
||||
|
||||
Reference in New Issue
Block a user