|
29 | 29 | #include <PiscesSysutils.h>
|
30 | 30 |
|
31 | 31 | #include <PiscesSurface.inl>
|
| 32 | +#include <limits.h> |
32 | 33 |
|
33 | 34 | #define SURFACE_NATIVE_PTR 0
|
34 | 35 | #define SURFACE_LAST SURFACE_NATIVE_PTR
|
@@ -74,12 +75,33 @@ Java_com_sun_pisces_AbstractSurface_getRGBImpl(JNIEnv* env, jobject objectHandle
|
74 | 75 | (*env)->GetLongField(env, objectHandle,
|
75 | 76 | fieldIds[SURFACE_NATIVE_PTR]));
|
76 | 77 |
|
77 |
| - CORRECT_DIMS(surface, x, y, width, height, dstX, dstY); |
| 78 | + if (surface == NULL) { |
| 79 | + JNI_ThrowNew(env, "java/lang/IllegalArgumentException", "Invalid surface"); |
| 80 | + return; |
| 81 | + } |
| 82 | + int surfaceWidth = surface->width; |
| 83 | + int surfaceHeight = surface->height; |
| 84 | + if (x < 0 || x >= surfaceWidth || |
| 85 | + y < 0 || y >= surfaceHeight || |
| 86 | + width < 0 || width > (surfaceWidth - x) || |
| 87 | + height < 0 || height > (surfaceHeight - y) || |
| 88 | + scanLength < width) { |
| 89 | + JNI_ThrowNew(env, "java/lang/IllegalArgumentException", "Illegal arguments"); |
| 90 | + return; |
| 91 | + } |
78 | 92 |
|
79 | 93 | if ((width > 0) && (height > 0)) {
|
80 | 94 | jint* dstData;
|
81 | 95 | jsize dstDataLength = (*env)->GetArrayLength(env, arrayHandle);
|
| 96 | + if (dstY > ((INT_MAX - offset - dstX) / scanLength)) { |
| 97 | + JNI_ThrowNew(env, "java/lang/IllegalArgumentException", "Out of bounds offset or scan length"); |
| 98 | + return; |
| 99 | + } |
82 | 100 | jint dstStart = offset + dstY * scanLength + dstX;
|
| 101 | + if (scanLength > ((INT_MAX - dstStart) / height)) { |
| 102 | + JNI_ThrowNew(env, "java/lang/IllegalArgumentException", "Out of bounds offset or scan length"); |
| 103 | + return; |
| 104 | + } |
83 | 105 | jint dstEnd = dstStart + height * scanLength - 1;
|
84 | 106 | if ((dstStart < 0) || (dstStart >= dstDataLength) || (dstEnd < 0) || (dstEnd >= dstDataLength)) {
|
85 | 107 | JNI_ThrowNew(env, "java/lang/IllegalArgumentException", "Out of range access of buffer");
|
@@ -130,12 +152,35 @@ Java_com_sun_pisces_AbstractSurface_setRGBImpl(JNIEnv* env, jobject objectHandle
|
130 | 152 | (*env)->GetLongField(env, objectHandle,
|
131 | 153 | fieldIds[SURFACE_NATIVE_PTR]));
|
132 | 154 |
|
133 |
| - CORRECT_DIMS(surface, x, y, width, height, srcX, srcY); |
| 155 | + if (surface == NULL) { |
| 156 | + JNI_ThrowNew(env, "java/lang/IllegalArgumentException", "Invalid surface"); |
| 157 | + return; |
| 158 | + } |
| 159 | + int surfaceWidth = surface->width; |
| 160 | + int surfaceHeight = surface->height; |
| 161 | + if (x < 0 || x >= surfaceWidth || |
| 162 | + y < 0 || y >= surfaceHeight || |
| 163 | + width < 0 || width > (surfaceWidth - x) || |
| 164 | + height < 0 || height > (surfaceHeight - y) || |
| 165 | + scanLength < width) { |
| 166 | + JNI_ThrowNew(env, "java/lang/IllegalArgumentException", "Illegal arguments"); |
| 167 | + return; |
| 168 | + } |
134 | 169 |
|
135 | 170 | if ((width > 0) && (height > 0)) {
|
136 | 171 | jint* srcData;
|
137 | 172 | jsize srcDataLength = (*env)->GetArrayLength(env, arrayHandle);
|
| 173 | + |
| 174 | + if (srcY > ((INT_MAX - offset - srcX) / scanLength)) { |
| 175 | + JNI_ThrowNew(env, "java/lang/IllegalArgumentException", "Out of bounds offset or scan length"); |
| 176 | + return; |
| 177 | + } |
138 | 178 | jint srcStart = offset + srcY * scanLength + srcX;
|
| 179 | + |
| 180 | + if (scanLength > ((INT_MAX - srcStart) / height)) { |
| 181 | + JNI_ThrowNew(env, "java/lang/IllegalArgumentException", "Out of bounds offset or scan length"); |
| 182 | + return; |
| 183 | + } |
139 | 184 | jint srcEnd = srcStart + height * scanLength - 1;
|
140 | 185 | if ((srcStart < 0) || (srcStart >= srcDataLength) || (srcEnd < 0) || (srcEnd >= srcDataLength)) {
|
141 | 186 | JNI_ThrowNew(env, "java/lang/IllegalArgumentException", "out of range access of buffer");
|
|
0 commit comments