Bug 6017 - BinaryFormatter.Deserialize fails for DateTime value type when serialized on Windows platform
Summary: BinaryFormatter.Deserialize fails for DateTime value type when serialized on ...
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 5.3.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-07-06 14:08 UTC by Vladimir Krougly
Modified: 2016-02-23 23:32 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Vladimir Krougly 2012-07-06 14:08:28 UTC
Windows works slightly different when DateTime values are being serialized. This causes a crash once the value is deserialized in Monotouch.

As an example, below code will yield a different result:
   DateTime tm = new DateTime(2012, 1, 1, 0, 0, 0, 0, DateTimeKind.Local);
   using (var memoryStream = new MemoryStream())
   {
       new BinaryFormatter().Serialize(memoryStream, tm);
       byte[] data = memoryStream.ToArray();
   }

On Windows the "data" array is:

00 01 00 00 00 ff ff ff ff 01 00 00 00 00 00 00 00 04 01 00 00 00 0f 53 79 73 74 65 6d 2e 44 61 74 65 54 69 6d 65 02 00 00 00 05 74 69 63 6b 73 08 64 61 74 65 44 61 74 61 00 00 09 09 00 80 72 70 2f 96 ce 08 00 80 72 70 2f 96 ce 88 0b

On iOS the "data" array is:
00 01 00 00 00 ff ff ff ff 01 00 00 00 00 00 00 00 04 01 00 00 00 0f 53 79 73 74 65 6d 2e 44 61 74 65 54 69 6d 65 02 00 00 00 05 74 69 63 6b 73 08 64 61 74 65 44 61 74 61 00 00 09 10 00 80 72 70 2f 96 ce 08 00 80 72 70 2f 96 ce 88 0b

The difference is in data[60].
 On Windows: data[60] = 0x10
 On iOS: data[60] = 0x09

This one byte discrepancy is causing a crash with TargetInvocationException being thrown in Monotouch.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2016-02-17 17:49:43 UTC
This is still happening.

This code:

	DateTime tm = new DateTime(2012, 1, 1, 0, 0, 0, 0, DateTimeKind.Local);
	using (var memoryStream = new MemoryStream())
	{
		new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Serialize(memoryStream, tm);
		byte[] data = memoryStream.ToArray();
		for (int i = 0; i < data.Length; i++)
			Console.Write ("{0} ", data [i].ToString ("x2"));
		Console.WriteLine ();
	}

prints:

00 01 00 00 00 ff ff ff ff 01 00 00 00 00 00 00 00 04 01 00 00 00 0f 53 79 73 74 65 6d 2e 44 61 74 65 54 69 6d 65 02 00 00 00 05 74 69 63 6b 73 08 64 61 74 65 44 61 74 61 00 00 09 10 00 80 72 70 2f 96 ce 08 00 80 72 70 2f 96 ce 88 0b
Comment 2 Marek Safar 2016-02-23 23:32:18 UTC
My latest .net version prints same values including "10" at position 60.