fix #104: fixed incorrect/confusing behaviour in stringlist_add/append stringpair_list_add/append.
authorKrista Grothoff <krista@pep-project.org>
Tue, 12 Jul 2016 13:32:55 +0200
changeset 843b1695eef237b
parent 841 bb25b36f52d6
child 844 8e7266b5a89c
fix #104: fixed incorrect/confusing behaviour in stringlist_add/append stringpair_list_add/append.
src/stringlist.c
src/stringpair.c
test/stringlist_test.cc
     1.1 --- a/src/stringlist.c	Tue Jul 12 12:29:10 2016 +0200
     1.2 +++ b/src/stringlist.c	Tue Jul 12 13:32:55 2016 +0200
     1.3 @@ -19,8 +19,9 @@
     1.4              free(result);
     1.5              return NULL;
     1.6          }
     1.7 -        result->next = NULL; // needed for one-element lists
     1.8      }
     1.9 +    
    1.10 +    result->next = NULL; // needed for one-element lists
    1.11  
    1.12      return result;
    1.13  }
    1.14 @@ -54,25 +55,29 @@
    1.15  {  
    1.16      assert(value);
    1.17  
    1.18 +    // empty list (no nodes)
    1.19      if (stringlist == NULL)
    1.20          return new_stringlist(value);
    1.21  
    1.22 +    // empty list (one node, no value)
    1.23 +    if (stringlist->value == NULL) {
    1.24 +        if (stringlist->next) 
    1.25 +            return NULL; // invalid list
    1.26 +            
    1.27 +        stringlist->value = strdup(value);
    1.28 +        assert(stringlist->value);
    1.29 +        
    1.30 +        if (stringlist->value == NULL)
    1.31 +            return NULL;
    1.32 +        
    1.33 +        return stringlist;
    1.34 +    }
    1.35 +    
    1.36      stringlist_t* list_curr = stringlist;
    1.37 -    
    1.38 +
    1.39      while (list_curr->next)
    1.40          list_curr = list_curr->next;
    1.41 - 
    1.42 -    // if list end exists without value,
    1.43 -    // we fill it in here instead of adding
    1.44 -    // a new node.
    1.45 -    if (list_curr->value == NULL) {
    1.46 -        list_curr->value = strdup(value);
    1.47 -        assert(list_curr->value);
    1.48 -        if (list_curr->value == NULL)
    1.49 -            return NULL;
    1.50 -        return list_curr;
    1.51 -    }
    1.52 -    
    1.53 +     
    1.54      list_curr->next = new_stringlist(value);
    1.55  
    1.56      assert(list_curr->next);
    1.57 @@ -91,6 +96,7 @@
    1.58      if (stringlist == NULL)
    1.59          return NULL;
    1.60  
    1.61 +    // Second list is empty
    1.62      if (second == NULL || second->value == NULL)
    1.63          return stringlist;
    1.64  
     2.1 --- a/src/stringpair.c	Tue Jul 12 12:29:10 2016 +0200
     2.2 +++ b/src/stringpair.c	Tue Jul 12 13:32:55 2016 +0200
     2.3 @@ -47,6 +47,9 @@
     2.4  DYNAMIC_API stringpair_t * stringpair_dup(const stringpair_t *src)
     2.5  {
     2.6      assert(src);
     2.7 +    if (src == NULL)
     2.8 +        return NULL;
     2.9 +    
    2.10      return new_stringpair(src->key, src->value);
    2.11  }
    2.12  
    2.13 @@ -98,25 +101,29 @@
    2.14  {
    2.15      assert(value);
    2.16  
    2.17 +    // empty list (no nodes)
    2.18      if (stringpair_list == NULL)
    2.19          return new_stringpair_list(value);
    2.20  
    2.21 +    // empty list (one node, no value)
    2.22 +    if (stringpair_list->value == NULL) {
    2.23 +        if (stringpair_list->next)
    2.24 +            return NULL; // invalid list
    2.25 +            
    2.26 +        stringpair_list->value = value;
    2.27 +        assert(stringpair_list->value);
    2.28 +        
    2.29 +        if (stringpair_list->value == NULL)
    2.30 +            return NULL;
    2.31 +        
    2.32 +        return stringpair_list;
    2.33 +    }
    2.34 +    
    2.35      stringpair_list_t* list_curr = stringpair_list;
    2.36      
    2.37      while (list_curr->next)
    2.38          list_curr = list_curr->next;
    2.39 - 
    2.40 -    // if list end exists without value,
    2.41 -    // we fill it in here instead of adding
    2.42 -    // a new node.
    2.43 -    if (list_curr->value == NULL) {
    2.44 -        list_curr->value = value; // ownership goes to us
    2.45 -        assert(list_curr->value);
    2.46 -        if (list_curr->value == NULL)
    2.47 -            return NULL;
    2.48 -        return list_curr;
    2.49 -    }
    2.50 -    
    2.51 +     
    2.52      list_curr->next = new_stringpair_list(value);
    2.53  
    2.54      assert(list_curr->next);
    2.55 @@ -136,6 +143,7 @@
    2.56      if (stringpair_list == NULL)
    2.57          return NULL;
    2.58  
    2.59 +    // second list is empty
    2.60      if (second == NULL || second->value == NULL)
    2.61          return stringpair_list;
    2.62  
     3.1 --- a/test/stringlist_test.cc	Tue Jul 12 12:29:10 2016 +0200
     3.2 +++ b/test/stringlist_test.cc	Tue Jul 12 13:32:55 2016 +0200
     3.3 @@ -72,32 +72,7 @@
     3.4          assert((p == NULL) == (p_dst == NULL));
     3.5      }
     3.6      assert(p_dst == NULL);
     3.7 -    
     3.8 -    cout << "\nAdd to 4-element stringlist with tail with no value…\n";
     3.9 -    // get tail
    3.10 -    p = src;
    3.11 -    while (p->next)
    3.12 -        p = p->next;
    3.13 -    
    3.14 -    if (p->value)
    3.15 -        free(p->value);
    3.16 -    p->value = NULL;
    3.17 -    
    3.18 -    strarr[3] = str0;
    3.19 -    stringlist_add(src, str0);
    3.20 -    
    3.21 -    cout << "checking contents\n";
    3.22 -    p = src;
    3.23 -    i = 0;
    3.24 -    while (p) {
    3.25 -        assert(p->value);
    3.26 -        assert(strcmp(p->value, strarr[i++]) == 0);
    3.27 -        assert(p->value != *(strarr + i)); // ensure this is a copy
    3.28 -        cout << p->value;
    3.29 -        p = p->next;
    3.30 -    }
    3.31 -    assert(p == NULL); // list ends properly
    3.32 -    
    3.33 +        
    3.34      cout << "freeing stringlists…\n\n";
    3.35      free_stringlist(src);
    3.36      free_stringlist(dst);
    3.37 @@ -113,7 +88,7 @@
    3.38      assert(dst->next == NULL);
    3.39      cout << "one-element stringlist duped, next element is NULL\n";
    3.40      
    3.41 -    cout << "\nAdd to one-element stringlist with no value…\n";
    3.42 +    cout << "\nAdd to empty stringlist (node exists, but no value…)\n";
    3.43      if (src->value)
    3.44          free(src->value);
    3.45      src->value = NULL;