@@ -1043,8 +1043,11 @@ private void checkWritebackEnabled() {
1043
1043
* This constant differs from all results that will ever be returned from
1044
1044
* {@link #staticFieldOffset}, {@link #objectFieldOffset},
1045
1045
* or {@link #arrayBaseOffset}.
1046
+ * <p>
1047
+ * The static type is @code long} to emphasize that long arithmetics should
1048
+ * always be used for offset calculations to avoid overflows.
1046
1049
*/
1047
- public static final int INVALID_FIELD_OFFSET = -1 ;
1050
+ public static final long INVALID_FIELD_OFFSET = -1 ;
1048
1051
1049
1052
/**
1050
1053
* Reports the location of a given field in the storage allocation of its
@@ -1172,11 +1175,15 @@ public void ensureClassInitialized(Class<?> c) {
1172
1175
* for the same class, you may use that scale factor, together with this
1173
1176
* base offset, to form new offsets to access elements of arrays of the
1174
1177
* given class.
1178
+ * <p>
1179
+ * The return value is in the range of a {@code int}. The return type is
1180
+ * {@code long} to emphasize that long arithmetics should always be used
1181
+ * for offset calculations to avoid overflows.
1175
1182
*
1176
1183
* @see #getInt(Object, long)
1177
1184
* @see #putInt(Object, long, int)
1178
1185
*/
1179
- public int arrayBaseOffset (Class <?> arrayClass ) {
1186
+ public long arrayBaseOffset (Class <?> arrayClass ) {
1180
1187
if (arrayClass == null ) {
1181
1188
throw new NullPointerException ();
1182
1189
}
@@ -1227,6 +1234,9 @@ public int arrayBaseOffset(Class<?> arrayClass) {
1227
1234
* will generally not work properly with accessors like {@link
1228
1235
* #getByte(Object, long)}, so the scale factor for such classes is reported
1229
1236
* as zero.
1237
+ * <p>
1238
+ * The computation of the actual memory offset should always use {@code
1239
+ * long} arithmetics to avoid overflows.
1230
1240
*
1231
1241
* @see #arrayBaseOffset
1232
1242
* @see #getInt(Object, long)
@@ -3840,7 +3850,7 @@ private void putShortParts(Object o, long offset, byte i0, byte i1) {
3840
3850
private native Object staticFieldBase0 (Field f );
3841
3851
private native boolean shouldBeInitialized0 (Class <?> c );
3842
3852
private native void ensureClassInitialized0 (Class <?> c );
3843
- private native int arrayBaseOffset0 (Class <?> arrayClass );
3853
+ private native int arrayBaseOffset0 (Class <?> arrayClass ); // public version returns long to promote correct arithmetics
3844
3854
private native int arrayIndexScale0 (Class <?> arrayClass );
3845
3855
private native int getLoadAverage0 (double [] loadavg , int nelems );
3846
3856
0 commit comments