Skip to content

Commit

Permalink
Only use system exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
verdie-g committed Jun 25, 2024
1 parent 4915a4d commit d57a978
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 49 deletions.
22 changes: 0 additions & 22 deletions EventPipe/CorruptedBlockException.cs

This file was deleted.

29 changes: 13 additions & 16 deletions EventPipe/EventPipeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -472,19 +472,15 @@ 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);
}

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}");
}
}

Expand Down Expand Up @@ -656,8 +652,7 @@ private static void ReadThreadSequencePoint(ref FastSerializerSequenceReader rea

private static TEnum ReadByteAsEnum<TEnum>(ref FastSerializerSequenceReader reader) where TEnum : Enum
{
CorruptedBlockException.ThrowIfFalse(TryReadByteAsEnum<TEnum>(ref reader, out var value), reader.AbsolutePosition);
return value;
return TryReadByteAsEnum<TEnum>(ref reader, out var value) ? value : throw new EndOfStreamException();
}

private static bool TryReadByteAsEnum<TEnum>(
Expand All @@ -682,8 +677,7 @@ private TEnum ReadInt16AsEnum<TEnum>(ref FastSerializerSequenceReader reader)

private static TEnum ReadInt32AsEnum<TEnum>(ref FastSerializerSequenceReader reader) where TEnum : Enum
{
CorruptedBlockException.ThrowIfFalse(TryReadInt32AsEnum<TEnum>(ref reader, out var value), reader.AbsolutePosition);
return value;
return TryReadInt32AsEnum<TEnum>(ref reader, out var value) ? value : throw new EndOfStreamException();
}

private static bool TryReadInt32AsEnum<TEnum>(
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions EventPipe/FastSerializer/FastSerializerSequenceReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
}
}
8 changes: 0 additions & 8 deletions EventPipe/InvalidNetTraceFileException.cs

This file was deleted.

0 comments on commit d57a978

Please sign in to comment.