diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Commons/DataConnections/TypedValueCodec.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Commons/DataConnections/TypedValueCodec.cs
index 348864839..c9d05ef9c 100644
--- a/src/Core/ZB.MOM.WW.OtOpcUa.Commons/DataConnections/TypedValueCodec.cs
+++ b/src/Core/ZB.MOM.WW.OtOpcUa.Commons/DataConnections/TypedValueCodec.cs
@@ -100,7 +100,16 @@ public static class TypedValueCodec
throw new ArgumentOutOfRangeException(nameof(maxElements));
if (value.CalculateSize() > maxBytes || DecodedBytes(value, maxBytes) > maxBytes)
throw new ArgumentException("Decoded value exceeds budget.");
- return DecodeCore(value, maxElements, maxBytes);
+ try
+ {
+ return DecodeCore(value, maxElements, maxBytes);
+ }
+ catch (Exception ex) when (ex is OverflowException or FormatException)
+ {
+ // #999 / review K-9: an out-of-range narrowing (an Int8 carrying 300) or a malformed GUID is a bad REQUEST,
+ // so it fails as the documented ArgumentException (→ gRPC InvalidArgument), not an unmapped internal error.
+ throw new ArgumentException($"Invalid typed value: {ex.Message}", ex);
+ }
}
internal static long DecodedBytes(Wire.TypedValue value, long limit)
@@ -132,10 +141,22 @@ public static class TypedValueCodec
return size;
}
+ /// 0001-01-01T00:00:00Z as Unix seconds — the protobuf Timestamp / DateTimeOffset lower bound.
+ private const long MinTimestampSeconds = -62_135_596_800L;
+
+ /// 9999-12-31T23:59:59Z as Unix seconds — the protobuf Timestamp / DateTimeOffset upper bound.
+ private const long MaxTimestampSeconds = 253_402_300_799L;
+
internal static DateTimeOffset DecodeTimestamp(Timestamp value)
{
if (value.Nanos % 100 != 0)
throw new ArgumentException("Timestamp is finer than domain tick precision.");
+ // #999 / review K-9: Timestamp.ToDateTimeOffset throws InvalidOperationException for seconds/nanos outside the
+ // representable range — which the gRPC service maps to Unavailable, telling the client to retry or fail over a
+ // request that can never succeed. Range-check first so it is the documented ArgumentException.
+ if (value.Seconds < MinTimestampSeconds || value.Seconds > MaxTimestampSeconds
+ || value.Nanos < 0 || value.Nanos > 999_999_999)
+ throw new ArgumentException("Timestamp is outside the representable range.");
return value.ToDateTimeOffset();
}
diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Protocol/VariantCodec.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Protocol/VariantCodec.cs
index d37ffde44..137817b60 100644
--- a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Protocol/VariantCodec.cs
+++ b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Protocol/VariantCodec.cs
@@ -61,6 +61,21 @@ public static class VariantCodec
/// The boxed value.
/// An unknown tag, or a payload that does not match the tag.
public static object? Decode(Variant? variant)
+ {
+ try
+ {
+ return DecodeCore(variant);
+ }
+ catch (Exception ex) when (ex is OverflowException or ArgumentOutOfRangeException)
+ {
+ // #999 / review K-9: an out-of-range payload (a Byte carrying 300, DatetimeUtc ticks past DateTime.MaxValue)
+ // is a protocol fault like any other contradictory payload. OutOfProcessScriptRunner.Complete catches only
+ // ScriptWorkerProtocolException, so these used to fault the run task of a runner documented "never faults".
+ throw new ScriptWorkerProtocolException($"variant tagged {variant!.Type} is out of range: {ex.Message}", ex);
+ }
+ }
+
+ private static object? DecodeCore(Variant? variant)
{
if (variant is null) return null;
return variant.Type switch
@@ -112,7 +127,7 @@ public static class VariantCodec
var item = array.Items[i];
if (item.Type != array.ElementType && item.Type != Variant.Types.TypeTag.Null)
throw new ScriptWorkerProtocolException($"array of {array.ElementType} holds a {item.Type} element");
- result.SetValue(Decode(item), i);
+ result.SetValue(DecodeCore(item), i);
}
return result;
}
diff --git a/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/DataConnections/GrpcDataCodecTests.cs b/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/DataConnections/GrpcDataCodecTests.cs
index 1ee96c60d..943f8f368 100644
--- a/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/DataConnections/GrpcDataCodecTests.cs
+++ b/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/DataConnections/GrpcDataCodecTests.cs
@@ -514,4 +514,48 @@ public sealed class GrpcDataCodecTests
DataProtoMapper.DecodeBrowseResponse(response, new DataDecodeLimits(1, 10000, 100))
);
}
+
+ ///
+ /// #999 / review K-9: a malformed wire value fails as the codec's documented (the
+ /// gRPC service maps it to InvalidArgument) — never an /
+ /// (unmapped: an internal error) or an
+ /// (mapped to Unavailable: the client retries or fails over a request that can never succeed).
+ ///
+ [Theory]
+ [MemberData(nameof(MalformedValues))]
+ public void Malformed_wire_values_fail_as_ArgumentException(string _, Protos.DataConnection.V1.TypedValue wire)
+ {
+ var ex = Should.Throw(() => TypedValueCodec.Decode(wire));
+ ex.ShouldBeAssignableTo($"{ex.GetType().Name}: {ex.Message}");
+ }
+
+ /// K-9: an out-of-range protobuf timestamp is an ArgumentException at the shared timestamp decoder too.
+ [Fact]
+ public void Out_of_range_timestamps_fail_as_ArgumentException()
+ {
+ var ex = Should.Throw(() => TypedValueCodec.DecodeTimestamp(new Google.Protobuf.WellKnownTypes.Timestamp { Seconds = long.MaxValue }));
+ ex.ShouldBeAssignableTo($"{ex.GetType().Name}: {ex.Message}");
+ }
+
+ public static TheoryData MalformedValues()
+ {
+ var data = new TheoryData
+ {
+ { "Int8 300", new() { Kind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.Int8, Signed = 300 } },
+ { "UInt8 300", new() { Kind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.UInt8, Unsigned = 300 } },
+ { "Int32 overflow", new() { Kind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.Int32, Signed = long.MaxValue } },
+ { "bad guid", new() { Kind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.Guid, Guid = "not-a-guid" } },
+ { "timestamp out of range", new() { Kind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.Timestamp, Timestamp = new Google.Protobuf.WellKnownTypes.Timestamp { Seconds = long.MaxValue } } },
+ { "timestamp bad nanos", new() { Kind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.Timestamp, Timestamp = new Google.Protobuf.WellKnownTypes.Timestamp { Seconds = 0, Nanos = -100 } } },
+ };
+ var array = new Protos.DataConnection.V1.TypedValue
+ {
+ Kind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.Array,
+ Array = new Protos.DataConnection.V1.TypedArray { ElementKind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.Int16 },
+ };
+ array.Array.Dimensions.Add(1);
+ array.Array.Elements.Add(new Protos.DataConnection.V1.TypedValue { Kind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.Int16, Signed = 1 << 20 });
+ data.Add("array element overflow", array);
+ return data;
+ }
}
diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.ScriptWorker.Tests/ProtocolTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.ScriptWorker.Tests/ProtocolTests.cs
index 8887f52c0..dd92a6ec8 100644
--- a/tests/Server/ZB.MOM.WW.OtOpcUa.ScriptWorker.Tests/ProtocolTests.cs
+++ b/tests/Server/ZB.MOM.WW.OtOpcUa.ScriptWorker.Tests/ProtocolTests.cs
@@ -76,7 +76,15 @@ public sealed class ProtocolTests
{
Should.Throw(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.Int16, S = "x" }));
Should.Throw(() => VariantCodec.Decode(new Variant { Type = (Variant.Types.TypeTag)99 }));
- Should.Throw(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.Int16, I = 1 << 20 }));
+ // #999 / review K-9: out-of-range payloads are the codec's documented failure too, never an OverflowException /
+ // ArgumentOutOfRangeException that escapes OutOfProcessScriptRunner.Complete (which catches only this type).
+ Should.Throw(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.Int16, I = 1 << 20 }));
+ Should.Throw(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.Byte, U = 300 }));
+ Should.Throw(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.DatetimeUtc, DtTicks = long.MaxValue }));
+ Should.Throw(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.DatetimeUtc, DtTicks = -1 }));
+ var array = new Variant { Type = Variant.Types.TypeTag.Array, Array = new ArrayValue { ElementType = Variant.Types.TypeTag.Byte } };
+ array.Array.Items.Add(new Variant { Type = Variant.Types.TypeTag.Byte, U = 1000 });
+ Should.Throw(() => VariantCodec.Decode(array));
}
private static Envelope Sample() => new()
@@ -165,7 +173,7 @@ public sealed class ProtocolTests
// A frame that parses must still decode its variants without an unexpected exception type.
if (envelope.BodyCase == Envelope.BodyOneofCase.Results)
foreach (var r in envelope.Results.Items)
- try { VariantCodec.Decode(r.Value); } catch (Exception ex) when (ex is ScriptWorkerProtocolException or OverflowException) { }
+ try { VariantCodec.Decode(r.Value); } catch (ScriptWorkerProtocolException) { }
}
}
catch (ScriptWorkerProtocolException)