Skip to content

Commit

Permalink
remove debug only uses of linq
Browse files Browse the repository at this point in the history
  • Loading branch information
Wraith2 committed Mar 8, 2023
1 parent 263f82e commit d7462c0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
using System;
using System.Diagnostics;
using System.Threading;
#if DEBUG
using System.Linq;
#endif
using System.Collections.Generic;

namespace Microsoft.Data.SqlClient.SNI
{
Expand Down Expand Up @@ -85,12 +83,19 @@ public override void ReturnPacket(SNIPacket packet)
#if DEBUG
private string GetStackParts()
{
return string.Join(Environment.NewLine,
Environment.StackTrace
.Split(new string[] { Environment.NewLine }, StringSplitOptions.None)
.Skip(3) // trims off the common parts at the top of the stack so you can see what the actual caller was
.Take(7) // trims off most of the bottom of the stack because when running under xunit there's a lot of spam
);
// trims off the common parts at the top of the stack so you can see what the actual caller was
// trims off most of the bottom of the stack because when running under xunit there's a lot of spam
string[] parts = Environment.StackTrace.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
List<string> take = new List<string>(7);
for (int index = 0; take.Count < 7 && index < parts.Length; index++)
{
if (index > 2)
{
take.Add(parts[index]);
}
}

return string.Join(Environment.NewLine, take.ToArray());
}
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ namespace Microsoft.Data.Common
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
#if DEBUG
using System.Linq;
#endif
using System.Text;
using Microsoft.Data.SqlClient;

Expand Down Expand Up @@ -312,7 +309,12 @@ private void ValidateCombinedSet(DBConnectionString componentSet, DBConnectionSt
// Component==Allow, Combined==Allow
// All values in the Combined Set should also be in the Component Set
// Combined - Component == null
Debug.Assert(combinedSet._restrictionValues.Except(componentSet._restrictionValues).Count() == 0, "Combined set allows values not allowed by component set");
#if DEBUG
HashSet<string> combined = new HashSet<string>(combinedSet._restrictionValues);
HashSet<string> component = new HashSet<string>(componentSet._restrictionValues);
combined.ExceptWith(component);
Debug.Assert(combined.Count == 0, "Combined set allows values not allowed by component set");
#endif
}
else if (combinedSet._behavior == KeyRestrictionBehavior.PreventUsage)
{
Expand All @@ -331,14 +333,24 @@ private void ValidateCombinedSet(DBConnectionString componentSet, DBConnectionSt
// Component==PreventUsage, Combined==Allow
// There shouldn't be any of the values from the Component Set in the Combined Set
// Intersect(Component, Combined) == null
Debug.Assert(combinedSet._restrictionValues.Intersect(componentSet._restrictionValues).Count() == 0, "Combined values allows values prevented by component set");
#if DEBUG
HashSet<string> combined = new HashSet<string>(combinedSet._restrictionValues);
HashSet<string> component = new HashSet<string>(componentSet._restrictionValues);
combined.IntersectWith(component);
Debug.Assert(combined.Count == 0, "Combined values allows values prevented by component set");
#endif
}
else if (combinedSet._behavior == KeyRestrictionBehavior.PreventUsage)
{
// Component==PreventUsage, Combined==PreventUsage
// All values in the Component Set should also be in the Combined Set
// Component - Combined == null
Debug.Assert(componentSet._restrictionValues.Except(combinedSet._restrictionValues).Count() == 0, "Combined values does not prevent all of the values prevented by the component set");
#if DEBUG
HashSet<string> combined = new HashSet<string>(combinedSet._restrictionValues);
HashSet<string> component = new HashSet<string>(componentSet._restrictionValues);
component.IntersectWith(combined);
Debug.Assert(component.Count == 0, "Combined values does not prevent all of the values prevented by the component set");
#endif
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
#if DEBUG
using System.Linq;
#endif
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -3336,7 +3333,16 @@ internal void CloneCleanupAltMetaDataSetArray()
internal void PushBuffer(byte[] buffer, int read)
{
#if DEBUG
Debug.Assert(!_snapshotInBuffs.Any(b => object.ReferenceEquals(b, buffer)));
if (_snapshotInBuffs != null && _snapshotInBuffs.Count > 0)
{
foreach (PacketData packet in _snapshotInBuffs)
{
if (object.ReferenceEquals(packet.Buffer, buffer))
{
Debug.Assert(false,"buffer is already present in packet list");
}
}
}
#endif
PacketData packetData = new PacketData();
packetData.Buffer = buffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Linq;
using System.Runtime.Caching;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
Expand Down Expand Up @@ -220,7 +219,17 @@ private X509Certificate2Collection GetSigningCertificate(string attestationUrl,
// Checks if any certificates in the collection are expired
private bool AnyCertificatesExpired(X509Certificate2Collection certificates)
{
return certificates.OfType<X509Certificate2>().Any(c => c.NotAfter < DateTime.Now);
if (certificates != null)
{
foreach (object item in certificates)
{
if (item is X509Certificate2 certificate && certificate.NotAfter < DateTime.Now)
{
return true;
}
}
}
return false;
}

// Verifies that a chain of trust can be built from the health report provided
Expand Down Expand Up @@ -325,7 +334,31 @@ private void VerifyEnclavePolicy(EnclaveReportPackage enclaveReportPackage)
// Verifies a byte[] enclave policy property
private void VerifyEnclavePolicyProperty(string property, byte[] actual, byte[] expected)
{
if (!actual.SequenceEqual(expected))
bool different = false;
if (actual == null || expected == null)
{
different = true;
}
else
{
if (actual.Length != expected.Length)
{
different = true;
}
else
{
for (int index = 0; index < actual.Length; index++)
{
if (actual[index] != expected[index])
{
different = true;
break;
}
}
}
}

if (different)
{
string exceptionMessage = String.Format(Strings.VerifyEnclavePolicyFailedFormat, property, BitConverter.ToString(actual), BitConverter.ToString(expected));
throw new ArgumentException(exceptionMessage);
Expand Down

0 comments on commit d7462c0

Please sign in to comment.