Skip to content

Commit 4f409d1

Browse files
Throw FileFormatException for invalid sectors
Additional fix for #354
1 parent a807c04 commit 4f409d1

File tree

5 files changed

+11
-49
lines changed

5 files changed

+11
-49
lines changed

OpenMcdf/FatChainEnumerator.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public bool MoveNext()
7979
return false;
8080
}
8181

82+
if (value > SectorType.Maximum)
83+
throw new FileFormatException($"Invalid FAT sector ID: {value}.");
84+
8285
index++;
8386
if (index >= fat.Context.SectorCount)
8487
{

OpenMcdf/MiniFatChainEnumerator.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public bool MoveNext()
7070
return false;
7171
}
7272

73+
if (value > SectorType.Maximum)
74+
throw new FileFormatException($"Invalid mini FAT sector ID: {value}.");
75+
7376
uint nextIndex = index + 1;
7477
if (nextIndex > SectorType.Maximum)
7578
throw new FileFormatException("Mini FAT chain length is greater than the maximum.");

OpenMcdf/MiniSector.cs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,15 @@ namespace OpenMcdf;
99
/// <param name="Length">The sector length</param>
1010
internal record struct MiniSector(uint Id, int Length)
1111
{
12-
public readonly bool IsValid => Id <= SectorType.Maximum;
13-
1412
/// <summary>
1513
/// The position of the mini sector in the mini FAT stream.
1614
/// </summary>
17-
public readonly long Position
18-
{
19-
get
20-
{
21-
ThrowIfInvalid();
22-
return Id * Length;
23-
}
24-
}
15+
public readonly long Position => Id * Length;
2516

2617
/// <summary>
2718
/// The end position of the mini sector in the mini FAT stream.
2819
/// </summary>
29-
public readonly long EndPosition
30-
{
31-
get
32-
{
33-
ThrowIfInvalid();
34-
return (Id + 1) * Length;
35-
}
36-
}
37-
38-
readonly void ThrowIfInvalid()
39-
{
40-
if (!IsValid)
41-
throw new InvalidOperationException($"Invalid mini FAT sector ID: {Id}.");
42-
}
20+
public readonly long EndPosition => (Id + 1) * Length;
4321

4422
[ExcludeFromCodeCoverage]
4523
public override readonly string ToString() => $"{Id}";

OpenMcdf/RootContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public void ExtendStreamLength(long length)
194194
void TrimBaseStream()
195195
{
196196
Sector lastUsedSector = Fat.GetLastUsedSector();
197-
if (!lastUsedSector.IsValid)
197+
if (lastUsedSector.Id > SectorType.Maximum)
198198
throw new FileFormatException("Last used sector is invalid");
199199

200200
if (Version is Version.V4 && lastUsedSector.EndPosition < RangeLockSectorOffset)

OpenMcdf/Sector.cs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,15 @@ internal record struct Sector(uint Id, int Length)
1111
{
1212
public static readonly Sector EndOfChain = new(SectorType.EndOfChain, 0);
1313

14-
public readonly bool IsValid => Id <= SectorType.Maximum;
15-
1614
/// <summary>
1715
/// The position of the sector in the compound file stream.
1816
/// </summary>
19-
public readonly long Position
20-
{
21-
get
22-
{
23-
ThrowIfInvalid();
24-
return (Id + 1) * Length;
25-
}
26-
}
17+
public readonly long Position => (Id + 1) * Length;
2718

2819
/// <summary>
2920
/// The end position of the sector in the compound file stream.
3021
/// </summary>
31-
public readonly long EndPosition
32-
{
33-
get
34-
{
35-
ThrowIfInvalid();
36-
return (Id + 2) * Length;
37-
}
38-
}
39-
40-
readonly void ThrowIfInvalid()
41-
{
42-
if (!IsValid)
43-
throw new InvalidOperationException($"Invalid FAT sector ID: {Id}.");
44-
}
22+
public readonly long EndPosition => (Id + 2) * Length;
4523

4624
[ExcludeFromCodeCoverage]
4725
public override readonly string ToString() => $"{Id}";

0 commit comments

Comments
 (0)