Skip to content

Commit

Permalink
Make strLstDup() null-tolerant.
Browse files Browse the repository at this point in the history
Duping a NULL StringList without checking if it is NULL is a useful capability.
  • Loading branch information
dwsteele committed Mar 15, 2019
1 parent 9382283 commit 5554377
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 4 additions & 0 deletions doc/xml/release.xml
Expand Up @@ -32,6 +32,10 @@
<release-item>
<p>Add <id>CIFS</id> storage driver.</p>
</release-item>

<release-item>
<p>Make <code>strLstDup()</code> null-tolerant.</p>
</release-item>
</release-development-list>
</release-core-list>
</release>
Expand Down
21 changes: 12 additions & 9 deletions src/common/type/stringList.c
Expand Up @@ -238,18 +238,21 @@ strLstDup(const StringList *sourceList)
FUNCTION_TEST_PARAM(STRING_LIST, sourceList);
FUNCTION_TEST_END();

ASSERT(sourceList != NULL);

// Create the list
StringList *this = strLstNew();
StringList *this = NULL;

// Copy strings
MEM_CONTEXT_BEGIN(lstMemContext((List *)this))
if (sourceList != NULL)
{
for (unsigned int listIdx = 0; listIdx < strLstSize(sourceList); listIdx++)
strLstAddInternal(this, strDup(strLstGet(sourceList, listIdx)));
// Create the list
this = strLstNew();

// Copy strings
MEM_CONTEXT_BEGIN(lstMemContext((List *)this))
{
for (unsigned int listIdx = 0; listIdx < strLstSize(sourceList); listIdx++)
strLstAddInternal(this, strDup(strLstGet(sourceList, listIdx)));
}
MEM_CONTEXT_END();
}
MEM_CONTEXT_END();

FUNCTION_TEST_RETURN(this);
}
Expand Down
3 changes: 2 additions & 1 deletion test/src/module/common/typeStringTest.c
Expand Up @@ -390,7 +390,8 @@ testRun(void)

TEST_RESULT_STR(strPtr(strLstJoin(list, ", ")), "item1, item2, [NULL]", "list with NULL at end");

TEST_RESULT_STR(strPtr(strLstJoin(strLstDup(list), ", ")), "item1, item2, [NULL]", "dup'd list will NULL at end");
TEST_RESULT_STR(strPtr(strLstJoin(strLstDup(list), ", ")), "item1, item2, [NULL]", "dup'd list with NULL at end");
TEST_RESULT_PTR(strLstDup(NULL), NULL, "dup NULL list");

strLstFree(list);
}
Expand Down

0 comments on commit 5554377

Please sign in to comment.