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

Detect dangerous reference changes in TryDeserialize() #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
19 changes: 17 additions & 2 deletions Assets/FullSerializer/Source/fsSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,22 @@ private fsResult InternalDeserialize_5_Converter(Type overrideConverterType, fsD
data = data.AsDictionary[Key_Content];
}

return GetConverter(resultType, overrideConverterType).TryDeserialize(data, ref result, resultType);
var converter = GetConverter(resultType, overrideConverterType);
if (converter.RequestCycleSupport(resultType)) {
object startResult = result; // This would cause a copy for value types, so only do it if RequestCycleSupport() cause then we know it's a reference type
var deserializeResult = converter.TryDeserialize(data, ref result, resultType);
if (!ReferenceEquals(startResult, result)) {
// If you get here, don't do this in TryDeserialize():
// instance = new Thing();
// Rather do something like this:
// ((Thing)instance).Update(newData);
return fsResult.Fail("TryDeserialize() for " + resultType + " has changed instance reference after CreateInstance(). You must update the instance without changing the reference, or cyclic references will break!");
}
return deserializeResult;

} else {
return converter.TryDeserialize(data, ref result, resultType);
}
}
}
}
}