diff --git a/EventPipe/CorruptedBlockException.cs b/EventPipe/CorruptedBlockException.cs deleted file mode 100644 index aa2782f..0000000 --- a/EventPipe/CorruptedBlockException.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Diagnostics.CodeAnalysis; - -namespace EventPipe; - -public class CorruptedBlockException : Exception -{ - internal static void ThrowIfFalse([DoesNotReturnIf(false)] bool b, long position) - { - if (!b) - { - throw new CorruptedBlockException("Unexpected end of block at position", position); - } - } - - internal CorruptedBlockException(string message, long position) - : base(message + " at position " + position) - { - Position = position; - } - - public long Position { get; } -} \ No newline at end of file diff --git a/EventPipe/EventPipeReader.cs b/EventPipe/EventPipeReader.cs index cfbff3d..94572c3 100644 --- a/EventPipe/EventPipeReader.cs +++ b/EventPipe/EventPipeReader.cs @@ -130,7 +130,7 @@ private static bool TryReadHeader(ref FastSerializerSequenceReader reader) if (!magicBytes.SequenceEqual(MagicBytes)) { - throw new InvalidNetTraceFileException("Stream is not in the event pipe format"); + throw new InvalidDataException("Stream is not in the event pipe format"); } if (!reader.TryReadString(out var serializerSignatureSeq)) @@ -143,7 +143,7 @@ private static bool TryReadHeader(ref FastSerializerSequenceReader reader) if (!serializerSignature.SequenceEqual(SerializerSignature)) { - throw new InvalidNetTraceFileException("Unexpected serializer signature"); + throw new InvalidDataException("Unexpected serializer signature"); } return true; @@ -262,7 +262,7 @@ private bool TryReadBlock(ref FastSerializerSequenceReader reader, in Serializat if (reader.AbsolutePosition != blockEndPosition) { - throw new CorruptedBlockException($"{serializationType.Name} end was not reached", reader.AbsolutePosition); + throw new InvalidDataException($"{serializationType.Name} end was not reached at position {reader.AbsolutePosition}"); } return true; @@ -425,7 +425,7 @@ private void ReadCompressedEventBlob(ref FastSerializerSequenceReader reader, if (reader.AbsolutePosition != payloadEndPosition) { - throw new CorruptedBlockException("Event blob payload end was not reached", reader.AbsolutePosition); + throw new InvalidDataException($"Event blob payload end was not reached at position {reader.AbsolutePosition}"); } state.PreviousMetadataId = metadataId; @@ -472,9 +472,7 @@ private EventMetadata ReadEventMetadata( { if (fieldDefinitions.Count == 0) { - throw new CorruptedBlockException( - "No V2 field definitions are expected after V1 field definitions", - reader.AbsolutePosition); + throw new InvalidDataException($"No V2 field definitions are expected after V1 field definitions at position {reader.AbsolutePosition}"); } fieldDefinitions = ReadFieldDefinitions(ref reader, EventFieldDefinitionVersion.V2); @@ -482,9 +480,7 @@ private EventMetadata ReadEventMetadata( if (reader.AbsolutePosition != tagPayloadEndPosition) { - throw new CorruptedBlockException( - "Event metadata tag end was not reached", - reader.AbsolutePosition); + throw new EndOfStreamException($"Event metadata tag end was not reached at position {reader.AbsolutePosition}"); } } @@ -656,8 +652,7 @@ private static void ReadThreadSequencePoint(ref FastSerializerSequenceReader rea private static TEnum ReadByteAsEnum(ref FastSerializerSequenceReader reader) where TEnum : Enum { - CorruptedBlockException.ThrowIfFalse(TryReadByteAsEnum(ref reader, out var value), reader.AbsolutePosition); - return value; + return TryReadByteAsEnum(ref reader, out var value) ? value : throw new EndOfStreamException(); } private static bool TryReadByteAsEnum( @@ -682,8 +677,7 @@ private TEnum ReadInt16AsEnum(ref FastSerializerSequenceReader reader) private static TEnum ReadInt32AsEnum(ref FastSerializerSequenceReader reader) where TEnum : Enum { - CorruptedBlockException.ThrowIfFalse(TryReadInt32AsEnum(ref reader, out var value), reader.AbsolutePosition); - return value; + return TryReadInt32AsEnum(ref reader, out var value) ? value : throw new EndOfStreamException(); } private static bool TryReadInt32AsEnum( @@ -715,13 +709,16 @@ private static void AssertTag(FastSerializerTag expectedTag, FastSerializerTag a { if (expectedTag != actualTag) { - throw new Exception($"Expected tag '{expectedTag}' but got '{actualTag}'"); + throw new InvalidDataException($"Expected tag '{expectedTag}' but got '{actualTag}'"); } } private static void ReadPadding(ref FastSerializerSequenceReader reader) { - CorruptedBlockException.ThrowIfFalse(TryReadPadding(ref reader), reader.AbsolutePosition); + if (!TryReadPadding(ref reader)) + { + throw new EndOfStreamException(); + } } private static bool TryReadPadding(ref FastSerializerSequenceReader reader) diff --git a/EventPipe/FastSerializer/FastSerializerSequenceReader.cs b/EventPipe/FastSerializer/FastSerializerSequenceReader.cs index 5ff9d68..2def219 100644 --- a/EventPipe/FastSerializer/FastSerializerSequenceReader.cs +++ b/EventPipe/FastSerializer/FastSerializerSequenceReader.cs @@ -265,7 +265,7 @@ public bool TryReadVarInt32(out int value) if (b > 0b_1111u) { - throw new FormatException(); + throw new InvalidDataException(); } res |= (uint)b << (maxBytesWithoutOverflow * 7); @@ -308,7 +308,7 @@ public bool TryReadVarInt64(out long value) if (b > 0b_1u) { - throw new FormatException(); + throw new InvalidDataException(); } res |= (ulong)b << (maxBytesWithoutOverflow * 7); @@ -318,6 +318,9 @@ public bool TryReadVarInt64(out long value) private void ThrowIfFalse([DoesNotReturnIf(false)] bool b) { - CorruptedBlockException.ThrowIfFalse(b, AbsolutePosition); + if (!b) + { + throw new EndOfStreamException(); + } } } \ No newline at end of file diff --git a/EventPipe/InvalidNetTraceFileException.cs b/EventPipe/InvalidNetTraceFileException.cs deleted file mode 100644 index 4245dc6..0000000 --- a/EventPipe/InvalidNetTraceFileException.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace EventPipe; - -public class InvalidNetTraceFileException : Exception -{ - internal InvalidNetTraceFileException(string message) : base(message) - { - } -} \ No newline at end of file