Use ReferenceEquals to avoid infinite recursion

Fix InlineImageDataToken not checking Data length before comparing bytes
Use a more straightforward compare Dictionary Content
This commit is contained in:
InusualZ
2020-03-14 09:35:49 -04:00
committed by Eliot Jones
parent 26f92a9630
commit 02289d75ac
13 changed files with 72 additions and 50 deletions

View File

@@ -92,19 +92,27 @@
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(IToken obj) public bool Equals(IToken obj)
{ {
if (this == obj) if (ReferenceEquals(this, obj))
{
return true; return true;
}
if (!(obj is ArrayToken other)) if (!(obj is ArrayToken other))
{
return false; return false;
}
if (other.Length != Length) if (other.Length != Length)
{
return false; return false;
}
for (var index = 0; index < Length; ++index) for (var index = 0; index < Length; ++index)
{ {
if (!Data[index].Equals(other[index])) if (!Data[index].Equals(other[index]))
{
return false; return false;
}
} }
return true; return true;

View File

@@ -44,8 +44,10 @@
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(IToken obj) public bool Equals(IToken obj)
{ {
if (this == obj) if (ReferenceEquals(this, obj))
{
return true; return true;
}
if (!(obj is BooleanToken other)) if (!(obj is BooleanToken other))
{ {

View File

@@ -29,6 +29,11 @@
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(IToken obj) public bool Equals(IToken obj)
{ {
if (ReferenceEquals(this, obj))
{
return true;
}
if (!(obj is CommentToken other)) if (!(obj is CommentToken other))
{ {
return false; return false;

View File

@@ -118,8 +118,10 @@
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(IToken obj) public bool Equals(IToken obj)
{ {
if (this == obj) if (ReferenceEquals(this, obj))
{
return true; return true;
}
if (!(obj is DictionaryToken other)) if (!(obj is DictionaryToken other))
{ {
@@ -127,11 +129,19 @@
} }
if (Data.Count != other.Data.Count) if (Data.Count != other.Data.Count)
{
return false; return false;
}
// TODO: Maybe consider using a sorted dictionary? foreach (var kvp in other.Data)
return Data.OrderBy(kvp => kvp.Key) {
.SequenceEqual(other.Data.OrderBy(kvp => kvp.Key)); if (!Data.TryGetValue(kvp.Key, out var val) || !val.Equals(kvp.Value))
{
return false;
}
}
return true;
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@@ -17,15 +17,12 @@
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(IToken obj) public bool Equals(IToken obj)
{ {
if (this == obj) if (ReferenceEquals(this, obj))
return true;
if (!(obj is EndOfLineToken other))
{ {
return false; return true;
} }
return true; return obj is EndOfLineToken;
} }
} }
} }

View File

@@ -141,8 +141,10 @@ namespace UglyToad.PdfPig.Tokens
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(IToken obj) public bool Equals(IToken obj)
{ {
if (this == obj) if (ReferenceEquals(this, obj))
{
return true; return true;
}
if (!(obj is HexToken other)) if (!(obj is HexToken other))
{ {

View File

@@ -24,8 +24,10 @@
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(IToken obj) public bool Equals(IToken obj)
{ {
if (this == obj) if (ReferenceEquals(this, obj))
{
return true; return true;
}
if (!(obj is IndirectReferenceToken other)) if (!(obj is IndirectReferenceToken other))
{ {

View File

@@ -22,8 +22,10 @@
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(IToken obj) public bool Equals(IToken obj)
{ {
if (this == obj) if (ReferenceEquals(this, obj))
{
return true; return true;
}
if (!(obj is InlineImageDataToken other)) if (!(obj is InlineImageDataToken other))
{ {
@@ -31,14 +33,17 @@
} }
if (Data.Count != other.Data.Count) if (Data.Count != other.Data.Count)
{
return false; return false;
}
// Note: This maybe slow?
// Maybe pre calculate some sort of hash and compare that?
for (var index = 0; index < Data.Count; ++index) for (var index = 0; index < Data.Count; ++index)
{ {
if (Data[index] != other.Data[index]) if (Data[index] != other.Data[index])
{
return false; return false;
}
} }
return true; return true;

View File

@@ -109,8 +109,10 @@
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(IToken obj) public bool Equals(IToken obj)
{ {
if (this == obj) if (ReferenceEquals(this, obj))
{
return true; return true;
}
if (!(obj is NumericToken other)) if (!(obj is NumericToken other))
{ {

View File

@@ -40,34 +40,17 @@
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(IToken obj) public bool Equals(IToken obj)
{ {
if (this == obj) if (ReferenceEquals(this, obj))
{
return true; return true;
}
if (!(obj is ObjectToken other)) if (!(obj is ObjectToken other))
{ {
return false; return false;
} }
// I don't think we should compare this value... return Number.Equals(other.Number) && Data.Equals(other.Data);
// If this values are the same but this object are not the same (reference)
// It means that one of the obj is not from stream or one of the object have been overwritten
// So maybe putting an assert instead, would be a good idea?
if (Position != other.Position)
{
return false;
}
if (!Number.Equals(other.Number))
{
return false;
}
if (!Data.Equals(other.Data))
{
return false;
}
return true;
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@@ -198,8 +198,10 @@
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(IToken obj) public bool Equals(IToken obj)
{ {
if (this == obj) if (ReferenceEquals(this, obj))
{
return true; return true;
}
if (!(obj is OperatorToken other)) if (!(obj is OperatorToken other))
{ {

View File

@@ -33,8 +33,10 @@
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(IToken obj) public bool Equals(IToken obj)
{ {
if (this == obj) if (ReferenceEquals(this, obj))
{
return true; return true;
}
if (!(obj is StreamToken other)) if (!(obj is StreamToken other))
{ {
@@ -46,12 +48,17 @@
return false; return false;
} }
// Note: This maybe slow? if (Data.Count != other.Data.Count)
// Maybe pre calculate some sort of hash and compare that? {
return false;
}
for (var index = 0; index < Data.Count; ++index) for (var index = 0; index < Data.Count; ++index)
{ {
if (Data[index] != other.Data[index]) if (Data[index] != other.Data[index])
{
return false; return false;
}
} }
return true; return true;

View File

@@ -61,20 +61,17 @@ namespace UglyToad.PdfPig.Tokens
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(IToken obj) public bool Equals(IToken obj)
{ {
if (this == obj) if (ReferenceEquals(this, obj))
{
return true; return true;
}
if (!(obj is StringToken other)) if (!(obj is StringToken other))
{ {
return false; return false;
} }
if (!EncodedWith.Equals(other.EncodedWith)) return EncodedWith.Equals(other.EncodedWith) && Data.Equals(other.Data);
{
return false;
}
return Data.Equals(other.Data);
} }
/// <inheritdoc /> /// <inheritdoc />