From 156644b8e5581703202d0839805db8a275e3f890 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Tue, 26 Jun 2012 10:43:47 -0600 Subject: [PATCH] fix incorrect array sizing in populateMultiArray We were assuming the array element size was always the native word size, which is not correct in general for primitive arrays, and this led to wasted space at best and memory corruption at worst. --- src/machine.cpp | 5 ++++- test/Floats.java | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/machine.cpp b/src/machine.cpp index 67f70196fe..964bfd5452 100644 --- a/src/machine.cpp +++ b/src/machine.cpp @@ -4889,7 +4889,10 @@ populateMultiArray(Thread* t, object array, int32_t* counts, PROTECT(t, class_); for (int32_t i = 0; i < counts[index]; ++i) { - object a = makeArray(t, counts[index + 1]); + object a = makeArray + (t, ceiling + (counts[index + 1] * classArrayElementSize(t, class_), BytesPerWord)); + arrayLength(t, a) = counts[index + 1]; setObjectClass(t, a, class_); set(t, array, ArrayBody + (i * BytesPerWord), a); diff --git a/test/Floats.java b/test/Floats.java index 2e869a9a87..723b899a1a 100644 --- a/test/Floats.java +++ b/test/Floats.java @@ -260,5 +260,17 @@ public class Floats { int result = Float.floatToIntBits(number); expect(result == orig); } + + for (int x = 0; x < 1000; ++x) { + int m = 100; + int n = 200; + double array[][] = new double[m][n]; + + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + array[i][j] = 1234567890.0; + } + } + } } }