diff options
author | Aditya Mandaleeka <adityamandaleeka@users.noreply.github.com> | 2016-05-04 18:50:00 -0700 |
---|---|---|
committer | Aditya Mandaleeka <adityamandaleeka@users.noreply.github.com> | 2016-05-04 18:50:00 -0700 |
commit | dc71a169507810c1dafad6fa20dc3881c6e4d4ec (patch) | |
tree | 531501aa10e48cbbcfb02c082a92bc401704f02b | |
parent | b889f007480b6818a5cb144e726d3e2940fd5cd3 (diff) | |
parent | e67df67213d1b4730d5d2c803c681bc55f009792 (diff) | |
download | coreclr-dc71a169507810c1dafad6fa20dc3881c6e4d4ec.tar.gz coreclr-dc71a169507810c1dafad6fa20dc3881c6e4d4ec.tar.bz2 coreclr-dc71a169507810c1dafad6fa20dc3881c6e4d4ec.zip |
Merge pull request #4765 from adityamandaleeka/array_alloc_bounds_check
Add fwrapv compile option
-rw-r--r-- | compileoptions.cmake | 4 | ||||
-rw-r--r-- | src/vm/gchelpers.cpp | 19 |
2 files changed, 6 insertions, 17 deletions
diff --git a/compileoptions.cmake b/compileoptions.cmake index bda438e050..9fcbb741d0 100644 --- a/compileoptions.cmake +++ b/compileoptions.cmake @@ -7,6 +7,10 @@ if (CLR_CMAKE_PLATFORM_UNIX) #-fms-compatibility Enable full Microsoft Visual C++ compatibility #-fms-extensions Accept some non-standard constructs supported by the Microsoft compiler + # Make signed arithmetic overflow of addition, subtraction, and multiplication wrap around + # using twos-complement representation (this is normally undefined according to the C++ spec). + add_compile_options(-fwrapv) + if(CLR_CMAKE_PLATFORM_DARWIN) # We cannot enable "stack-protector-strong" on OS X due to a bug in clang compiler (current version 7.0.2) add_compile_options(-fstack-protector) diff --git a/src/vm/gchelpers.cpp b/src/vm/gchelpers.cpp index 9b77b0ed96..bf81847716 100644 --- a/src/vm/gchelpers.cpp +++ b/src/vm/gchelpers.cpp @@ -350,28 +350,13 @@ OBJECTREF AllocateArrayEx(TypeHandle arrayType, INT32 *pArgs, DWORD dwNumArgs, B lowerBound = pArgs[i]; i++; } - int length = pArgs[i]; if (length < 0) COMPlusThrow(kOverflowException); - if ((SIZE_T)length > MaxArrayLength(componentSize)) - { - // This will cause us to throw below if we don't throw anything else before then. maxArrayDimensionLengthOverflow = true; - } - - if (length > 0) - { - int highestAllowableLowerBound = INT32_MAX - (length - 1); - if (lowerBound > highestAllowableLowerBound) - { - // We throw because the lower bound is large enough that the sum of the - // dimension's length and the lower bound would exceed INT32_MAX. - COMPlusThrow(kArgumentOutOfRangeException, W("ArgumentOutOfRange_ArrayLBAndLength")); - } - } - + if ((length > 0) && (lowerBound + (length - 1) < lowerBound)) + COMPlusThrow(kArgumentOutOfRangeException, W("ArgumentOutOfRange_ArrayLBAndLength")); safeTotalElements = safeTotalElements * S_UINT32(length); if (safeTotalElements.IsOverflow()) ThrowOutOfMemoryDimensionsExceeded(); |