Skip to content

Commit 508d8c4

Browse files
committed
Improve null safety
1 parent e74208d commit 508d8c4

File tree

8 files changed

+18
-17
lines changed

8 files changed

+18
-17
lines changed

Protest/Database/ContactsJsonConverter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ public override void Write(Utf8JsonWriter writer, Database value, JsonSerializer
4444

4545
writer.WriteStartObject();
4646

47-
if (title?.value.Length > 0) writer.WriteString(_title, title?.value ?? String.Empty);
47+
if (title?.value?.Length > 0) writer.WriteString(_title, title?.value ?? String.Empty);
4848
if (name?.Length > 0) writer.WriteString(_name, name);
49-
if (department?.value.Length > 0) writer.WriteString(_department, department?.value ?? String.Empty);
49+
if (department?.value?.Length > 0) writer.WriteString(_department, department?.value ?? String.Empty);
5050

51-
if (email?.value.Length > 0) writer.WriteString(_email, email?.value ?? String.Empty);
52-
if (telephoneNumber?.value.Length > 0) writer.WriteString(_telephone, telephoneNumber?.value ?? String.Empty);
53-
if (mobileNumber?.value.Length > 0) writer.WriteString(_mobile, mobileNumber?.value ?? String.Empty);
51+
if (email?.value?.Length > 0) writer.WriteString(_email, email?.value ?? String.Empty);
52+
if (telephoneNumber?.value?.Length > 0) writer.WriteString(_telephone, telephoneNumber?.value ?? String.Empty);
53+
if (mobileNumber?.value?.Length > 0) writer.WriteString(_mobile, mobileNumber?.value ?? String.Empty);
5454

5555
writer.WriteEndObject();
5656
}

Protest/Protocols/Dhcp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ private static byte[] Request(long timestamp, byte[] transactionId, string mac,
414414

415415
mac = mac.Replace(":", String.Empty);
416416
mac = mac.Replace("-", String.Empty);
417-
if (mac is not null || mac.Length == 12) {
417+
if (mac is not null && mac.Length == 12) {
418418
buffer[index++] = Convert.ToByte(mac[0..2], 16); //client mac address
419419
buffer[index++] = Convert.ToByte(mac[2..4], 16);
420420
buffer[index++] = Convert.ToByte(mac[4..6], 16);

Protest/Proxy/TcpReverseProxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private async Task ServeClient(TcpClient proxyClient) {
6868
Interlocked.Increment(ref this.errors);
6969
}
7070
finally {
71-
proxyClient?.Close();
71+
proxyClient.Close();
7272
}
7373
}
7474

Protest/Tasks/Issues.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
namespace Protest.Tasks;
1818

1919
internal static class Issues {
20+
public const long UNIX_BASE_TICKS = 62135596800000;
21+
2022
private const int MIN_LIFELINE_ENTRIES = 10;
2123
private const double WEAK_PASSWORD_ENTROPY_THRESHOLD = 36.0;
2224
private const double RTT_STANDARD_DEVIATION_MULTIPLIER = 20.0;
@@ -26,6 +28,7 @@ internal static class Issues {
2628
private const int DISK_SPACE_THRESHOLD = 85;
2729
private const int DISK_IO_THRESHOLD = 75;
2830

31+
2932
public enum SeverityLevel : byte {
3033
info = 1,
3134
warning = 2,
@@ -132,7 +135,7 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
132135

133136
if (filtered.Any()) {
134137
byte[] bytes = JsonSerializer.SerializeToUtf8Bytes(filtered.Select(o => new {
135-
timestamp = o.timestamp / 10000 - 62135596800000,
138+
timestamp = o.timestamp / 10000 - UNIX_BASE_TICKS,
136139
severity = o.severity,
137140
issue = o.message,
138141
name = o.name,
@@ -206,7 +209,7 @@ private static void ScanDevices() {
206209
CheckMacAddresses();
207210

208211
foreach (KeyValuePair<string, Database.Entry> host in ipAddresses) {
209-
if (CheckRtt(host.Value, host.Key, out Issue? issue)) {
212+
if (CheckRtt(host.Value, host.Key, out Issue? issue) && issue.HasValue) {
210213
issues.Add(issue.Value);
211214
}
212215
}

Protest/Tasks/Lifeline.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ private static void LifelineLoop() {
189189
wmiThread?.Join();
190190
snmpThread?.Join();
191191

192-
task.status = TaskWrapper.TaskStatus.Idle;
192+
task?.status = TaskWrapper.TaskStatus.Idle;
193193
task.Sleep(Math.Max((int)((TWO_HOURS_IN_TICKS - (DateTime.UtcNow.Ticks - startTimeStamp)) / 10_000), 0));
194194

195195
if (task.cancellationToken.IsCancellationRequested) {

Protest/Tasks/Watchdog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private static void WatchLoop() {
138138
Thread.Sleep((int)gap);
139139

140140
while (true) {
141-
task.status = TaskWrapper.TaskStatus.Running;
141+
task?.status = TaskWrapper.TaskStatus.Running;
142142

143143
long loopStartTimeStamp = DateTime.UtcNow.Ticks;
144144

Protest/Tools/IpDiscovery.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,10 @@ private static void DiscoverAdapter(ConcurrentDictionary<string, HostEntry> dic,
219219
}
220220

221221
if (gwIpV4 is not null || gwIpV6 is not null) {
222-
string ipv4String = gwIpV4.ToString();
222+
string ipv4String = gwIpV4?.ToString() ?? null;
223223

224-
string hostname = NetBios.GetBiosName(ipv4String, 200);
225-
string mac = Arp.ArpRequest(ipv4String);
224+
string hostname = ipv4String is null ? String.Empty : NetBios.GetBiosName(ipv4String, 200);
225+
string mac = ipv4String is null ? String.Empty : Arp.ArpRequest(ipv4String);
226226

227227
HostEntry gwHost = new HostEntry() {
228228
description = String.Empty,

Protest/Tools/LiveStats.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,7 @@ private static void WmiQuery(WebSocket ws, Lock mutex, string firstAlive, ref st
379379
WsWriteText(ws, $"{{\"activeUser\":\"{Data.EscapeJsonText(username)}\",\"source\":\"WMI\"}}", mutex);
380380
}
381381
}
382-
catch (NullReferenceException ex) {
383-
Logger.Error(ex);
384-
}
382+
//catch (NullReferenceException) { }
385383
catch { }
386384
}
387385

0 commit comments

Comments
 (0)