diff --git a/src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs b/src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs index 168cca02bbe..7993b10a9ae 100644 --- a/src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs +++ b/src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs @@ -27,6 +27,26 @@ public BuildEventArgsSerializationTests() _ = ItemGroupLoggingHelper.ItemGroupIncludeLogMessagePrefix; } + [Fact] + public void WriteBlobFromStream() + { + byte[] bytes = new byte[] { 1, 2, 3, 4, 5 }; + MemoryStream inputStream = new MemoryStream(bytes); + + MemoryStream outputStream = new MemoryStream(); + using BinaryWriter binaryWriter = new BinaryWriter(outputStream); + BuildEventArgsWriter writer = new BuildEventArgsWriter(binaryWriter); + + writer.WriteBlob(BinaryLogRecordKind.ProjectImportArchive, inputStream); + binaryWriter.Flush(); + + outputStream.Position = 0; + BinaryReader binaryReader = new BinaryReader(outputStream); + Assert.Equal(BinaryLogRecordKind.ProjectImportArchive, (BinaryLogRecordKind)binaryReader.Read7BitEncodedInt()); + Assert.Equal(bytes.Length, binaryReader.Read7BitEncodedInt()); + Assert.Equal(bytes, binaryReader.ReadBytes(bytes.Length)); + } + [Theory] [InlineData(true)] [InlineData(false)] diff --git a/src/Build/Logging/BinaryLogger/BinaryLogger.cs b/src/Build/Logging/BinaryLogger/BinaryLogger.cs index 28a16df7c9c..6dfc5cfb26d 100644 --- a/src/Build/Logging/BinaryLogger/BinaryLogger.cs +++ b/src/Build/Logging/BinaryLogger/BinaryLogger.cs @@ -239,7 +239,14 @@ public void Shutdown() { using (FileStream fileStream = File.OpenRead(archiveFilePath)) { - eventArgsWriter.WriteBlob(BinaryLogRecordKind.ProjectImportArchive, fileStream); + if (fileStream.Length > int.MaxValue) + { + LogMessage("Imported files archive exceeded 2GB limit and it's not embedded."); + } + else + { + eventArgsWriter.WriteBlob(BinaryLogRecordKind.ProjectImportArchive, fileStream); + } } File.Delete(archiveFilePath); diff --git a/src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs b/src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs index 7b40c84f4be..084c6416564 100644 --- a/src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs +++ b/src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs @@ -220,12 +220,17 @@ public void WriteBlob(BinaryLogRecordKind kind, byte[] bytes) public void WriteBlob(BinaryLogRecordKind kind, Stream stream) { + if (stream.Length > int.MaxValue) + { + throw new ArgumentOutOfRangeException(nameof(stream)); + } + // write the blob directly to the underlying writer, // bypassing the memory stream using var redirection = RedirectWritesToOriginalWriter(); Write(kind); - Write(stream.Length); + Write((int)stream.Length); Write(stream); }