summaryrefslogtreecommitdiff
path: root/libjava/prims.cc
diff options
context:
space:
mode:
authorralph <ralph@138bc75d-0d04-0410-961f-82ee72b054a4>2003-10-16 21:19:53 +0000
committerralph <ralph@138bc75d-0d04-0410-961f-82ee72b054a4>2003-10-16 21:19:53 +0000
commit566cfa76991b51e1fbac20cc23def73f3f8c5366 (patch)
tree5eb019a86e608ffec1700a4faa313de2a574966e /libjava/prims.cc
parenta0a4226a8599e38a66023813b78fc47a7c7b61c7 (diff)
downloadgcc-566cfa76991b51e1fbac20cc23def73f3f8c5366.tar.gz
* prims.cc (_Jv_NewObjectArray): Make sure byte size doesn't
overflow a jint. (_Jv_NewPrimArray): Check for overflowing a jint, replacing a check for overflowing size_t, since the lower level functions take a jint. * testsuite/libjava.lang/newarray_overflow.java: New file. * testsuite/libjava.lang/newarray_overflow.out: New file. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@72577 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libjava/prims.cc')
-rw-r--r--libjava/prims.cc13
1 files changed, 10 insertions, 3 deletions
diff --git a/libjava/prims.cc b/libjava/prims.cc
index 71cd8c3ece0..26e88329ed5 100644
--- a/libjava/prims.cc
+++ b/libjava/prims.cc
@@ -70,8 +70,10 @@ details. */
// around for use if we run out of memory.
static java::lang::OutOfMemoryError *no_memory;
-// Largest representable size_t.
-#define SIZE_T_MAX ((size_t) (~ (size_t) 0))
+// Number of bytes in largest array object we create. This could be
+// increased to the largest size_t value, so long as the appropriate
+// functions are changed to take a size_t argument instead of jint.
+#define MAX_OBJECT_SIZE ((1<<31) - 1)
static const char *no_properties[] = { NULL };
@@ -481,6 +483,11 @@ _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
// Ensure that elements pointer is properly aligned.
jobjectArray obj = NULL;
size_t size = (size_t) elements (obj);
+ // Check for overflow.
+ if (__builtin_expect ((size_t) count >
+ (MAX_OBJECT_SIZE - 1 - size) / sizeof (jobject), false))
+ throw no_memory;
+
size += count * sizeof (jobject);
jclass klass = _Jv_GetArrayClass (elementClass,
@@ -516,7 +523,7 @@ _Jv_NewPrimArray (jclass eltype, jint count)
// Check for overflow.
if (__builtin_expect ((size_t) count >
- (SIZE_T_MAX - size) / elsize, false))
+ (MAX_OBJECT_SIZE - size) / elsize, false))
throw no_memory;
jclass klass = _Jv_GetArrayClass (eltype, 0);