09-07-2021 10:26 AM
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?
09-13-2021 05:27 AM
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;
}
09-15-2021 04:31 PM
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.