|
42 | 42 |
|
43 | 43 | import java.nio.ByteBuffer; |
44 | 44 | import java.nio.ByteOrder; |
45 | | -import java.util.Objects; |
46 | 45 |
|
47 | 46 | import com.oracle.truffle.api.dsl.Bind; |
48 | 47 | import com.oracle.truffle.api.dsl.Cached; |
|
57 | 56 | import com.oracle.truffle.api.object.Shape; |
58 | 57 | import com.oracle.truffle.api.profiles.InlinedBranchProfile; |
59 | 58 | import com.oracle.truffle.api.strings.TruffleString; |
| 59 | +import com.oracle.truffle.js.runtime.Boundaries; |
60 | 60 | import com.oracle.truffle.js.runtime.Errors; |
61 | 61 | import com.oracle.truffle.js.runtime.JSAgentWaiterList; |
62 | 62 | import com.oracle.truffle.js.runtime.JSConfig; |
@@ -166,21 +166,28 @@ long getBufferSize() { |
166 | 166 |
|
167 | 167 | private void ensureNotDetached() throws IndexOutOfBoundsException { |
168 | 168 | if (isDetached()) { |
169 | | - throw DetachedBufferIndexOutOfBoundsException.INSTANCE; |
| 169 | + throw BufferIndexOutOfBoundsException.INSTANCE; |
170 | 170 | } |
171 | 171 | } |
172 | 172 |
|
173 | 173 | @ExportMessage |
174 | 174 | void readBuffer(long byteOffset, byte[] destination, int destinationOffset, int length) throws InvalidBufferOffsetException { |
175 | 175 | try { |
176 | 176 | ensureNotDetached(); |
177 | | - System.arraycopy(byteArray, Objects.checkFromIndexSize(Math.toIntExact(byteOffset), length, byteArray.length), |
178 | | - destination, Objects.checkFromIndexSize(destinationOffset, length, destination.length), length); |
| 177 | + System.arraycopy(byteArray, checkFromIndexSize(Math.toIntExact(byteOffset), length, byteArray.length), |
| 178 | + destination, checkFromIndexSize(destinationOffset, length, destination.length), length); |
179 | 179 | } catch (IndexOutOfBoundsException | ArithmeticException e) { |
180 | 180 | throw InvalidBufferOffsetException.create(byteOffset, length); |
181 | 181 | } |
182 | 182 | } |
183 | 183 |
|
| 184 | + private static int checkFromIndexSize(int fromIndex, int size, int length) { |
| 185 | + if ((length | fromIndex | size) < 0 || size > length - fromIndex) { |
| 186 | + throw BufferIndexOutOfBoundsException.INSTANCE; |
| 187 | + } |
| 188 | + return fromIndex; |
| 189 | + } |
| 190 | + |
184 | 191 | @ExportMessage |
185 | 192 | byte readBufferByte(long byteOffset) throws InvalidBufferOffsetException { |
186 | 193 | try { |
@@ -345,15 +352,15 @@ final long getBufferSize() { |
345 | 352 |
|
346 | 353 | private void ensureNotDetached() { |
347 | 354 | if (isDetached()) { |
348 | | - throw DetachedBufferIndexOutOfBoundsException.INSTANCE; |
| 355 | + throw BufferIndexOutOfBoundsException.INSTANCE; |
349 | 356 | } |
350 | 357 | } |
351 | 358 |
|
352 | 359 | @ExportMessage |
353 | 360 | final void readBuffer(long byteOffset, byte[] destination, int destinationOffset, int length) throws InvalidBufferOffsetException { |
354 | 361 | try { |
355 | 362 | ensureNotDetached(); |
356 | | - byteBuffer.get(Math.toIntExact(byteOffset), destination, destinationOffset, length); |
| 363 | + Boundaries.byteBufferGet(byteBuffer, Math.toIntExact(byteOffset), destination, destinationOffset, length); |
357 | 364 | } catch (IndexOutOfBoundsException | ArithmeticException e) { |
358 | 365 | throw InvalidBufferOffsetException.create(byteOffset, length); |
359 | 366 | } |
@@ -768,10 +775,10 @@ public static JSArrayBufferObject createInteropArrayBuffer(Shape shape, JSDynami |
768 | 775 | } |
769 | 776 |
|
770 | 777 | @SuppressWarnings("serial") |
771 | | - static final class DetachedBufferIndexOutOfBoundsException extends IndexOutOfBoundsException { |
772 | | - private static final IndexOutOfBoundsException INSTANCE = new DetachedBufferIndexOutOfBoundsException(); |
| 778 | + static final class BufferIndexOutOfBoundsException extends IndexOutOfBoundsException { |
| 779 | + private static final IndexOutOfBoundsException INSTANCE = new BufferIndexOutOfBoundsException(); |
773 | 780 |
|
774 | | - private DetachedBufferIndexOutOfBoundsException() { |
| 781 | + private BufferIndexOutOfBoundsException() { |
775 | 782 | } |
776 | 783 |
|
777 | 784 | @SuppressWarnings("sync-override") |
|
0 commit comments