util: alloc: Introduce freeing helpers that clear the memory before freeing

For a few cases where we handle secret information it's good to clear
the buffers containing sensitive data before freeing them.

Introduce VIR_DISPOSE, VIR_DISPOSE_N and VIR_DISPOSE_STRING that allow
simple clearing fo the buffers holding sensitive information on cleanup
paths.
This commit is contained in:
Peter Krempa
2016-05-13 14:59:01 +02:00
parent ced1e846a0
commit eb2116fd9a
4 changed files with 130 additions and 0 deletions

View File

@@ -383,6 +383,41 @@ testInsertArray(const void *opaque ATTRIBUTE_UNUSED)
}
static int
testDispose(const void *opaque ATTRIBUTE_UNUSED)
{
int *num = NULL;
int *nums = NULL;
size_t nnums = 0;
char *str = NULL;
VIR_DISPOSE(num);
VIR_DISPOSE_N(nums, nnums);
VIR_DISPOSE_STRING(str);
nnums = 10;
VIR_DISPOSE_N(nums, nnums);
if (VIR_ALLOC(num) < 0)
return -1;
VIR_DISPOSE(num);
nnums = 10;
if (VIR_ALLOC_N(nums, nnums) < 0)
return -1;
VIR_DISPOSE_N(nums, nnums);
if (VIR_STRDUP(str, "test") < 0)
return -1;
VIR_DISPOSE_STRING(str);
return 0;
}
static int
mymain(void)
{
@@ -400,6 +435,8 @@ mymain(void)
ret = -1;
if (virtTestRun("insert array", testInsertArray, NULL) < 0)
ret = -1;
if (virtTestRun("dispose tests", testDispose, NULL) < 0)
ret = -1;
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}