Skip to content

Commit

Permalink
Address some CodeQL comments. Rework Merge to address an issue with s…
Browse files Browse the repository at this point in the history
…hared entries. Still need to address shift-copy issues.
  • Loading branch information
mdickson committed Nov 29, 2023
1 parent b1ad5c2 commit bfa2aa6
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions OpenSim/Region/Framework/Scenes/LinksetData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public int AddOrUpdateLinksetDataKey(string key, string value, string pass)
return 1;

LinksetDataEntry entry = null;
if (Data.TryGetValue(key, out entry) == true)
if (Data.TryGetValue(key, out entry))
{
if (entry.CheckPassword(pass) == false)
if (!entry.CheckPassword(pass))
return -1;

if (entry.Value == value)
Expand Down Expand Up @@ -85,10 +85,10 @@ public int DeleteLinksetDataKey(string key, string pass)
return -1;

LinksetDataEntry entry;
if (Data.TryGetValue(key, out entry) == false)
if (!Data.TryGetValue(key, out entry))
return -1;

if (entry.CheckPassword(pass) == false)
if (!entry.CheckPassword(pass))
return 1;

Data.Remove(key);
Expand Down Expand Up @@ -234,6 +234,7 @@ public bool LinksetDataOverLimit()
{
return (LinksetDataBytesFree < 0);
}

/// <summary>
/// Merge the linksetData present in another Linkset into this one.
/// The current root will have the new linkset for the merged sog.
Expand All @@ -242,18 +243,13 @@ public bool LinksetDataOverLimit()
/// <param name="otherLinkset"></param>
public void MergeLinksetData(LinksetData otherLinksetData)
{
// Nothing to merge?
if (otherLinksetData == null)
return;

lock (linksetDataLock)
{
var values = otherLinksetData.Data.ToArray();

otherLinksetData.Data.Clear();
otherLinksetData.LinksetDataBytesFree = 0;
otherLinksetData.LinksetDataBytesUsed = LINKSETDATA_MAX;

foreach (var kvp in values)
foreach (var kvp in otherLinksetData.Data)
{
// If its already present skip it
if (Data.ContainsKey(kvp.Key))
Expand All @@ -263,10 +259,15 @@ public void MergeLinksetData(LinksetData otherLinksetData)
if (LinksetDataOverLimit())
break;

// Do we send events?
Data.Add(kvp.Key, kvp.Value);
LinksetDataAccountingDelta(kvp.Value.GetCost(kvp.Key));
var value = new LinksetDataEntry(kvp.Value.Value, kvp.Value.Password);
Data.Add(kvp.Key, value);
LinksetDataAccountingDelta(value.GetCost(kvp.Key));
}

// Clear the LinksetData entries from the "other" SOG
otherLinksetData.Data.Clear();
otherLinksetData.LinksetDataBytesFree = 0;
otherLinksetData.LinksetDataBytesUsed = LINKSETDATA_MAX;
}
}

Expand All @@ -281,12 +282,12 @@ public string ReadLinksetData(string key, string pass)
lock (linksetDataLock)
{
if (Data.Count <= 0)
{
LinksetDataEntry entry;
if (Data.TryGetValue(key, out entry) == true)
return entry.CheckPasswordAndGetValue(pass);

return string.Empty;

LinksetDataEntry entry;
if (Data.TryGetValue(key, out entry))
{
return entry.CheckPasswordAndGetValue(pass);
}

return string.Empty;
Expand Down Expand Up @@ -349,18 +350,12 @@ public bool CheckPassword(string pass)
{
// A undocumented caveat for LinksetData appears to be that even for unprotected values,
// if a pass is provided, it is still treated as protected
if (this.Password == pass)
return true;
else
return false;
return this.Password == pass ? true : false;

Check notice

Code scanning / CodeQL

Unnecessarily complex Boolean expression Note

The expression 'A ? true : false' can be simplified to 'A'.
}

public string CheckPasswordAndGetValue(string pass)
{
if (string.IsNullOrEmpty(this.Password) || (this.Password == pass))
return this.Value;
else
return string.Empty;
return (string.IsNullOrEmpty(this.Password) || (this.Password == pass)) ? this.Value : string.Empty;
}

/// <summary>
Expand All @@ -376,7 +371,7 @@ public int GetCost(string key)
cost += Encoding.UTF8.GetBytes(key).Length;
cost += Encoding.UTF8.GetBytes(this.Value).Length;

if (string.IsNullOrEmpty(this.Password) == false)
if (!string.IsNullOrEmpty(this.Password))
{
// For parity, the pass adds 32 bytes regardless of the length. See LL caveats
cost += Math.Max(Encoding.UTF8.GetBytes(this.Password).Length, 32);
Expand All @@ -387,7 +382,7 @@ public int GetCost(string key)

public bool IsProtected()
{
return (string.IsNullOrEmpty(this.Password) == false);
return !string.IsNullOrEmpty(Password);
}
}
}

0 comments on commit bfa2aa6

Please sign in to comment.