Skip to content

Commit

Permalink
Avoid nullref while trying to throw a more helpful exception. (#55316)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenMolloy committed Jul 15, 2021
1 parent 0dcf196 commit 0663b30
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 @@ -1167,4 +1167,7 @@
<data name="ReadOnlyCollectionDeserialization" xml:space="preserve">
<value>Collection type '{0}' cannot be deserialized since it</value>
</data>
<data name="UnknownNullType" xml:space="preserve">
<value>Unknown Type for null value</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,9 @@ public void ReplaceDeserializedObject(string id, object? oldObj, object? newObj)
// throw in such cases to allow us add fix-up support in the future if we need to.
if (DeserializedObjects.IsObjectReferenced(id))
{
// https://github.com/dotnet/runtime/issues/41465 - oldObj or newObj may be null below - suppress compiler error by asserting non-null
Debug.Assert(oldObj != null && newObj != null);
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.FactoryObjectContainsSelfReference, DataContract.GetClrTypeFullName(oldObj.GetType()), DataContract.GetClrTypeFullName(newObj.GetType()), id)));
string oldType = (oldObj != null) ? DataContract.GetClrTypeFullName(oldObj.GetType()) : SR.UnknownNullType;
string newType = (newObj != null) ? DataContract.GetClrTypeFullName(newObj.GetType()) : SR.UnknownNullType;
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.FactoryObjectContainsSelfReference, oldType, newType, id)));
}
DeserializedObjects.Remove(id);
DeserializedObjects.Add(id, newObj);
Expand Down

0 comments on commit 0663b30

Please sign in to comment.