Skip to content

Commit 68e1fd0

Browse files
committed
ensure it does not skip outside of the buffer
1 parent 8d10d72 commit 68e1fd0

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ public static void populateBuffer(Object store, ByteBuffer source, int count, in
328328
populateMatrix4fArray((Matrix4f[]) store, source, count, byteOffset, byteStride, numComponents, format);
329329
}
330330
}
331+
332+
private static void skip(ByteBuffer buff, int n) {
333+
buff.position(Math.min(buff.position() + n, buff.limit()));
334+
}
335+
331336
private static void populateByteBuffer(ByteBuffer buffer, ByteBuffer source, int count, int byteOffset, int byteStride, int numComponents, VertexBuffer.Format format) {
332337
int componentSize = format.getComponentSize();
333338
int index = byteOffset;
@@ -355,7 +360,7 @@ private static void populateShortBuffer(ShortBuffer buffer, ByteBuffer source, i
355360
}
356361

357362
if (dataLength < stride) {
358-
source.position(source.position() + (stride - dataLength));
363+
skip(source, stride - dataLength);
359364
}
360365
index += stride;
361366
}
@@ -374,7 +379,7 @@ private static void populateIntBuffer(IntBuffer buffer, ByteBuffer source, int c
374379
buffer.put(source.getInt());
375380
}
376381
if (dataLength < stride) {
377-
source.position(source.position() + (stride - dataLength));
382+
skip(source, stride - dataLength);
378383
}
379384
index += stride;
380385
}
@@ -392,7 +397,7 @@ private static void populateFloatBuffer(FloatBuffer buffer, ByteBuffer source, i
392397
buffer.put(readAsFloat(source, format));
393398
}
394399
if (dataLength < stride) {
395-
source.position(source.position() + (stride - dataLength));
400+
skip(source, stride - dataLength);
396401
}
397402
index += stride;
398403
}
@@ -448,7 +453,7 @@ private static void populateByteArray(byte[] array, ByteBuffer source, int count
448453
System.arraycopy(buffer, 0, array, arrayIndex, numComponents);
449454
arrayIndex += numComponents;
450455
if (dataLength < stride) {
451-
source.position(source.position() + (stride - dataLength));
456+
skip(source, stride - dataLength);
452457
}
453458
index += stride;
454459
}
@@ -484,7 +489,7 @@ private static void populateShortArray(short[] array, ByteBuffer source, int cou
484489
arrayIndex++;
485490
}
486491
if (dataLength < stride) {
487-
source.position(source.position() + (stride - dataLength));
492+
skip(source, stride - dataLength);
488493
}
489494
index += stride;
490495
}
@@ -582,7 +587,7 @@ private static void populateFloatArray(float[] array, ByteBuffer source, int cou
582587
arrayIndex++;
583588
}
584589
if (dataLength < stride) {
585-
source.position(source.position() + (stride - dataLength));
590+
skip(source, stride - dataLength);
586591
}
587592
index += stride;
588593
}
@@ -605,7 +610,7 @@ private static void populateVector3fArray(Vector3f[] array, ByteBuffer source, i
605610

606611
arrayIndex++;
607612
if (dataLength < stride) {
608-
source.position(source.position() + (stride - dataLength));
613+
skip(source, stride - dataLength);
609614
}
610615
index += stride;
611616
}
@@ -629,7 +634,7 @@ private static void populateQuaternionArray(Quaternion[] array, ByteBuffer sourc
629634

630635
arrayIndex++;
631636
if (dataLength < stride) {
632-
source.position(source.position() + (stride - dataLength));
637+
skip(source, stride - dataLength);
633638
}
634639
index += stride;
635640
}
@@ -667,7 +672,7 @@ private static void populateMatrix4fArray(Matrix4f[] array, ByteBuffer source, i
667672

668673
arrayIndex++;
669674
if (dataLength < stride) {
670-
source.position(source.position() + (stride - dataLength));
675+
skip(source, stride - dataLength);
671676
}
672677

673678
index += stride;

0 commit comments

Comments
 (0)