diff --git a/docs/public-api.md b/docs/public-api.md index 7a00678..9959296 100644 --- a/docs/public-api.md +++ b/docs/public-api.md @@ -94,7 +94,7 @@ drop points that fail a plausibility rule (see the README), so a scan can yield | COPC | The header count (the extended 1.4 count when it states one). It is not checked against the chunk data until read. | | EPT | The point count declared in the manifest, or the sum of points across discovered octree nodes. | | Leica PTG | Total valid returns across all gridded scans, determined by decoding the column validity bitmasks. | -| CC BIN | The point count declared for the entity. Points with non-finite coordinates are skipped. Packed point normals are decoded in file versions 41, 42 and 50. A stored zero direction is returned as `PclVector3.Zero`. | +| CC BIN | The point count declared for the entity. The reader rejects files with a truncated XYZ block or a point count that exceeds the available XYZ data. Points with non-finite coordinates are skipped. Packed point normals are decoded in file versions 41, 42 and 50. A stored zero direction is returned as `PclVector3.Zero`. | `HasColors`, `HasIntensities` and `HasNormals` follow what the file contains, except for the scans of a ReCap project, where all three are true when the `.rcs` file was found and false otherwise, without the file being opened. A ReCap scan reads the channels diff --git a/src/PclIO/Formats/CcBin/CcBinDocument.cs b/src/PclIO/Formats/CcBin/CcBinDocument.cs index 07c8097..9af2b95 100644 --- a/src/PclIO/Formats/CcBin/CcBinDocument.cs +++ b/src/PclIO/Formats/CcBin/CcBinDocument.cs @@ -203,7 +203,8 @@ private bool TryParsePointCloud( // 43 bytes: flags (8), shift (24), scale (8), marker (3) byte[] ccHead = new byte[43]; - if (fs.Read(ccHead) < 43) return false; + // Confirm the XYZ array before treating an incomplete point block as a damaged scan. + if (fs.Read(ccHead) < ccHead.Length || ccHead[^1] != 3) return false; double sx = BinaryPrimitives.ReadDoubleLittleEndian(ccHead.AsSpan(8, 8)); double sy = BinaryPrimitives.ReadDoubleLittleEndian(ccHead.AsSpan(16, 8)); @@ -213,13 +214,15 @@ private bool TryParsePointCloud( throw FormatError.Corrupted("CC BIN has an invalid global shift or scale."); byte[] countBuf = new byte[4]; - if (fs.Read(countBuf) < 4) return false; + if (fs.Read(countBuf) < countBuf.Length) + throw FormatError.Truncated($"CC BIN point count for scan {scanIndex}"); uint count = BinaryPrimitives.ReadUInt32LittleEndian(countBuf); if (count == 0) return false; long ptStart = fs.Position; long ptBytes = (long)count * 12; - if (ptStart + ptBytes > fileLength) return false; + if (ptBytes > fileLength - ptStart) + throw FormatError.Truncated($"CC BIN XYZ coordinates for scan {scanIndex} ({count} declared points)"); pointsEndOffset = ptStart + ptBytes; diff --git a/tests/PclIO.Tests/FormatTests.cs b/tests/PclIO.Tests/FormatTests.cs index 29bb1fe..c5e2036 100644 --- a/tests/PclIO.Tests/FormatTests.cs +++ b/tests/PclIO.Tests/FormatTests.cs @@ -6519,6 +6519,28 @@ public void Cc_Bin_Opens_Adjacent_Multi_Cloud_Entities() Assert.Equal(2, doc.Scans[1].PointCount); } + [Fact] + public void Cc_Bin_Rejects_Truncated_Coordinates_In_A_Later_Cloud() + { + using var dir = new TempDirectory(); + byte[] bytes = SyntheticFixtures.CreateSyntheticCcBinMultiCloud( + [[new(1, 2, 3)], [new(4, 5, 6), new(7, 8, 9)]], truncateLastCloudPoints: true); + + var error = Assert.Throws(() => PclFile.Open(dir.Write("truncated_xyz.bin", bytes))); + Assert.Contains("XYZ coordinates", error.Message); + } + + [Fact] + public void Cc_Bin_Rejects_An_Oversized_Point_Count_In_A_Later_Cloud() + { + using var dir = new TempDirectory(); + byte[] bytes = SyntheticFixtures.CreateSyntheticCcBinMultiCloud( + [[new(1, 2, 3)], [new(4, 5, 6)]], declaredLastCloudPointCount: uint.MaxValue); + + var error = Assert.Throws(() => PclFile.Open(dir.Write("oversized_count.bin", bytes))); + Assert.Contains("XYZ coordinates", error.Message); + } + [Fact] public void Cc_Bin_Reads_Intensity_Within_Declared_Count_Only() { diff --git a/tests/PclIO.Tests/SyntheticFixtures.cs b/tests/PclIO.Tests/SyntheticFixtures.cs index 5a4adc4..a720e94 100644 --- a/tests/PclIO.Tests/SyntheticFixtures.cs +++ b/tests/PclIO.Tests/SyntheticFixtures.cs @@ -3000,7 +3000,9 @@ public static byte[] CreateSyntheticCcBin( /// Creates a synthetic CC BIN file containing multiple adjacent cloud entities. public static byte[] CreateSyntheticCcBinMultiCloud( IReadOnlyList> clouds, - uint version = 47) + uint version = 47, + bool truncateLastCloudPoints = false, + uint? declaredLastCloudPointCount = null) { using var ms = new MemoryStream(); ms.Write("CCB2"u8.ToArray()); @@ -3011,12 +3013,15 @@ public static byte[] CreateSyntheticCcBinMultiCloud( for (int c = 0; c < clouds.Count; c++) { + bool isLast = c == clouds.Count - 1; WriteCcBinCloudEntity( ms, clouds[c], name: $"Cloud_{c + 1}", id: (uint)(100 + c), - version: version); + version: version, + truncatePoints: isLast && truncateLastCloudPoints, + declaredPointCount: isLast ? declaredLastCloudPointCount : null); } return ms.ToArray(); @@ -3040,7 +3045,8 @@ private static void WriteCcBinCloudEntity( IReadOnlyDictionary? doubleProperties = null, bool truncatePoints = false, bool truncateColorBlock = false, - bool truncateIntensityData = false) + bool truncateIntensityData = false, + uint? declaredPointCount = null) { Span u32Buf = stackalloc byte[4]; @@ -3108,7 +3114,7 @@ private static void WriteCcBinCloudEntity( ms.WriteByte(0x03); // 6. Point count (uint32 LE) - BinaryPrimitives.WriteUInt32LittleEndian(u32Buf, (uint)points.Count); + BinaryPrimitives.WriteUInt32LittleEndian(u32Buf, declaredPointCount ?? (uint)points.Count); ms.Write(u32Buf); // 7. Points data (12 bytes per point)