Skip to content

WIP (4.0) Fix reading of massive scans (>~4.3 billion points) - #381

Open
asmaloney wants to merge 1 commit into
masterfrom
fix-type-mismatch
Open

asmaloney wants to merge 1 commit into
masterfrom
fix-type-mismatch

Conversation

@asmaloney

Copy link
Copy Markdown
Owner

Type

  • New feature
  • Bug fix
  • Refactor
  • Documentation/formatting

NOTE: This changes the CompressedVectorReader API, so it will have to wait for 4.0 to be merged.

Summary

It was possible to corrupt data when reading a very large scan (> 2^32 points) because of a type mismatch between some capacities and indices (size_t vs. unsigned int).

Fixed by changing unsigned int to size_t throughout.

Additional Notes

I generated the test file based on the example in #357. It resulted in a ~26GB file with the following structure:

<?xml version="1.0"?>
<e57Root type="Structure" xmlns="http://www.astm.org/COMMIT/E57/2010-e57-v1.0">
    <formatName type="String"><![CDATA[ASTM E57 3D Imaging Data File]]></formatName>
    <guid type="String"><![CDATA[A GUID]]></guid>
    <versionMajor type="Integer">1</versionMajor>
    <versionMinor type="Integer" />
    <coordinateMetadata type="String" />
    <data3D type="Vector" allowHeterogeneousChildren="1">
        <vectorChild type="Structure">
            <guid type="String"><![CDATA[A Second GUID]]></guid>
            <name type="String"><![CDATA[int32-wrap repro]]></name>
            <description type="String"><![CDATA[4294967299 spherical points]]></description>
            <points type="CompressedVector" fileOffset="48" recordCount="4294967299">
                <prototype type="Structure">
                    <sphericalRange type="ScaledInteger" minimum="0" maximum="65535" scale="9.765625e-04" />
                    <sphericalAzimuth type="ScaledInteger" minimum="0" maximum="65535" scale="6.103515625e-05" offset="-2" />
                    <sphericalElevation type="ScaledInteger" minimum="0" maximum="65535" scale="3.0517578125e-05" offset="-1" />
                </prototype>
                <codecs type="Vector" allowHeterogeneousChildren="1" />
            </points>
        </vectorChild>
    </data3D>
    <images2D type="Vector" allowHeterogeneousChildren="1" />
</e57Root>

Before the fix:

file           : BigUn.e57
field          : sphericalRange
recordCount    : 4294967299
2^32           : 4294967296
K = N - 2^32   : 3

[A] single read(), capacity = 4294967299 (32.0 GiB)
    read() returned      : 3
    expected if correct  : 4294967299
    N mod 2^32           : 3
    -> under-reports     : YES

    slot                  got        expected     wrapped-from
                0     0.304688    51.420898     0.304688  <- record 2^32+i landed here
                1    37.358398    23.188477    37.358398  <- record 2^32+i landed here
                2    28.033203    21.701172    28.033203  <- record 2^32+i landed here
       4294967296 -12345.000000     0.304688            -  <- never written (sentinel intact)
       4294967297 -12345.000000    37.358398            -  <- never written (sentinel intact)
       4294967298 -12345.000000    28.033203            -  <- never written (sentinel intact)

    head slots wrong      : 3 / 3
    tail slots untouched  : 3 / 3
    middle spot-checks bad: 0 / 64

[B] looped read(), capacity = 10000000
    read() calls          : 430
    records transferred   : 4294967299
    mismatches            : 0
    -> CORRECT

==> REPRODUCED: same file, same library -- one read() corrupts, looped read() is exact.

After the fix:

file           : BigUn.e57
field          : sphericalRange
recordCount    : 4294967299
2^32           : 4294967296
K = N - 2^32   : 3

[A] single read(), capacity = 4294967299 (32.0 GiB)
    read() returned      : 3
    expected if correct  : 4294967299
    N mod 2^32           : 3
    -> under-reports     : YES

    slot                  got        expected     wrapped-from
                0    51.420898    51.420898     0.304688
                1    23.188477    23.188477    37.358398
                2    21.701172    21.701172    28.033203
       4294967296     0.304688     0.304688            -
       4294967297    37.358398    37.358398            -
       4294967298    28.033203    28.033203            -

    head slots wrong      : 0 / 3
    tail slots untouched  : 0 / 3
    middle spot-checks bad: 0 / 64

[B] looped read(), capacity = 10000000
    read() calls          : 430
    records transferred   : 4294967299
    mismatches            : 0
    -> CORRECT

==> NOT reproduced (see counts above).

Note that I could only test this on macOS.

Fixes #357

Checklist

  • The included code and/or docs were written by a human
  • If including code changes, clang-format was run on the code

It was possible to corrupt data when reading a very large scan (> 2^32 points) because of a type mismatch between some capacities and indices (size_t vs. unsigned int).

Fixes #357
@asmaloney

Copy link
Copy Markdown
Owner Author

@MeshMatze I can't say I understand all the stuff going on in that example, but running it produced expected results before and after the fix (see above).

I can only test this on macOS. Can you check it on Windows and/or Linux by any chance?

Because it changes the public API, I'm going to hold off merging this until version 4.0. I'm not sure too many people are using single scans with more than 4.3 billion points, so it's not too pressing…

Thank you!

@asmaloney asmaloney mentioned this pull request Nov 18, 2026
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.

Silent data corruption by buffer

1 participant