Skip to content

IsContentOfFilesTheSame assumes full Stream.Read #825

Closed
@KalleOlaviNiemitalo

Description

In the Nerdbank.GitVersioning.Tasks.CompareFiles class, the IsContentOfFilesTheSame method calls Stream.Read and assumes that a return value smaller than requested means that it got to the end of the file. As mentioned in #797 (comment), such an assumption is generally a bug. Here though, the stream refers to a regular file, which makes the assumption more likely to hold.

bytesRead = fileStream1.Read(buffer1, 0, buffer1.Length);
if (fileStream2.Read(buffer2, 0, buffer2.Length) != bytesRead)
{
// We should never get here since we compared file lengths, but
// this is a sanity check.
return false;
}

The CompareFiles task is used in Nerdbank.GitVersioning.targets, for deciding whether to run the Copy task. The bug might cause the task to consider the files different when they are actually identical; the target would then needlessly copy the file and cause MSBuild to execute other targets, resulting in a slower incremental build but eventually the correct artefacts.

It might be possible to delete the CompareFiles task altogether and use the SkipUnchangedFiles parameter of the Copy task instead.

<!-- Avoid applying the newly generated AssemblyVersionInfo.cs file to the build
unless it has changed in order to allow for incremental building. -->
<Nerdbank.GitVersioning.Tasks.CompareFiles OriginalItems="$(VersionSourceFile)" NewItems="$(NewVersionSourceFile)">
<Output TaskParameter="AreChanged" PropertyName="AssemblyVersionInfoChanged" />
</Nerdbank.GitVersioning.Tasks.CompareFiles>
<Copy Condition=" '$(AssemblyVersionInfoChanged)' == 'true' " SourceFiles="$(NewVersionSourceFile)" DestinationFiles="$(VersionSourceFile)" />

<!-- Avoid applying the newly generated Version.rc file to the build
unless it has changed in order to allow for incremental building. -->
<Nerdbank.GitVersioning.Tasks.CompareFiles OriginalItems="$(VersionSourceFile)" NewItems="$(NewVersionSourceFile)">
<Output TaskParameter="AreChanged" PropertyName="_NativeVersionInfoChanged" />
</Nerdbank.GitVersioning.Tasks.CompareFiles>
<Copy Condition=" '$(_NativeVersionInfoChanged)' == 'true' " SourceFiles="$(NewVersionSourceFile)" DestinationFiles="$(VersionSourceFile)" />

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions