Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 9d286cc

Browse files
morganbrkrwq
authored andcommitted
Throw PlatformNotSupportedExceptions from IDeserializationCallback on types that are no longer serializable. (#20384)
1 parent e35726d commit 9d286cc

File tree

5 files changed

+12
-127
lines changed

5 files changed

+12
-127
lines changed

src/System.Collections/src/System/Collections/Generic/LinkedList.cs

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,6 @@ public struct Enumerator : IEnumerator<T>, IEnumerator, ISerializable, IDeserial
544544
private int _version;
545545
private T _current;
546546
private int _index;
547-
private SerializationInfo _siInfo; //A temporary variable which we need during deserialization.
548547

549548
const string LinkedListName = "LinkedList";
550549
const string CurrentValueName = "Current";
@@ -558,7 +557,6 @@ internal Enumerator(LinkedList<T> list)
558557
_node = list.head;
559558
_current = default(T);
560559
_index = 0;
561-
_siInfo = null;
562560
}
563561

564562
private Enumerator(SerializationInfo info, StreamingContext context)
@@ -630,51 +628,7 @@ void ISerializable.GetObjectData(SerializationInfo info, StreamingContext contex
630628

631629
void IDeserializationCallback.OnDeserialization(Object sender)
632630
{
633-
if (_list != null)
634-
{
635-
return; // Somebody had a dependency on this Dictionary and fixed us up before the ObjectManager got to it.
636-
}
637-
638-
if (_siInfo == null)
639-
{
640-
throw new SerializationException(SR.Serialization_InvalidOnDeser);
641-
}
642-
643-
_list = (LinkedList<T>)_siInfo.GetValue(LinkedListName, typeof(LinkedList<T>));
644-
_version = _siInfo.GetInt32(VersionName);
645-
_current = (T)_siInfo.GetValue(CurrentValueName, typeof(T));
646-
_index = _siInfo.GetInt32(IndexName);
647-
648-
if (_list._siInfo != null)
649-
{
650-
_list.OnDeserialization(sender);
651-
}
652-
653-
if (_index == _list.Count + 1)
654-
{
655-
// end of enumeration
656-
_node = null;
657-
}
658-
else
659-
{
660-
_node = _list.First;
661-
662-
// We don't care if we can point to the correct node if the LinkedList was changed
663-
// MoveNext will throw upon next call and Current has the correct value.
664-
if (_node != null && _index != 0)
665-
{
666-
for (int i = 0; i < _index; i++)
667-
{
668-
_node = _node.next;
669-
}
670-
if (_node == _list.First)
671-
{
672-
_node = null;
673-
}
674-
}
675-
}
676-
677-
_siInfo = null;
631+
throw new PlatformNotSupportedException();
678632
}
679633
}
680634
}

src/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -348,53 +348,10 @@ protected override void GetObjectData(SerializationInfo info, StreamingContext c
348348

349349
void IDeserializationCallback.OnDeserialization(Object sender)
350350
{
351-
// Don't do anything here as the work has already been done by the constructor
351+
throw new PlatformNotSupportedException();
352352
}
353353

354-
protected override void OnDeserialization(Object sender) => OnDeserializationImpl(sender);
355-
356-
private void OnDeserializationImpl(Object sender)
357-
{
358-
if (_siInfo == null)
359-
{
360-
throw new SerializationException(SR.Serialization_InvalidOnDeser);
361-
}
362-
363-
_comparer = (IComparer<T>)_siInfo.GetValue(ComparerName, typeof(IComparer<T>));
364-
int savedCount = _siInfo.GetInt32(CountName);
365-
_max = (T)_siInfo.GetValue(MaxName, typeof(T));
366-
_min = (T)_siInfo.GetValue(MinName, typeof(T));
367-
_lBoundActive = _siInfo.GetBoolean(LowerBoundActiveName);
368-
_uBoundActive = _siInfo.GetBoolean(UpperBoundActiveName);
369-
_underlying = new SortedSet<T>();
370-
371-
if (savedCount != 0)
372-
{
373-
T[] items = (T[])_siInfo.GetValue(ItemsName, typeof(T[]));
374-
375-
if (items == null)
376-
{
377-
throw new SerializationException(SR.Serialization_MissingValues);
378-
}
379-
380-
for (int i = 0; i < items.Length; i++)
381-
{
382-
_underlying.Add(items[i]);
383-
}
384-
}
385-
386-
_underlying._version = _siInfo.GetInt32(VersionName);
387-
_count = _underlying._count;
388-
_version = _underlying._version - 1;
389-
VersionCheck(); // this should update the count to be right and update root to be right
390-
391-
if (_count != savedCount)
392-
{
393-
throw new SerializationException(SR.Serialization_MismatchedCount);
394-
}
395-
396-
_siInfo = null;
397-
}
354+
protected override void OnDeserialization(Object sender) => throw new PlatformNotSupportedException();
398355
}
399356
}
400357
}

src/System.Collections/src/System/Collections/Generic/SortedSet.cs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,7 +1861,6 @@ public struct Enumerator : IEnumerator<T>, IEnumerator, ISerializable, IDeserial
18611861
private Node _current;
18621862

18631863
private bool _reverse;
1864-
private SerializationInfo _siInfo;
18651864

18661865
internal Enumerator(SortedSet<T> set)
18671866
{
@@ -1875,8 +1874,6 @@ internal Enumerator(SortedSet<T> set)
18751874
_current = null;
18761875
_reverse = false;
18771876

1878-
_siInfo = null;
1879-
18801877
Initialize();
18811878
}
18821879

@@ -1891,8 +1888,6 @@ internal Enumerator(SortedSet<T> set, bool reverse)
18911888
_current = null;
18921889
_reverse = reverse;
18931890

1894-
_siInfo = null;
1895-
18961891
Initialize();
18971892
}
18981893

@@ -1903,34 +1898,7 @@ void ISerializable.GetObjectData(SerializationInfo info, StreamingContext contex
19031898

19041899
void IDeserializationCallback.OnDeserialization(Object sender)
19051900
{
1906-
OnDeserialization(sender);
1907-
}
1908-
1909-
private void OnDeserialization(Object sender)
1910-
{
1911-
if (_siInfo == null)
1912-
{
1913-
throw new SerializationException(SR.Serialization_InvalidOnDeser);
1914-
}
1915-
1916-
_tree = (SortedSet<T>)_siInfo.GetValue(TreeName, typeof(SortedSet<T>));
1917-
_version = _siInfo.GetInt32(EnumVersionName);
1918-
_reverse = _siInfo.GetBoolean(ReverseName);
1919-
bool EnumStarted = _siInfo.GetBoolean(EnumStartName);
1920-
_stack = new Stack<Node>(2 * (int)Log2(_tree.Count + 1));
1921-
_current = null;
1922-
if (EnumStarted)
1923-
{
1924-
T item = (T)_siInfo.GetValue(NodeValueName, typeof(T));
1925-
Initialize();
1926-
1927-
// go until it reaches the value we want
1928-
while (this.MoveNext())
1929-
{
1930-
if (_tree.Comparer.Compare(Current, item) == 0)
1931-
break;
1932-
}
1933-
}
1901+
throw new PlatformNotSupportedException();
19341902
}
19351903

19361904
private void Initialize()

src/System.Security.Cryptography.X509Certificates/src/System/Security/Cryptography/X509Certificates/X509Certificate.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ void ISerializable.GetObjectData(SerializationInfo info, StreamingContext contex
182182
throw new PlatformNotSupportedException();
183183
}
184184

185-
void IDeserializationCallback.OnDeserialization(object sender) { }
185+
void IDeserializationCallback.OnDeserialization(object sender)
186+
{
187+
throw new PlatformNotSupportedException();
188+
}
186189

187190
public IntPtr Handle
188191
{

src/System.Security.Principal.Windows/src/System/Security/Principal/WindowsIdentity.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,10 @@ void ISerializable.GetObjectData(SerializationInfo info, StreamingContext contex
247247
throw new PlatformNotSupportedException();
248248
}
249249

250-
void IDeserializationCallback.OnDeserialization(object sender) { }
250+
void IDeserializationCallback.OnDeserialization(object sender)
251+
{
252+
throw new PlatformNotSupportedException();
253+
}
251254

252255
//
253256
// Factory methods.

0 commit comments

Comments
 (0)