0

In the definition of the SAFEARRAY data structure cDims is listed as USHORT

typedef struct tagSAFEARRAY {
  USHORT         cDims;
  USHORT         fFeatures;
  ULONG          cbElements;
  ULONG          cLocks;
  PVOID          pvData;
  SAFEARRAYBOUND rgsabound[1];
} SAFEARRAY, *LPSAFEARRAY;

Both SafeArrayCreate and SafeArrayAllocDescriptor expect a UINT

SAFEARRAY* SafeArrayCreate(
  _In_ VARTYPE        vt,
  _In_ UINT           cDims,
  _In_ SAFEARRAYBOUND *rgsabound
);

What will happen if I pass a really large UINT to SafeArrayCreate? Is it a bug, what am I missing here?

drgs
  • 375
  • 2
  • 8
  • 1
    You should ask Microsoft for the internals of SafeArrayCreate(), and if it casts (narrowing) UINT to USHORT. Anyhow, if your arrays have less than 2^16 dimensions everything will be smooth. – Ripi2 Aug 03 '23 at 18:16
  • It does. Tried by trial an error and everything over 65536 throws an invalid parameter error. – drgs Aug 03 '23 at 19:06
  • 1
    This interop api also needs to be usable in languages that don't support unsigned types, like Python, Java, Basic. By making the integer promotion explicit, they can still create arrays with more than 32767 dimensions. – Hans Passant Aug 03 '23 at 20:54

0 Answers0