Przeglądaj źródła

ensure it does not skip outside of the buffer

Riccardo Balbo 3 miesięcy temu
rodzic
commit
68e1fd0534

+ 14 - 9
jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java

@@ -328,6 +328,11 @@ public class GltfUtils {
             populateMatrix4fArray((Matrix4f[]) store, source, count, byteOffset, byteStride, numComponents, format);
             populateMatrix4fArray((Matrix4f[]) store, source, count, byteOffset, byteStride, numComponents, format);
         }
         }
     }
     }
+
+    private static void skip(ByteBuffer buff, int n) {
+        buff.position(Math.min(buff.position() + n, buff.limit()));
+    }
+
     private static void populateByteBuffer(ByteBuffer buffer, ByteBuffer source, int count, int byteOffset, int byteStride, int numComponents, VertexBuffer.Format format) {
     private static void populateByteBuffer(ByteBuffer buffer, ByteBuffer source, int count, int byteOffset, int byteStride, int numComponents, VertexBuffer.Format format) {
         int componentSize = format.getComponentSize();
         int componentSize = format.getComponentSize();
         int index = byteOffset;
         int index = byteOffset;
@@ -355,7 +360,7 @@ public class GltfUtils {
             }
             }
 
 
             if (dataLength < stride) {
             if (dataLength < stride) {
-                source.position(source.position() + (stride - dataLength));
+                skip(source, stride - dataLength);
             }
             }
             index += stride;
             index += stride;
         }
         }
@@ -374,7 +379,7 @@ public class GltfUtils {
                 buffer.put(source.getInt());
                 buffer.put(source.getInt());
             }
             }
             if (dataLength < stride) {
             if (dataLength < stride) {
-                source.position(source.position() + (stride - dataLength));
+                skip(source, stride - dataLength);
             }
             }
             index += stride;
             index += stride;
         }
         }
@@ -392,7 +397,7 @@ public class GltfUtils {
                 buffer.put(readAsFloat(source, format));
                 buffer.put(readAsFloat(source, format));
             }
             }
             if (dataLength < stride) {
             if (dataLength < stride) {
-                source.position(source.position() + (stride - dataLength));
+                skip(source, stride - dataLength);
             }
             }
             index += stride;
             index += stride;
         }
         }
@@ -448,7 +453,7 @@ public class GltfUtils {
             System.arraycopy(buffer, 0, array, arrayIndex, numComponents);
             System.arraycopy(buffer, 0, array, arrayIndex, numComponents);
             arrayIndex += numComponents;
             arrayIndex += numComponents;
             if (dataLength < stride) {
             if (dataLength < stride) {
-                source.position(source.position() + (stride - dataLength));
+                skip(source, stride - dataLength);
             }
             }
             index += stride;
             index += stride;
         }
         }
@@ -484,7 +489,7 @@ public class GltfUtils {
                 arrayIndex++;
                 arrayIndex++;
             }
             }
             if (dataLength < stride) {
             if (dataLength < stride) {
-                source.position(source.position() + (stride - dataLength));
+                skip(source, stride - dataLength);
             }
             }
             index += stride;
             index += stride;
         }
         }
@@ -582,7 +587,7 @@ public class GltfUtils {
                 arrayIndex++;
                 arrayIndex++;
             }
             }
             if (dataLength < stride) {
             if (dataLength < stride) {
-                source.position(source.position() + (stride - dataLength));
+                skip(source, stride - dataLength);
             }
             }
             index += stride;
             index += stride;
         }
         }
@@ -605,7 +610,7 @@ public class GltfUtils {
 
 
             arrayIndex++;
             arrayIndex++;
             if (dataLength < stride) {
             if (dataLength < stride) {
-                source.position(source.position() + (stride - dataLength));
+                skip(source, stride - dataLength);
             }
             }
             index += stride;
             index += stride;
         }
         }
@@ -629,7 +634,7 @@ public class GltfUtils {
 
 
             arrayIndex++;
             arrayIndex++;
             if (dataLength < stride) {
             if (dataLength < stride) {
-                source.position(source.position() + (stride - dataLength));
+                skip(source, stride - dataLength);
             }
             }
             index += stride;
             index += stride;
         }
         }
@@ -667,7 +672,7 @@ public class GltfUtils {
 
 
             arrayIndex++;
             arrayIndex++;
             if (dataLength < stride) {
             if (dataLength < stride) {
-                source.position(source.position() + (stride - dataLength));
+                skip(source, stride - dataLength);
             }
             }
 
 
             index += stride;
             index += stride;