Fix out-of-bounds reads in Validate()'s bow-tie and adjacency checks - #317
Open
Roland Shum (ShumWengSang) wants to merge 1 commit into
Open
Conversation
ValidateNoBowties used a vertex index equal to the unused-vertex sentinel index_t(-1) directly as the subscript faceIds[j] (faceIds sized nVerts). ValidateIndices allows the sentinel through, so it reached the bow-tie walk and indexed past the array. Skip it, as CleanImpl already does on the equivalent loop. Under VALIDATE_ASYMMETRIC_ADJ, find_edge read adjacency[k*3..+2] with an out-of-range k guarded only by assert(k < nFaces); in diagnostic-message mode the out-of-range value is recorded but does not stop processing, and the assert is elided under NDEBUG, so release builds had no check. Promote it to a runtime bound.
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Validate()is meant to diagnose untrusted meshes, but two inputs it is specifically supposed to catch instead made it read out of bounds:Bow-tie check (
DirectXMeshValidate.cpp:335). A vertex index equal to the unused-vertex sentinelindex_t(-1)is used directly as the subscriptfaceIds[j], wherefaceIdsis sizednVerts.ValidateIndicesintentionally lets the sentinel through (it marks an unused vertex), so it reaches the bow-tie walk and indexes past the end of the array — an out-of-bounds read and write.Adjacency check (
DirectXMeshP.h:409, underVALIDATE_ASYMMETRIC_ADJ). An out-of-range adjacency indexkis used to readadjacency[k*3 .. +2], guarded only byassert(k < nFaces). In diagnostic-message mode the out-of-range value is recorded but does not stop processing, so it reachesfind_edge— and the assert is compiled out underNDEBUG, so release builds had no check at all.Fix
Both are one-line bounds guards, each mirroring a check the library already uses elsewhere:
CleanImplalready does on the equivalent loop:if (j == index_t(-1)) continue;if (k >= nFaces) { result = false; continue; }Total change is +7 / −1 in one file. A valid mesh is unaffected; a genuinely invalid one still returns
E_FAIL— only the out-of-bounds access is removed.Behavior
Each row replays an input against unpatched vs. patched builds (Release
/O2 /DNDEBUG, AddressSanitizer). The fault site is the same line the diff touches.#1 under
VALIDATE_BOWTIES; #2 underVALIDATE_ASYMMETRIC_ADJ+ diagnostic messages.DirectXMeshValidate.cpp:335{ 0xFFFF, 1, 2 }, nVerts = 3S_OKDirectXMeshValidate.cpp:335{ 0xFFFFFFFF, 1, 2 }(32-bit index)S_OKDirectXMeshP.h:409{ 5, -1, -1 }, nFaces = 1E_FAILDirectXMeshP.h:409{ 1, -1, -1 }—k == nFacesboundaryE_FAILS_OK→S_OKE_FAIL→E_FAILE_FAIL→E_FAIL