forked from DiscUtils/DiscUtils
-
-
Notifications
You must be signed in to change notification settings - Fork 32
VHDX LogEntry.TryRead() Bug Fix and Improvements #5
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
Merged
Olof-Lagerkvist
merged 1 commit into
LTRData:LTRData.DiscUtils-initial
from
glenebob:vhdx-logentry-tryread-improvements
Mar 1, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,9 +33,9 @@ namespace DiscUtils.Vhdx; | |
|
|
||
| internal sealed class LogEntry | ||
| { | ||
| public const int LogSectorSize = (int)(4 * Sizes.OneKiB); | ||
| private readonly List<Descriptor> _descriptors = new List<Descriptor>(); | ||
| public const int LogSectorSize = 4 * Sizes.OneKiB; | ||
|
|
||
| private readonly List<Descriptor> _descriptors = new List<Descriptor>(); | ||
| private readonly LogEntryHeader _header; | ||
|
|
||
| private LogEntry(long position, LogEntryHeader header, List<Descriptor> descriptors) | ||
|
|
@@ -101,90 +101,84 @@ public static bool TryRead(Stream logStream, out LogEntry entry) | |
| { | ||
| var position = logStream.Position; | ||
|
|
||
| var sectorBuffer = ArrayPool<byte>.Shared.Rent(LogSectorSize); | ||
| var bytesToRead = LogEntryHeader.ByteCount; | ||
| Span<byte> headerBuffer = stackalloc byte[bytesToRead]; | ||
| if (logStream.ReadMaximum(headerBuffer) != bytesToRead) | ||
| { | ||
| entry = null; | ||
| return false; | ||
| } | ||
|
|
||
| var header = new LogEntryHeader(); | ||
| header.ReadFrom(headerBuffer); | ||
|
|
||
| if (!header.IsValid) | ||
| { | ||
| entry = null; | ||
| return false; | ||
| } | ||
|
|
||
| var entryLength = checked((int)header.EntryLength); | ||
| var logEntryBuffer = ArrayPool<byte>.Shared.Rent(entryLength); | ||
|
|
||
| try | ||
| { | ||
| if (logStream.ReadMaximum(sectorBuffer, 0, LogSectorSize) != LogSectorSize) | ||
| { | ||
| entry = null; | ||
| return false; | ||
| } | ||
| headerBuffer.CopyTo(logEntryBuffer); | ||
|
|
||
| var sig = EndianUtilities.ToUInt32LittleEndian(sectorBuffer, 0); | ||
| if (sig != LogEntryHeader.LogEntrySignature) | ||
| bytesToRead = entryLength - LogEntryHeader.ByteCount; | ||
| if (logStream.ReadMaximum(logEntryBuffer, LogEntryHeader.ByteCount, bytesToRead) != bytesToRead) | ||
| { | ||
| entry = null; | ||
| return false; | ||
| } | ||
|
|
||
| var header = new LogEntryHeader(); | ||
| header.ReadFrom(sectorBuffer.AsSpan(0, LogSectorSize)); | ||
|
|
||
| if (!header.IsValid || header.EntryLength > logStream.Length) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| Array.Clear(logEntryBuffer, 4, sizeof(uint)); | ||
| if (header.Checksum != | ||
| Crc32LittleEndian.Compute(Crc32Algorithm.Castagnoli, logEntryBuffer, 0, entryLength)) | ||
| { | ||
| entry = null; | ||
| return false; | ||
| } | ||
|
|
||
| var logEntryBuffer = ArrayPool<byte>.Shared.Rent(checked((int)header.EntryLength)); | ||
| try | ||
| { | ||
| System.Buffer.BlockCopy(sectorBuffer, 0, logEntryBuffer, 0, LogSectorSize); | ||
|
|
||
| logStream.ReadExactly(logEntryBuffer, LogSectorSize, checked((int)(header.EntryLength - LogSectorSize))); | ||
|
|
||
| EndianUtilities.WriteBytesLittleEndian(0, logEntryBuffer, 4); | ||
| if (header.Checksum != | ||
| Crc32LittleEndian.Compute(Crc32Algorithm.Castagnoli, logEntryBuffer, 0, (int)header.EntryLength)) | ||
| { | ||
| entry = null; | ||
| return false; | ||
| } | ||
| var dataPos = MathUtilities.RoundUp((int)header.DescriptorCount * 32 + 64, LogSectorSize); | ||
|
|
||
| var dataPos = MathUtilities.RoundUp((int)header.DescriptorCount * 32 + 64, LogSectorSize); | ||
| var descriptors = new List<Descriptor>(); | ||
| for (var i = 0; i < header.DescriptorCount; ++i) | ||
| { | ||
| var offset = i * 32 + 64; | ||
| Descriptor descriptor; | ||
|
|
||
| var descriptors = new List<Descriptor>(); | ||
| for (var i = 0; i < header.DescriptorCount; ++i) | ||
| var descriptorSig = EndianUtilities.ToUInt32LittleEndian(logEntryBuffer, offset); | ||
| switch (descriptorSig) | ||
| { | ||
| var offset = i * 32 + 64; | ||
| Descriptor descriptor; | ||
|
|
||
| var descriptorSig = EndianUtilities.ToUInt32LittleEndian(logEntryBuffer, offset); | ||
| switch (descriptorSig) | ||
| { | ||
| case Descriptor.ZeroDescriptorSignature: | ||
| descriptor = new ZeroDescriptor(); | ||
| break; | ||
| case Descriptor.DataDescriptorSignature: | ||
| descriptor = new DataDescriptor(logEntryBuffer, dataPos); | ||
| dataPos += LogSectorSize; | ||
| break; | ||
| default: | ||
| entry = null; | ||
| return false; | ||
| } | ||
|
|
||
| descriptor.ReadFrom(logEntryBuffer, offset); | ||
| if (!descriptor.IsValid(header.SequenceNumber)) | ||
| { | ||
| case Descriptor.ZeroDescriptorSignature: | ||
| descriptor = new ZeroDescriptor(); | ||
| break; | ||
| case Descriptor.DataDescriptorSignature: | ||
| descriptor = new DataDescriptor(logEntryBuffer, dataPos); | ||
| dataPos += LogSectorSize; | ||
| break; | ||
| default: | ||
| entry = null; | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| descriptors.Add(descriptor); | ||
| descriptor.ReadFrom(logEntryBuffer, offset); | ||
| if (!descriptor.IsValid(header.SequenceNumber)) | ||
| { | ||
| entry = null; | ||
| return false; | ||
| } | ||
|
|
||
| entry = new LogEntry(position, header, descriptors); | ||
| return true; | ||
| } | ||
| finally | ||
| { | ||
| ArrayPool<byte>.Shared.Return(logEntryBuffer); | ||
| descriptors.Add(descriptor); | ||
| } | ||
|
|
||
| entry = new LogEntry(position, header, descriptors); | ||
| return true; | ||
| } | ||
| finally | ||
| { | ||
| ArrayPool<byte>.Shared.Return(sectorBuffer); | ||
| ArrayPool<byte>.Shared.Return(logEntryBuffer); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant.