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

Incorrect element-count calculation in SafeArray.cpp numElements() cause 2D and higher SafeArrays to fail #48

Closed
EJP286CRSKW opened this issue Nov 7, 2023 · 1 comment

Comments

@EJP286CRSKW
Copy link

EJP286CRSKW commented Nov 7, 2023

static int numElements(SAFEARRAY *psa)
{
  int nDims = SafeArrayGetDim(psa);
  int elems = 0;
  for(int i=1;i<=nDims;i++) {
    long lb, ub;
    SafeArrayGetLBound(psa, i, &lb);
    SafeArrayGetUBound(psa, i, &ub);
    elems += ub - lb + 1;
  }
  return elems;
}

This calculation is incorrect, as the number of elements in an mxn array is mxn, not m+n. It happens to be correct in the 1D case only, but not beyond. The effect of this is that fromXXXArray() and toXXXArray() don't work for 2D and higher.

Solution:

static int numElements(SAFEARRAY *psa)
{
  int nDims = SafeArrayGetDim(psa);
  int elems = 0;
  for(int i=1;i<=nDims;i++) {
    long lb, ub;
    SafeArrayGetLBound(psa, i, &lb);
    SafeArrayGetUBound(psa, i, &ub);
    if (elems == 0)
        elems = 1;
    elems *= ub - lb + 1;
  }
  return elems;
}
@EJP286CRSKW EJP286CRSKW changed the title Incorrect and pointless calculations in SafeArray.coo numElements() Incorrect and pointless calculations in SafeArray.cpp numElements() Nov 7, 2023
@EJP286CRSKW EJP286CRSKW changed the title Incorrect and pointless calculations in SafeArray.cpp numElements() Incorrect element-count calculation in SafeArray.cpp numElements() Dec 1, 2023
@EJP286CRSKW EJP286CRSKW changed the title Incorrect element-count calculation in SafeArray.cpp numElements() Incorrect element-count calculation in SafeArray.cpp numElements() cause 2D and higher SafeArrays to fail Dec 1, 2023
@freemansoft
Copy link
Owner

Merged d69e34c

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