ddiakopoulos / tinyply

:earth_africa: C++11 ply 3d mesh format importer & exporter
598 stars 118 forks source link

Fix buffer overrun if header specifies more vertices than the file really has #54

Open RamonArguelles opened 2 years ago

RamonArguelles commented 2 years ago

As discussed in here:

There is a buffer overrun when parsing a PLY with a header that specifies more vertices than what the file really has. Aka, if the header says there are 20 vertices, but the contents only have 19, you'll hit the problem.

Looking a little bit into the code, tinyply already uses the information from the header to allocate a buffer that should fit the file contents. The size of the buffer is thus known, but it is not passed all the way down. This PR implements a simple fix that mainly passes the buffer size a couple of frames down into the stack, when we are about to read from the file and copy into the buffer. If we pass the buffer size down, we can check if the copy is going to fit, and if not, throw an exception instead of attempting to reference beyond the buffer.

We tested our local fix with both master and 2.4 branches, and it does seem to work in both cases.

This PR is for the 2.4 branch. Another alternative, though, is to complete the PR into master first, and then merge master into the 2.4 branch (dealing with the merge conflicts at that point)