LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Dereference of out-of-bounds pointer error when accessing an array in a very large structure

In LabWindows/CVI 2020 (and 2019, 2015), I have a very large structure that has an array, and the offset of that array within the structure is at or beyond 2^29 (512 MiB).  If I allocate an instance of that structure and attempt to access the aforementioned array, I get a dereference of out of bounds pointer error in a debug build with resource tracking enabled.

 

For example, the following code has an issue at the "t->val[0] = 2.4;" line:

 

#include <ansi_c.h>

typedef struct _S
{ char a[(512*1024*1024)];
  double val[1];       
} S;

int main (int argc, char *argv[])
{ S *t = malloc (sizeof (S));
  t->val[0] = 2.4;
  free (t);
  return 0;
}

 

If I make the a field slightly smaller, the issue goes away:

 

#include <ansi_c.h>

typedef struct _S
{ char a[(512*1024*1024)-8];
  double val[1];       
} S;

int main (int argc, char *argv[])
{ S *t = malloc (sizeof (S));
  t->val[0] = 2.4;
  free (t);
  return 0;
}

 

It doesn't matter if the target is 32-bit or 64-bit.

 

Is this a known issue?  

0 Kudos
Message 1 of 3
(1,865 Views)

the following works:

 

#include <ansi_c.h>

#define A_SIZE 512*1024*1024

typedef struct _S
{
char *a;
double val[1];
} S;

int main (int argc, char *argv[])
{
S t = {0} ;
t.a = malloc(A_SIZE) ;
t.val[0] = 2.4;
free (t.a);
return 0;
}

0 Kudos
Message 2 of 3
(1,815 Views)

I may end up doing that to get the runtime bounds checking working again.  To quickly get around the issue, I made a malloc wrapper that looks something like:

 

void * umalloc (size_t elements, size_t sizeOfElement)
{
  void * ret = malloc (elements, sizeOfElement);
  return ret;
}

 

If the return value of malloc is first assigned to a variable of type void *, the runtime array bounds checks are not enabled for the block of memory.  It's not optimal, but it at least I have bounds checking for everything else in the program besides this large structure.

0 Kudos
Message 3 of 3
(1,798 Views)