Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Conversation

glenebob
Copy link

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
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.


var sig = EndianUtilities.ToUInt32LittleEndian(sectorBuffer, 0);
Copy link
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
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
@LTRData
Copy link
Owner

LTRData commented Feb 29, 2024

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
@LTRData LTRData merged commit 59e6430 into LTRData:LTRData.DiscUtils-initial Mar 1, 2024
1 check passed
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