Browse Source

ENGINE-800: semantics for stringlist_append are now that if the second pointer is sequal to the first, we copy the list and append it. This matches the copy semantics of other-pointer appends.

audit_branch
parent
commit
c454df0521
3 changed files with 48 additions and 9 deletions
  1. +1
    -1
      src/pEpEngine.c
  2. +29
    -8
      src/stringlist.c
  3. +18
    -0
      test/src/StringlistTest.cc

+ 1
- 1
src/pEpEngine.c View File

@ -2760,7 +2760,7 @@ DYNAMIC_API PEP_STATUS log_event(
{
#if defined(_WIN32) && !defined(NDEBUG)
log_output_debug(title, entity, description, comment);
log_output_debug(title, entity, description, comment);
#endif
#if defined(ANDROID) && !defined(NDEBUG)


+ 29
- 8
src/stringlist.c View File

@ -144,27 +144,27 @@ DYNAMIC_API stringlist_t *stringlist_add_unique(
return result;
if (!stringlist)
return NULL; // If the previous call fails somehow. this code is bizarre.
return NULL; // If the previous call fails somehow. this code is bizarre.
stringlist_t* list_curr = stringlist;
stringlist_t** next_ptr_addr = NULL;
while (list_curr) {
next_ptr_addr = &list_curr->next;
if (strcmp(list_curr->value, value) == 0)
return list_curr;
list_curr = list_curr->next;
next_ptr_addr = &list_curr->next;
if (strcmp(list_curr->value, value) == 0)
return list_curr;
list_curr = list_curr->next;
}
if (!next_ptr_addr)
return NULL; // This is an error, because stringlist_add_first should
// have handled this case
return NULL; // This is an error, because stringlist_add_first should
// have handled this case
*next_ptr_addr = new_stringlist(value);
if (!*next_ptr_addr)
return NULL;
return NULL;
return *next_ptr_addr;
@ -185,6 +185,27 @@ DYNAMIC_API stringlist_t *stringlist_append(
return stringlist;
stringlist_t *_s = stringlist;
if (stringlist == second) {
// Passing in the same pointer twice is not cool.
// Since the semantics are to copy the second list,
// we'll just do that, presuming that the
// caller wants this.
second = stringlist_dup(stringlist);
stringlist_t** end_ptr = NULL;
while (_s) {
end_ptr = &_s->next;
_s = _s->next;
}
if (!end_ptr)
return NULL;
*end_ptr = second;
return stringlist;
}
stringlist_t *_s2;
for (_s2 = second; _s2 != NULL; _s2 = _s2->next) {
_s = stringlist_add(_s, _s2->value);


+ 18
- 0
test/src/StringlistTest.cc View File

@ -365,3 +365,21 @@ TEST_F(StringlistTest, check_string_to_stringlist_commas_to_two) {
ASSERT_STREQ(sl->value, str0);
ASSERT_STREQ(sl->next->value, str1);
}
TEST_F(StringlistTest, check_stringlist_append_self) {
const char* str0 = "I am so tired";
const char* str1 = "of doing stuff";
const char* str2 = "Bob";
stringlist_t* s1 = new_stringlist(str0);
stringlist_add(s1, str1);
stringlist_add(s1, str2);
ASSERT_EQ(stringlist_length(s1), 3);
stringlist_t* s2 = stringlist_append(s1, s1);
ASSERT_EQ(s1, s2);
ASSERT_EQ(stringlist_length(s1), 6);
ASSERT_STREQ(str0, s1->next->next->next->value);
ASSERT_STREQ(str1, s1->next->next->next->next->value);
ASSERT_STREQ(str2, s1->next->next->next->next->next->value);
}

Loading…
Cancel
Save