Skip to content

VHDX LogEntry.TryRead() Bug Fix and Improvements#5

Merged
Olof-Lagerkvist merged 1 commit into
LTRData:LTRData.DiscUtils-initialfrom
glenebob:vhdx-logentry-tryread-improvements
Mar 1, 2024
Merged

VHDX LogEntry.TryRead() Bug Fix and Improvements#5
Olof-Lagerkvist merged 1 commit into
LTRData:LTRData.DiscUtils-initialfrom
glenebob:vhdx-logentry-tryread-improvements

Conversation

@glenebob

Copy link
Copy Markdown

Change LogEntryHeader member variables to properties.
Read header into 64-byte, stackalloc'd buffer, then read remaining bytes directly into logEntryBuffer.
Remove buggy byte availability calculation.
Remove redundant signature check (which is done in LogEntryHeader.IsValid).
Use ReadMaximum() instread of ReadExactly() to guarantee no exception will be thrown, and use its return value to verify correct byte count.

@glenebob

Copy link
Copy Markdown
Author

I made some changes back in 2017 in the DiscUtils/DiscUtils repo, and then more recently noticed a bug in this code related to those changes. I originally opened a pull request in DiscUtils/DiscUtils with changes very similar to these, but that repo is basically abandoned, so I ported the changes for this repo.

}
headerBuffer.CopyTo(logEntryBuffer);

var sig = EndianUtilities.ToUInt32LittleEndian(sectorBuffer, 0);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant.

var header = new LogEntryHeader();
header.ReadFrom(sectorBuffer.AsSpan(0, LogSectorSize));

if (!header.IsValid || header.EntryLength > logStream.Length)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This data availability check would be broken when working within a stream starting at an offset > 0. ReadMaximum() below does the availability check correctly.

@glenebob glenebob marked this pull request as ready for review February 29, 2024 19:40
@Olof-Lagerkvist

Copy link
Copy Markdown
Member

Thanks a lot for the contribution! I'll just take a look at the changes before I merge the PR. But it looks like a good improvement.

Change LogEntryHeader member variables to properties.
Read header into 64-byte, stackalloc'd buffer, then read remaining bytes directly into logEntryBuffer.
Remove buggy byte availability calculation.
Remove redundant signature check (which is done in LogEntryHeader.IsValid).
Use ReadMaximum instread of ReadExactly to guarantee no exception will be thrown, and use its return value to verify correct byte count.
@glenebob glenebob force-pushed the vhdx-logentry-tryread-improvements branch from 5edede9 to b0ecacc Compare March 1, 2024 22:04
@Olof-Lagerkvist Olof-Lagerkvist merged commit 59e6430 into LTRData:LTRData.DiscUtils-initial Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants