Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArrayIndexOutOfBoundsException in SafeArray.toVariantArray() #42

Closed
EJP286CRSKW opened this issue Sep 22, 2023 · 1 comment
Closed

ArrayIndexOutOfBoundsException in SafeArray.toVariantArray() #42

EJP286CRSKW opened this issue Sep 22, 2023 · 1 comment

Comments

@EJP286CRSKW
Copy link

EJP286CRSKW commented Sep 22, 2023

This method reads as follows:

JNIEXPORT jobjectArray JNICALL Java_com_jacob_com_SafeArray_toVariantArray
  (JNIEnv *env, jobject _this)
{
  SAFEARRAY *sa = extractSA(env, _this);
  if (!sa) {
    ThrowComFail(env, "safearray object corrupted", -1);
    return NULL;
  }
  long lb, ub;
  SafeArrayGetLBound(sa, 1, &lb);
  SafeArrayGetUBound(sa, 1, &ub);
  int num = ub - lb + 1;
  jclass vClass = env->FindClass("com/jacob/com/Variant");
  // create an array of Variant's
  jobjectArray varr = env->NewObjectArray(num, vClass, 0);
  // fill them in
  jmethodID variantCons =
         env->GetMethodID(vClass, "<init>", "()V");
  for(int i=lb;i<=ub;i++) {
    long ix = i;
    // construct a variant to return
    jobject newVariant = env->NewObject(vClass, variantCons);
    // get the VARIANT from the newVariant
    VARIANT *v = extractVariant(env, newVariant);
    SafeArrayGetElement(sa, &ix, (void*) v);
    // put in object array
    env->SetObjectArrayElement(varr, i, newVariant);
  }
  return varr;
}

The problem is the variable i in the SetObjectArrayElement() call. It is running from lower to upper bound of the SafeArray, which is at least 1 for the lower bound, possibly a much larger number. Basically the issue is that SafeArray is indexed from at least 1 and Java arrays are indexed from zero.

Solution:

    // put in object array
    env->SetObjectArrayElement(varr, i-lb, newVariant); // Use 'i-lb' here, not 'i', for 0-relative Java array.
@freemansoft
Copy link
Owner

This change has been pushed into the source for some release in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants