Skip to content

Commit

Permalink
Fix: Cloning objects with children with missing Converters could resu…
Browse files Browse the repository at this point in the history
…lt in an unhandled exception.
  • Loading branch information
zorbathut committed Dec 25, 2024
1 parent 70ec2b8 commit 3744f10
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.

### Fixed
* Reading recorder files with objects with a missing Converter could result in an unhandled exception.
* Cloning objects with children with missing Converters could result in an unhandled exception.


## [v0.8.0]
Expand Down
12 changes: 10 additions & 2 deletions src/Recorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,16 @@ internal override void Record<T>(ref T value, string label, Parameters parameter

seen?.Add(label);

// Explicit cast here because we want an error if we have the wrong type!
value = (T)recorded.ParseElement(typeof(T), value, readerGlobals, parameters.CreateSettings());
// Avoid an explicit cast because that can cause null reference errors
var result = recorded.ParseElement(typeof(T), value, readerGlobals, parameters.CreateSettings());
if (result != null)
{
value = (T)result;
}
else
{
value = default;
}
}

public override void Ignore(string label)
Expand Down
18 changes: 18 additions & 0 deletions test/unit/Misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public void Record(Dec.Recorder recorder)
}
}

struct TestStructMember : Dec.IRecordable
{
public TestStruct value;

public void Record(Dec.Recorder recorder)
{
recorder.Record(ref value, nameof(value));
}
}

[Test]
public void UnavailableStructRecorderNormal()
{
Expand Down Expand Up @@ -52,5 +62,13 @@ public void UnavailableStructRecorderAsThis()

ExpectErrors(() => Dec.Recorder.Read<TestStructAsThis>(recorded));
}

[Test]
public void UnavailableStructRecorderClone()
{
var initial = new TestStructMember();

ExpectErrors(() => Dec.Recorder.Clone(initial));
}
}
}

0 comments on commit 3744f10

Please sign in to comment.