Skip to content

Commit

Permalink
fix fuzzing error
Browse files Browse the repository at this point in the history
  • Loading branch information
mregen committed Jan 16, 2025
1 parent 6d2af00 commit 536567d
Showing 1 changed file with 42 additions and 39 deletions.
81 changes: 42 additions & 39 deletions Stack/Opc.Ua.Core/Types/BuiltIn/NodeId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,15 @@ public static NodeId Parse(IServiceMessageContext context, string text, NodeIdPa

if (index < 0)
{
throw new ServiceResultException(StatusCodes.BadNodeIdInvalid, $"Invalid NodeId ({originalText}).");
throw ServiceResultException.Create(StatusCodes.BadNodeIdInvalid, "Invalid NodeId ({0}).", originalText);

Check warning on line 315 in Stack/Opc.Ua.Core/Types/BuiltIn/NodeId.cs

View check run for this annotation

Codecov / codecov/patch

Stack/Opc.Ua.Core/Types/BuiltIn/NodeId.cs#L315

Added line #L315 was not covered by tests
}

var namespaceUri = Utils.UnescapeUri(text.Substring(4, index - 4));
namespaceIndex = (options?.UpdateTables == true) ? context.NamespaceUris.GetIndexOrAppend(namespaceUri) : context.NamespaceUris.GetIndex(namespaceUri);

if (namespaceIndex < 0)
{
throw new ServiceResultException(StatusCodes.BadNodeIdInvalid, $"No mapping to NamespaceIndex for NamespaceUri ({namespaceUri}).");
throw ServiceResultException.Create(StatusCodes.BadNodeIdInvalid, "No mapping to NamespaceIndex for NamespaceUri ({0}).", namespaceUri);

Check warning on line 323 in Stack/Opc.Ua.Core/Types/BuiltIn/NodeId.cs

View check run for this annotation

Codecov / codecov/patch

Stack/Opc.Ua.Core/Types/BuiltIn/NodeId.cs#L323

Added line #L323 was not covered by tests
}

text = text.Substring(index + 1);
Expand All @@ -332,7 +332,7 @@ public static NodeId Parse(IServiceMessageContext context, string text, NodeIdPa

if (index < 0)
{
throw new ServiceResultException(StatusCodes.BadNodeIdInvalid, $"Invalid ExpandedNodeId ({originalText}).");
throw ServiceResultException.Create(StatusCodes.BadNodeIdInvalid, "Invalid ExpandedNodeId ({0}).", originalText);

Check warning on line 335 in Stack/Opc.Ua.Core/Types/BuiltIn/NodeId.cs

View check run for this annotation

Codecov / codecov/patch

Stack/Opc.Ua.Core/Types/BuiltIn/NodeId.cs#L335

Added line #L335 was not covered by tests
}

if (UInt16.TryParse(text.Substring(3, index - 3), out ushort ns))
Expand All @@ -348,58 +348,61 @@ public static NodeId Parse(IServiceMessageContext context, string text, NodeIdPa
text = text.Substring(index + 1);
}

var idType = text.Substring(0, 1);
text = text.Substring(2);

switch (idType)
if (text.Length >= 2)
{
case "i":
{
if (UInt32.TryParse(text, out uint number))
{
return new NodeId(number, (ushort)namespaceIndex);
}
char idType = text[0];
text = text.Substring(2);

break;
}

case "s":
switch (idType)
{
if (!String.IsNullOrWhiteSpace(text))
case 'i':
{
return new NodeId(text, (ushort)namespaceIndex);
}
if (UInt32.TryParse(text, out uint number))
{
return new NodeId(number, (ushort)namespaceIndex);
}

break;
}
break;
}

case "b":
{
try
case 's':
{
var bytes = Convert.FromBase64String(text);
return new NodeId(bytes, (ushort)namespaceIndex);
if (!String.IsNullOrWhiteSpace(text))
{
return new NodeId(text, (ushort)namespaceIndex);
}

break;
}
catch (Exception)

case 'b':
{
// error handled after the switch statement.
}
try
{
var bytes = Convert.FromBase64String(text);
return new NodeId(bytes, (ushort)namespaceIndex);
}
catch (Exception)

Check warning on line 385 in Stack/Opc.Ua.Core/Types/BuiltIn/NodeId.cs

View check run for this annotation

Codecov / codecov/patch

Stack/Opc.Ua.Core/Types/BuiltIn/NodeId.cs#L385

Added line #L385 was not covered by tests
{
// error handled after the switch statement.
}

Check warning on line 388 in Stack/Opc.Ua.Core/Types/BuiltIn/NodeId.cs

View check run for this annotation

Codecov / codecov/patch

Stack/Opc.Ua.Core/Types/BuiltIn/NodeId.cs#L388

Added line #L388 was not covered by tests

break;
}
break;
}

case "g":
{
if (Guid.TryParse(text, out var guid))
case 'g':
{
return new NodeId(guid, (ushort)namespaceIndex);
}
if (Guid.TryParse(text, out var guid))
{
return new NodeId(guid, (ushort)namespaceIndex);
}

break;
break;
}
}
}

throw new ServiceResultException(StatusCodes.BadNodeIdInvalid, $"Invalid NodeId Identifier ({originalText}).");
throw ServiceResultException.Create(StatusCodes.BadNodeIdInvalid, "Invalid NodeId Identifier ({0}).", originalText);

Check warning on line 405 in Stack/Opc.Ua.Core/Types/BuiltIn/NodeId.cs

View check run for this annotation

Codecov / codecov/patch

Stack/Opc.Ua.Core/Types/BuiltIn/NodeId.cs#L405

Added line #L405 was not covered by tests
}

/// <summary>
Expand Down

0 comments on commit 536567d

Please sign in to comment.