From 7ddd2d22c25ae6d68ed4260f1e6b68675c144336 Mon Sep 17 00:00:00 2001 From: Nathan Mytelka Date: Wed, 14 Jul 2021 09:43:21 -0700 Subject: [PATCH 1/3] Move version check earlier --- src/Tasks/StateFileBase.cs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Tasks/StateFileBase.cs b/src/Tasks/StateFileBase.cs index 191d2dfe090..65a92dd69d8 100644 --- a/src/Tasks/StateFileBase.cs +++ b/src/Tasks/StateFileBase.cs @@ -77,8 +77,17 @@ internal static StateFileBase DeserializeCache(string stateFile, TaskLoggingHelp using (FileStream s = File.OpenRead(stateFile)) { var translator = BinaryTranslator.GetReadTranslator(s, buffer: null); + byte version = 0; translator.Translate(ref version); + // If retVal is still null or the version is wrong, log a message not a warning. This could be a valid cache with the wrong version preventing correct deserialization. + // For the latter case, internals may be unexpectedly null. + if (retVal == null || version != CurrentSerializationVersion) + { + log.LogMessageFromResources("General.CouldNotReadStateFileMessage", stateFile, log.FormatResourceString("General.IncompatibleStateFileType")); + return null; + } + var constructors = requiredReturnType.GetConstructors(); foreach (var constructor in constructors) { @@ -88,18 +97,8 @@ internal static StateFileBase DeserializeCache(string stateFile, TaskLoggingHelp retVal = constructor.Invoke(new object[] { translator }) as StateFileBase; } } - - // If retVal is still null or the version is wrong, log a message not a warning. This could be a valid cache with the wrong version preventing correct deserialization. - // For the latter case, internals may be unexpectedly null. - if (retVal == null || version != CurrentSerializationVersion) - { - // When upgrading to Visual Studio 2008 and running the build for the first time the resource cache files are replaced which causes a cast error due - // to a new version number on the tasks class. "Unable to cast object of type 'Microsoft.Build.Tasks.SystemState' to type 'Microsoft.Build.Tasks.StateFileBase". - // If there is an invalid cast, a message rather than a warning should be emitted. - log.LogMessageFromResources("General.CouldNotReadStateFileMessage", stateFile, log.FormatResourceString("General.IncompatibleStateFileType")); - return null; - } - else if (!requiredReturnType.IsInstanceOfType(retVal)) + + if (!requiredReturnType.IsInstanceOfType(retVal)) { log.LogMessageFromResources("General.CouldNotReadStateFileMessage", stateFile, log.FormatResourceString("General.IncompatibleStateFileType")); From aa75790d6d05be45284eb02a54a37cfd48856221 Mon Sep 17 00:00:00 2001 From: Nathan Mytelka Date: Wed, 14 Jul 2021 14:26:29 -0700 Subject: [PATCH 2/3] Move null check --- src/Tasks/StateFileBase.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Tasks/StateFileBase.cs b/src/Tasks/StateFileBase.cs index 65a92dd69d8..06f8c792f53 100644 --- a/src/Tasks/StateFileBase.cs +++ b/src/Tasks/StateFileBase.cs @@ -82,7 +82,7 @@ internal static StateFileBase DeserializeCache(string stateFile, TaskLoggingHelp translator.Translate(ref version); // If retVal is still null or the version is wrong, log a message not a warning. This could be a valid cache with the wrong version preventing correct deserialization. // For the latter case, internals may be unexpectedly null. - if (retVal == null || version != CurrentSerializationVersion) + if (version != CurrentSerializationVersion) { log.LogMessageFromResources("General.CouldNotReadStateFileMessage", stateFile, log.FormatResourceString("General.IncompatibleStateFileType")); return null; @@ -98,7 +98,7 @@ internal static StateFileBase DeserializeCache(string stateFile, TaskLoggingHelp } } - if (!requiredReturnType.IsInstanceOfType(retVal)) + if (retVal == null || !requiredReturnType.IsInstanceOfType(retVal)) { log.LogMessageFromResources("General.CouldNotReadStateFileMessage", stateFile, log.FormatResourceString("General.IncompatibleStateFileType")); From e4d4e0a471f2668ace56b0dbd3dabd281e7a42b5 Mon Sep 17 00:00:00 2001 From: Nathan Mytelka Date: Wed, 14 Jul 2021 14:40:00 -0700 Subject: [PATCH 3/3] Fix comment --- src/Tasks/StateFileBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tasks/StateFileBase.cs b/src/Tasks/StateFileBase.cs index 7325cfc7034..20f98be86e0 100644 --- a/src/Tasks/StateFileBase.cs +++ b/src/Tasks/StateFileBase.cs @@ -80,7 +80,7 @@ internal static StateFileBase DeserializeCache(string stateFile, TaskLoggingHelp byte version = 0; translator.Translate(ref version); - // If retVal is still null or the version is wrong, log a message not a warning. This could be a valid cache with the wrong version preventing correct deserialization. + // If the version is wrong, log a message not a warning. This could be a valid cache with the wrong version preventing correct deserialization. // For the latter case, internals may be unexpectedly null. if (version != CurrentSerializationVersion) {