Skip to content

Commit

Permalink
Enable .NET analyzers for all projects (#1414)
Browse files Browse the repository at this point in the history
* Enable .NET analyzers for all projects
* fix all new warnings, mostly security and perf
  • Loading branch information
mregen committed Jun 5, 2021
1 parent 8406716 commit 9923141
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Libraries/Opc.Ua.Client.ComplexTypes/ComplexTypeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ bool fullTypeList
var typeSystem = await m_session.LoadDataTypeSystem().ConfigureAwait(false);

// sort dictionaries with import dependencies to the end of the list
var sortedTypeSystem = typeSystem.OrderBy(t => t.Value.TypeDictionary?.Import?.Count()).ToList();
var sortedTypeSystem = typeSystem.OrderBy(t => t.Value.TypeDictionary?.Import?.Length).ToList();

bool allTypesLoaded = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static void DataMemberAttribute(this PropertyBuilder typeBuilder, string
ConstructorInfo ctorInfo = attributeType.GetConstructor(Type.EmptyTypes);
CustomAttributeBuilder builder = new CustomAttributeBuilder(
ctorInfo,
new object[0], // constructor arguments
Array.Empty<object>(), // constructor arguments
new[] // properties to assign
{
attributeType.GetProperty("DefaultEncodingId"),
Expand Down Expand Up @@ -117,7 +117,7 @@ ExpandedNodeId xmlEncodingId
ConstructorInfo ctorInfo = attributeType.GetConstructor(Type.EmptyTypes);
CustomAttributeBuilder builder = new CustomAttributeBuilder(
ctorInfo,
new object[0], // constructor arguments
Array.Empty<object>(), // constructor arguments
new[] // properties to assign
{
attributeType.GetProperty("ComplexTypeId"),
Expand All @@ -144,7 +144,7 @@ ExpandedNodeId xmlEncodingId
ConstructorInfo ctorInfo = attributeType.GetConstructor(Type.EmptyTypes);
CustomAttributeBuilder builder = new CustomAttributeBuilder(
ctorInfo,
new object[0], // constructor arguments
Array.Empty<object>(), // constructor arguments
new[] // properties to assign
{
attributeType.GetProperty("ValueRank"),
Expand All @@ -170,7 +170,7 @@ public static void EnumMemberAttribute(this FieldBuilder typeBuilder, string Nam
ConstructorInfo ctorInfo = attributeType.GetConstructor(Type.EmptyTypes);
CustomAttributeBuilder builder = new CustomAttributeBuilder(
ctorInfo,
new object[0], // constructor arguments
Array.Empty<object>(), // constructor arguments
new[] // properties to assign
{
attributeType.GetProperty("Value")
Expand All @@ -193,7 +193,7 @@ private static CustomAttributeBuilder DataMemberAttributeBuilder(string name, bo
ConstructorInfo ctorInfo = attributeType.GetConstructor(Type.EmptyTypes);
CustomAttributeBuilder builder = new CustomAttributeBuilder(
ctorInfo,
new object[0], // constructor arguments
Array.Empty<object>(), // constructor arguments
new[] // properties to assign
{
attributeType.GetProperty("Name"),
Expand All @@ -218,7 +218,7 @@ private static CustomAttributeBuilder DataContractAttributeBuilder(string Namesp
ConstructorInfo ctorInfo = attributeType.GetConstructor(Type.EmptyTypes);
CustomAttributeBuilder builder = new CustomAttributeBuilder(
ctorInfo,
new object[0], // constructor arguments
Array.Empty<object>(), // constructor arguments
new[] // properties to assign
{
attributeType.GetProperty("Namespace")
Expand Down
8 changes: 4 additions & 4 deletions Libraries/Opc.Ua.Client.ComplexTypes/Types/BaseComplexType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ private void AddSeparator(StringBuilder body)
{
if (body.Length == 0)
{
body.Append("{");
body.Append('{');
}
else
{
Expand All @@ -302,17 +302,17 @@ private void AddSeparator(StringBuilder body)
{
bool first = true;
var enumerable = value as IEnumerable;
body.Append("[");
body.Append('[');
foreach (var item in enumerable)
{
if (!first)
{
body.Append(",");
body.Append(',');
}
AppendPropertyValue(formatProvider, body, item);
first = false;
}
body.Append("]");
body.Append(']');
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public override string ToString(string format, IFormatProvider formatProvider)

if (body.Length > 0)
{
body.Append("}");
body.Append('}');
return body.ToString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public override string ToString(string format, IFormatProvider formatProvider)

if (body.Length > 0)
{
body.Append("}");
body.Append('}');
return body.ToString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ========================================================================
/* ========================================================================
* Copyright (c) 2005-2020 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
Expand Down Expand Up @@ -494,7 +494,7 @@ private static List<string> Parse(string pattern)
ii++;
}

buffer.Append("]");
buffer.Append(']');
tokens.Add(buffer.ToString());
buffer.Length = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ private string GetSubjectName(ApplicationRecordDataType application, Certificate
{
if (builder.Length > 0)
{
builder.Append(",");
builder.Append(',');
}

if (field.StartsWith("CN=", StringComparison.Ordinal))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public void Start()
httpsOptions.CheckCertificateRevocation = false;
httpsOptions.ClientCertificateMode = ClientCertificateMode.NoCertificate;
httpsOptions.ServerCertificate = m_serverCert;
httpsOptions.SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
httpsOptions.SslProtocols = SslProtocols.None;
m_hostBuilder.UseKestrel(options => {
options.Listen(IPAddress.Any, m_uri.Port, listenOptions => {
// listenOptions.NoDelay = true;
Expand Down Expand Up @@ -342,7 +342,11 @@ public async Task SendAsync(HttpContext context)
context.Response.ContentLength = response.Length;
context.Response.ContentType = context.Request.ContentType;
context.Response.StatusCode = (int)HttpStatusCode.OK;
#if NETSTANDARD2_1
await context.Response.Body.WriteAsync(response.AsMemory(0, response.Length)).ConfigureAwait(false);
#else
await context.Response.Body.WriteAsync(response, 0, response.Length).ConfigureAwait(false);
#endif
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@ public async Task TestAutoAccept(bool trusted, bool autoAccept)
// override the autoaccept flag, but do not approve
certValidator = validator.Update();
certValidator.AutoAcceptUntrustedCertificates = autoAccept;
approver = new CertValidationApprover(new StatusCode[] { });
approver = new CertValidationApprover(Array.Empty<StatusCode>());
certValidator.CertificateValidation += approver.OnCertificateValidation;
if (trusted)
{
Expand Down
4 changes: 4 additions & 0 deletions Tests/Opc.Ua.Core.Tests/Types/Utils/UtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ public void RelativePathParseAlphanumericWithNamespaceIndexStringPath()
/// exponential entity expansion in this version of .NET.
/// </summary>
[Test]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA3075:Insecure DTD processing in XML",
Justification =
"Validate that XmlDocument DtdProcessing is protected against" +
"exponential entity expansion in this version of .NET.")]
public void ExponentialEntityExpansionProcessing()
{
StringBuilder xmlEEXX = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private string ReplaceLastIpByte(string ipAddress, string lastIpByte)
if (isValidIP)
{
byte[] ipAddressBytes = validIp.GetAddressBytes();
for (int pos = 0; pos < ipAddressBytes.Count() - 1; pos++)
for (int pos = 0; pos < ipAddressBytes.Length - 1; pos++)
{
newIPAddress += string.Format("{0}.", ipAddressBytes[pos]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private static IPAddress GetFirstNicLastIPByteChanged(byte lastIpByte)
if (isValidIP)
{
byte[] ipAddressBytes = validIp.GetAddressBytes();
ipAddressBytes[ipAddressBytes.Count() - 1] = lastIpByte;
ipAddressBytes[ipAddressBytes.Length - 1] = lastIpByte;
return new IPAddress(ipAddressBytes);
}
}
Expand Down
1 change: 1 addition & 0 deletions UA Core Library.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
azure-pipelines-preview.yml = azure-pipelines-preview.yml
azure-pipelines.yml = azure-pipelines.yml
Tests\codecoverage.cmd = Tests\codecoverage.cmd
common.props = common.props
Directory.Build.props = Directory.Build.props
lgtm.yml = lgtm.yml
Expand Down
1 change: 1 addition & 0 deletions common.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<NeutralLanguage>en-US</NeutralLanguage>
<HighEntropyVA>true</HighEntropyVA>
<IsPackable>false</IsPackable>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<!--TODO <GenerateDocumentationFile>true</GenerateDocumentationFile>-->
</PropertyGroup>
Expand Down

0 comments on commit 9923141

Please sign in to comment.