Fix buffer overrun if header specifies more vertices than the file really has #54
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.
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)