Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid nullref while trying to throw a more helpful exception. #55316

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)));
var oldType = (oldObj != null) ? DataContract.GetClrTypeFullName(oldObj.GetType()) : "unknown type";
var newType = (newObj != null) ? DataContract.GetClrTypeFullName(newObj.GetType()) : "unknown type";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
var oldType = (oldObj != null) ? DataContract.GetClrTypeFullName(oldObj.GetType()) : "unknown type";
var newType = (newObj != null) ? DataContract.GetClrTypeFullName(newObj.GetType()) : "unknown type";
string oldType = (oldObj != null) ? DataContract.GetClrTypeFullName(oldObj.GetType()) : "unknown type";
string newType = (newObj != null) ? DataContract.GetClrTypeFullName(newObj.GetType()) : "unknown type";

Also, does "unknown type" need to come from a resx?

throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.FactoryObjectContainsSelfReference, oldType, newType, id)));
}
DeserializedObjects.Remove(id);
DeserializedObjects.Add(id, newObj);
Expand Down