Skip to content

Commit 3903119

Browse files
author
Coder
committed
fix(code-quality): Address null dereferences and use short-circuiting operator
1 parent 3aabf98 commit 3903119

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

DotNetPlugin.Impl/Plugin.Commands.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static void cbNetTestCommand ( string[] args )
2525
Console.WriteLine ( ".Net test command!" );
2626
string empty = string.Empty;
2727
string Left = Interaction.InputBox ( "Enter value pls", "NetTest", "", -1, -1 );
28-
if ( Left == null | Operators.CompareString ( Left, "", false ) == 0 )
28+
if ( Left == null || Operators.CompareString ( Left, "", false ) == 0 )
2929
{ Console.WriteLine ( "cancel pressed!" ); }
3030
else
3131
{ Console.WriteLine ( $"line: {Left}" ); }
@@ -950,6 +950,10 @@ public static string GetLabel ( nuint address )
950950
string TryGetDereferencedString ( nuint address )
951951
{
952952
var data = ReadMemory ( address, 64 ); // read 64 bytes (arbitrary)
953+
if ( data == null )
954+
{
955+
return null;
956+
}
953957
int end = Array.IndexOf ( data, ( byte ) 0 );
954958
if ( end <= 0 )
955959
{
@@ -1157,8 +1161,10 @@ public static void LabelMatchingBytes ( nuint address, byte[] pattern, string va
11571161
{
11581162
byte[] actualBytes = ReadMemory ( address, ( uint ) pattern.Length );
11591163

1160-
if ( actualBytes.Length != pattern.Length )
1161-
{ return; }
1164+
if ( actualBytes == null || actualBytes.Length != pattern.Length )
1165+
{
1166+
return;
1167+
}
11621168

11631169
for ( int i = 0; i < pattern.Length; i++ )
11641170
{
@@ -1579,7 +1585,7 @@ private static bool TryResolveSymbols ( nuint address, out AddressSymbols symbol
15791585
if ( !string.IsNullOrEmpty ( retrievedComment ) )
15801586
{
15811587
// Handle auto-comment marker (\1)
1582-
symbols.Comment = ( retrievedComment.Length > 0 && retrievedComment[0] == '\x01' )
1588+
symbols.Comment = ( retrievedComment[0] == '\x01' )
15831589
? retrievedComment.Substring ( 1 )
15841590
: retrievedComment;
15851591
}
@@ -1890,18 +1896,28 @@ private static nuint GetPotentialStringPointer ( Bridge.BASIC_INSTRUCTION_INFO d
18901896
private static string ExtractStringFromMemory ( nuint address )
18911897
{
18921898
var strData = ReadMemory ( address, 64 );
1899+
if ( strData == null )
1900+
{
1901+
return null;
1902+
}
18931903
int len = Array.IndexOf ( strData, ( byte ) 0 );
18941904

18951905
if ( len <= 0 )
1896-
{ return null; }
1906+
{
1907+
return null;
1908+
}
18971909

18981910
var decoded = Encoding.ASCII.GetString ( strData, 0, len );
18991911

19001912
// Check if the string contains only printable ASCII characters
19011913
if ( decoded.All ( c => c >= 0x20 && c < 0x7F ) )
1902-
{ return decoded; }
1903-
1904-
return null;
1914+
{
1915+
return decoded;
1916+
}
1917+
else
1918+
{
1919+
return null;
1920+
}
19051921
}
19061922

19071923
/// <summary>

0 commit comments

Comments
 (0)