Fix underflow panic in InitTriInfo (#14893)

# Objective

- Fix #14874

## Solution

- Change the place where a panic occurs from `t < iNrTrianglesIn - 1` to
`t + 1 < iNrTrianglesIn`.

## Testing

- After the fix, the following code runs successfully without any panic.

```rust
use bevy::prelude::Mesh;
use bevy_render::{
    mesh::{Indices, PrimitiveTopology},
    render_asset::RenderAssetUsages,
};

const POSITIONS: &[[f32; 3]] = &[[0.0, 1.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0]];

const NORMALS: &[[f32; 3]] = &[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]];

const INDICES: &[u32] = &[0, 1, 2];

const UVS: &[[f32; 2]] = &[[0.0, 1.0], [0.0, 0.0], [0.0, 1.0]];

fn main() {
    let mut mesh = Mesh::new(
        PrimitiveTopology::TriangleList,
        RenderAssetUsages::default(),
    );

    mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, POSITIONS.to_vec());
    mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, UVS.to_vec());
    mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, NORMALS.to_vec());
    mesh.insert_indices(Indices::U32(INDICES.to_vec()));
    mesh.generate_tangents().ok();

}

```

## Migration Guide

- No breaking changes introduced.
This commit is contained in:
Kumikaya 2024-08-25 22:13:23 +08:00 committed by GitHub
parent 01cce9b11c
commit 9a2eb878a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -253,6 +253,10 @@ pub unsafe fn genTangSpace<I: Geometry>(geometry: &mut I, fAngularThreshold: f32
t += 1 t += 1
} }
iNrTrianglesIn = iTotTris - iDegenTriangles; iNrTrianglesIn = iTotTris - iDegenTriangles;
if iNrTrianglesIn <= 0 {
return false;
}
DegenPrologue( DegenPrologue(
pTriInfos.as_mut_ptr(), pTriInfos.as_mut_ptr(),
piTriListIn.as_mut_ptr(), piTriListIn.as_mut_ptr(),
@ -1030,7 +1034,7 @@ unsafe fn InitTriInfo<I: Geometry>(
} }
f += 1 f += 1
} }
while t < iNrTrianglesIn - 1 { while t + 1 < iNrTrianglesIn {
let iFO_a: i32 = (*pTriInfos.offset(t as isize)).iOrgFaceNumber; let iFO_a: i32 = (*pTriInfos.offset(t as isize)).iOrgFaceNumber;
let iFO_b: i32 = (*pTriInfos.offset((t + 1) as isize)).iOrgFaceNumber; let iFO_b: i32 = (*pTriInfos.offset((t + 1) as isize)).iOrgFaceNumber;
if iFO_a == iFO_b { if iFO_a == iFO_b {