Skip to content

Commit c96b879

Browse files
committed
Now errors when overflowing the fixed sized array buffer #11
1 parent 06d2ddd commit c96b879

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

include/javaproxy.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ JNIEnv *g_env;
1616
JavaVM *g_jvm;
1717
jobject g_activity;
1818

19-
#define ARRAY_SIZE 10
19+
#define ARRAY_SIZE 32
2020

2121
#if defined(ANDROID_MODULE)
2222
#define attachCurrentThread() g_jvm->AttachCurrentThread(&g_env, nullptr)
@@ -288,15 +288,17 @@ struct JavaProxy {
288288
// int readWrite(int address, byte[] write) {
289289
int invokeReadWrite(int argc, slib_par_t *arg, var_s *retval) {
290290
int result = 0;
291-
if (_instance != nullptr) {
291+
int writeLen = populateByteArray(argc, arg, 2);
292+
if (writeLen > ARRAY_SIZE) {
293+
error(retval, "write array", 1, ARRAY_SIZE);
294+
} else if (_instance != nullptr) {
292295
attachCurrentThread();
293296
jmethodID method = g_env->GetMethodID(_clazz, "readWrite", "(II[BI)J");
294297
var_int_t value = 0;
295298
if (method != nullptr) {
296299
auto address = get_param_int(argc, arg, 0, 0);
297300
auto readBytes = get_param_int(argc, arg, 1, 2);
298-
populateByteArray(argc, arg, 2);
299-
value = g_env->CallIntMethod(_instance, method, address, readBytes, _array, argc - 1);
301+
value = g_env->CallIntMethod(_instance, method, address, readBytes, _array, writeLen);
300302
}
301303
if (!checkException(retval)) {
302304
v_setint(retval, value);
@@ -310,13 +312,15 @@ struct JavaProxy {
310312
// int write(int address, byte[] write) {
311313
int invokeWrite(int argc, slib_par_t *arg, var_s *retval) {
312314
int result = 0;
313-
if (_instance != nullptr) {
315+
int writeLen = populateByteArray(argc, arg, 1);
316+
if (writeLen > ARRAY_SIZE) {
317+
error(retval, "write array", 1, ARRAY_SIZE);
318+
} else if (_instance != nullptr) {
314319
attachCurrentThread();
315320
jmethodID method = g_env->GetMethodID(_clazz, "write", "(I[BI)V");
316321
if (method != nullptr) {
317322
auto address = get_param_int(argc, arg, 0, 0);
318-
populateByteArray(argc, arg, 1);
319-
g_env->CallVoidMethod(_instance, method, address, _array, argc - 1);
323+
g_env->CallVoidMethod(_instance, method, address, _array, writeLen);
320324
}
321325
if (!checkException(retval)) {
322326
result = 1;
@@ -327,7 +331,8 @@ struct JavaProxy {
327331
}
328332

329333
// populate the java byte array with the contents of the basic array
330-
void populateByteArray(int argc, slib_par_t *params, int offset) {
334+
int populateByteArray(int argc, slib_par_t *params, int offset) {
335+
int result;
331336
if (!_array) {
332337
_array = g_env->NewByteArray(ARRAY_SIZE);
333338
}
@@ -340,13 +345,16 @@ struct JavaProxy {
340345
var_s *elem = v_elem(array, i);
341346
elements[i] = v_is_type(elem, V_INT) ? elem->v.i : elem->v.n;
342347
}
348+
result = size;
343349
} else {
344350
for (int i = offset, j = 0; i < argc && i < ARRAY_SIZE; i++, j++) {
345351
elements[j] = get_param_int(argc, params, i, 0);
346352
}
353+
result = argc - 1;
347354
}
348355
// make the changes available to the java side
349356
g_env->ReleaseByteArrayElements(_array, elements, 0);
357+
return result;
350358
}
351359

352360
protected:

ioio/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static int cmd_twimaster_write(var_s *self, int argc, slib_par_t *arg, var_s *re
9393
static int cmd_spimaster_write(var_s *self, int argc, slib_par_t *arg, var_s *retval) {
9494
int result = 0;
9595
if (argc != 2) {
96-
error(retval, "SpiMaster.write", ARRAY_SIZE);
96+
error(retval, "SpiMaster.write", 2, ARRAY_SIZE);
9797
} else {
9898
int id = get_io_class_id(self, retval);
9999
if (id != -1) {

0 commit comments

Comments
 (0)