mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user