Skip to content

Commit

Permalink
Use CborReaderState.FormatError in Peek() instead of throwing excepti…
Browse files Browse the repository at this point in the history
…ons.
  • Loading branch information
eiriktsarpalis committed Mar 23, 2020
1 parent c77f4ad commit 77717cc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public static void ReadMap_IndefiniteLength_OddKeyValuePairs_ShouldThrowFormatEx
reader.ReadInt64();
}

Assert.Equal(CborReaderState.EndMap, reader.Peek()); // don't want this to fail
Assert.Equal(CborReaderState.FormatError, reader.Peek()); // don't want this to fail
Assert.Throws<FormatException>(() => reader.ReadEndMap());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal enum CborReaderState
Tag,
Special,
Finished,
FormatError,
EndOfData,
}

Expand Down Expand Up @@ -57,6 +58,7 @@ public CborReaderState Peek()
return _nestedDataItemStack.Peek().type switch
{
CborMajorType.Array => CborReaderState.EndArray,
CborMajorType.Map when !_isEvenNumberOfDataItemsWritten => CborReaderState.FormatError,

This comment has been minimized.

Copy link
@bartonjs

bartonjs Mar 23, 2020

Member

How would the definite length reader hit this state? It seems like unreachable code.

This comment has been minimized.

Copy link
@eiriktsarpalis

eiriktsarpalis Mar 23, 2020

Author Member

Yep, removing

CborMajorType.Map => CborReaderState.EndMap,
_ => throw new Exception("CborReader internal error. Invalid CBOR major type pushed to stack."),
};
Expand All @@ -83,13 +85,14 @@ public CborReaderState Peek()
CborMajorType.ByteString => CborReaderState.EndByteString,
CborMajorType.TextString => CborReaderState.EndTextString,
CborMajorType.Array => CborReaderState.EndArray,
CborMajorType.Map when !_isEvenNumberOfDataItemsWritten => CborReaderState.FormatError,

This comment has been minimized.

Copy link
@bartonjs

bartonjs Mar 23, 2020

Member

It occurs to me that Written should be Read here :)

This comment has been minimized.

Copy link
@eiriktsarpalis

eiriktsarpalis Mar 23, 2020

Author Member

Indeed it should

CborMajorType.Map => CborReaderState.EndMap,
_ => throw new Exception("CborReader internal error. Invalid CBOR major type pushed to stack."),
};
}
else
{
throw new FormatException("Unexpected CBOR break byte.");
return CborReaderState.FormatError;
}
}

Expand All @@ -105,7 +108,7 @@ public CborReaderState Peek()
CborMajorType.Map => CborReaderState.StartMap,
CborMajorType.Tag => CborReaderState.Tag,
CborMajorType.Special => CborReaderState.Special,
_ => throw new FormatException("Invalid CBOR major type"),
_ => CborReaderState.FormatError,
};
}

Expand Down

0 comments on commit 77717cc

Please sign in to comment.