Skip to content

Commit

Permalink
BatchNode: Fix IndexOutOfBoundsException (#2297)
Browse files Browse the repository at this point in the history
- There are temporary arrays that are reused to work with vertex buffers.

- Some conditions cause the index to be used for storing more vertices that they can hold.

- This throws the exception.

- This fix validates the arrays size before accessing them, recreating the arrays if they are too small.
  • Loading branch information
jcfandino authored Jul 31, 2024
1 parent a9fec64 commit 1e10225
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions jme3-core/src/main/java/com/jme3/scene/BatchNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,7 @@ protected void doBatch() {

//init the temp arrays if something has been batched only.
if (matMap.size() > 0) {
//TODO these arrays should be allocated by chunk instead to avoid recreating them each time the batch is changed.
//init temp float arrays
tmpFloat = new float[maxVertCount * 3];
tmpFloatN = new float[maxVertCount * 3];
if (useTangents) {
tmpFloatT = new float[maxVertCount * 4];
}
initTempFloatArrays();
}
}

Expand Down Expand Up @@ -387,7 +381,6 @@ private void mergeGeometries(Mesh outMesh, List<Geometry> geometries) {
int maxWeights = -1;

Mesh.Mode mode = null;
float lineWidth = 1f;
for (Geometry geom : geometries) {
totalVerts += geom.getVertexCount();
totalTris += geom.getTriangleCount();
Expand Down Expand Up @@ -537,6 +530,7 @@ private void doTransforms(FloatBuffer bindBufPos, FloatBuffer bindBufNorm, Float
Vector3f norm = vars.vect2;
Vector3f tan = vars.vect3;

validateTempFloatArrays(end - start);
int length = (end - start) * 3;
int tanLength = (end - start) * 4;

Expand Down Expand Up @@ -617,6 +611,22 @@ private void doTransforms(FloatBuffer bindBufPos, FloatBuffer bindBufNorm, Float
}
}

private void validateTempFloatArrays(int vertCount) {
if (maxVertCount < vertCount) {
maxVertCount = vertCount;
initTempFloatArrays();
}
}

private void initTempFloatArrays() {
//TODO these arrays should be allocated by chunk instead to avoid recreating them each time the batch is changed.
tmpFloat = new float[maxVertCount * 3];
tmpFloatN = new float[maxVertCount * 3];
if (useTangents) {
tmpFloatT = new float[maxVertCount * 4];
}
}

private void doCopyBuffer(FloatBuffer inBuf, int offset, FloatBuffer outBuf, int componentSize) {
TempVars vars = TempVars.get();
Vector3f pos = vars.vect1;
Expand Down

0 comments on commit 1e10225

Please sign in to comment.