I would be interested to know if its possible to explicitly taint a variable in C, as being uninitialized.
Pseudo code...
{
int *array;
array = some_alloc();
b = array[0];
some_free(array);
TAINT_MACRO(array);
/* the compiler should raise an uninitialized warning here */
b = array[0];
}
Here is one example of one way to taint a variable, but GCC is raising a warning when 'a' is assigned the uninitialized var, rather then the second use of 'a'.
{
int a = 10;
printf("first %d\n", a);
do {
int b;
a = b;
} while(0);
printf("second %d\n", a);
}
The only solution I could come up with is to explicitly shadow the variable with an uninitialized one, (voids are added so there are no unused warnings).
#define TAINT_MACRO_BEGIN(array) (void)(array); { void **array; (void)array;
#define TAINT_MACRO_END(array) } (void)(array);
{
int *array;
array = some_alloc();
b = array[0];
some_free(array);
TAINT_MACRO_BEGIN(array);
/* the compiler should raise an uninitialized warning here */
b = array[0];
TAINT_MACRO_END(array);
}
This method adds too much overhead to include in existing code (adds a lot of noise and annoying to maintain), so I was wondering if there was some other way to tell the compiler a variable is uninitialized.
I know there are static checkers and I do use these, but Im looking for something the that can give a warning at compile time and without false positives which I believe is possible in this case and can avoid a certain class of bugs.