Skip to content

Commit

Permalink
BinaryFormatter bug fix (#104896)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik authored Jul 18, 2024
1 parent e9ffa8c commit bec5970
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ private void WriteMembers(NameInfo memberNameInfo,
return;
}

if (!WriteKnownValueClass(memberNameInfo, memberTypeNameInfo, memberData!))
if (!WriteKnownValueClass(memberNameInfo, memberTypeNameInfo, memberData!, ref assignUniqueIdToValueType))
{
if (outType == null)
{
Expand Down Expand Up @@ -616,10 +616,10 @@ private void WriteArrayMember(WriteObjectInfo objectInfo, NameInfo arrayElemType
actualTypeInfo._isArrayItem = true;
}

if (!WriteKnownValueClass(arrayElemTypeNameInfo, actualTypeInfo, data!))
bool assignUniqueIdForValueTypes = false;
if (!WriteKnownValueClass(arrayElemTypeNameInfo, actualTypeInfo, data!, ref assignUniqueIdForValueTypes))
{
object obj = data!;
bool assignUniqueIdForValueTypes = false;
if (ReferenceEquals(arrayElemTypeNameInfo._type, Converter.s_typeofObject))
{
assignUniqueIdForValueTypes = true;
Expand Down Expand Up @@ -799,11 +799,12 @@ private long Schedule(object obj, bool assignUniqueIdToValueType, Type? type, Wr
}

// Determines if a type is a primitive type, if it is it is written
private bool WriteKnownValueClass(NameInfo memberNameInfo, NameInfo typeNameInfo, object data)
private bool WriteKnownValueClass(NameInfo memberNameInfo, NameInfo typeNameInfo, object data, ref bool assignUniqueIdToValueType)
{
if (ReferenceEquals(typeNameInfo._type, Converter.s_typeofString))
{
WriteString(memberNameInfo, typeNameInfo, data);
return true;
}
else
{
Expand All @@ -817,18 +818,22 @@ private bool WriteKnownValueClass(NameInfo memberNameInfo, NameInfo typeNameInfo
if (typeNameInfo._isArray) // null if an array
{
_serWriter.WriteItem(memberNameInfo, typeNameInfo, data);
return true;
}
else
else if (memberNameInfo._type == typeNameInfo._type
|| memberNameInfo._type == typeof(object)
|| (memberNameInfo._type != null && Nullable.GetUnderlyingType(memberNameInfo._type) != null))
{
_serWriter.WriteMember(memberNameInfo, typeNameInfo, data);
return true;
}
}
}

return true;
assignUniqueIdToValueType = true;
return false;
}


// Writes an object reference to the stream.
private void WriteObjectRef(NameInfo nameInfo, long objectId) =>
_serWriter!.WriteMemberObjectRef(nameInfo, (int)objectId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,22 @@ public void Roundtrip_ArrayContainingArrayAtNonZeroLowerBound()
BinaryFormatterHelpers.Clone(Array.CreateInstance(typeof(uint[]), new[] { 5 }, new[] { 1 }));
}

[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Unpatched versions of .NET Framework would throw.")]
public void AssignUniqueIdToValueType()
{
Tuple<IComparable, object> tuple = new Tuple<IComparable, object>(42, new byte[] { 1, 2, 3, 4 });
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream stream = new MemoryStream())
{
formatter.Serialize(stream, tuple);
stream.Position = 0;
Tuple<IComparable, object> deserialized = (Tuple<IComparable, object>)formatter.Deserialize(stream);
Assert.Equal(tuple.Item1, deserialized.Item1);
Assert.Equal(tuple.Item2, deserialized.Item2);
}
}

private static void ValidateEqualityComparer(object obj)
{
Type objType = obj.GetType();
Expand Down

0 comments on commit bec5970

Please sign in to comment.