Boundary check for variable length arrays (VLA)?

Is there a way to check buffer overflow in VLA? I used -fstack-protector-all -Wstack-protector but received the following warnings:

warning: do not protect local variables: variable-length buffer

Is there a library for this? (-execute for heap memory)

I am currently using Valgrind and gdb.

+5
source share
3 answers

You can use -fmudflap instead of -fstack-protector-all

Update: some documents and parameters are here http://gcc.gnu.org/wiki/Mudflap_Pointer_Debugging

+1
source

, alloca() . , c99 , GCC, , , , alloca().

, - , , .

0

, ; , - , "" . malloc() , .

, , , , . .

- :

void work(int n)
{
  int data[n];   /* Our variable-length array. */

  data[0] = 0;
}

- :

#include "vla-tracking.h"

void work(int n)
{
  VLA_NEW(int, data, n);  /* Our variable-length array. */

  VLA_SET(data, 0, 0);
}

Then create the appropriate macro definitions (and helper code) to track access. As I said, it will be ugly. Of course, the idea is that macros can only “compile” simple definitions controlled by some build time settings (debug / release mode or something else).

0
source

All Articles