mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-22 05:35:25 +00:00
util: string: Add helper to free non-NULL terminated string arrays
To free string lists with some strings stolen from the middle we need to walk the complete array. Introduce a new helper that takes the string list size to free such string lists.
This commit is contained in:
parent
48d81cef3b
commit
5d4a482584
@ -1924,6 +1924,7 @@ virStrcpy;
|
||||
virStrdup;
|
||||
virStringArrayHasString;
|
||||
virStringFreeList;
|
||||
virStringFreeListCount;
|
||||
virStringJoin;
|
||||
virStringListLength;
|
||||
virStringReplace;
|
||||
|
@ -187,6 +187,26 @@ void virStringFreeList(char **strings)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* virStringFreeListCount:
|
||||
* @strings: array of strings to free
|
||||
* @count: number of elements in the array
|
||||
*
|
||||
* Frees a string array of @count length.
|
||||
*/
|
||||
void
|
||||
virStringFreeListCount(char **strings,
|
||||
size_t count)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < count; i++)
|
||||
VIR_FREE(strings[i]);
|
||||
|
||||
VIR_FREE(strings);
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
virStringArrayHasString(char **strings, const char *needle)
|
||||
{
|
||||
|
@ -42,6 +42,7 @@ char *virStringJoin(const char **strings,
|
||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
|
||||
|
||||
void virStringFreeList(char **strings);
|
||||
void virStringFreeListCount(char **strings, size_t count);
|
||||
|
||||
bool virStringArrayHasString(char **strings, const char *needle);
|
||||
|
||||
|
@ -497,6 +497,26 @@ testStringToLong(const void *opaque)
|
||||
}
|
||||
|
||||
|
||||
/* The point of this test is to check whether all members of the array are
|
||||
* freed. The test has to be checked using valgrind. */
|
||||
static int
|
||||
testVirStringFreeListCount(const void *opaque ATTRIBUTE_UNUSED)
|
||||
{
|
||||
char **list;
|
||||
|
||||
if (VIR_ALLOC_N(list, 4) < 0)
|
||||
return -1;
|
||||
|
||||
ignore_value(VIR_STRDUP(list[0], "test1"));
|
||||
ignore_value(VIR_STRDUP(list[2], "test2"));
|
||||
ignore_value(VIR_STRDUP(list[3], "test3"));
|
||||
|
||||
virStringFreeListCount(list, 4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
mymain(void)
|
||||
{
|
||||
@ -701,6 +721,11 @@ mymain(void)
|
||||
TEST_STRTOL("-18446744073709551616", NULL, 0, -1, 0U, -1,
|
||||
0LL, -1, 0ULL, -1);
|
||||
|
||||
/* test virStringFreeListCount */
|
||||
if (virtTestRun("virStringFreeListCount", testVirStringFreeListCount,
|
||||
NULL) < 0)
|
||||
ret = -1;
|
||||
|
||||
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user