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 />
public bool Equals(IToken obj)
{
if (this == obj)
if (ReferenceEquals(this, obj))
{
return true;
}
if (!(obj is ArrayToken other))
{
return false;
}
if (other.Length != Length)
{
return false;
}
for (var index = 0; index < Length; ++index)
{
if (!Data[index].Equals(other[index]))
{
return false;
}
}
return true;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -40,34 +40,17 @@
/// <inheritdoc />
public bool Equals(IToken obj)
{
if (this == obj)
if (ReferenceEquals(this, obj))
{
return true;
}
if (!(obj is ObjectToken other))
{
return false;
}
// I don't think we should compare this value...
// 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;
return Number.Equals(other.Number) && Data.Equals(other.Data);
}
/// <inheritdoc />

View File

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

View File

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

View File

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