Fix double-iteration in curl_slist_remove (#1951)

Also backfill tests.  Fixes #1948.
This commit is contained in:
Andrew Gaul 2022-05-27 22:56:20 +09:00 committed by GitHub
parent 92fd5bc3e1
commit 904682b856
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 1 deletions

View File

@ -109,7 +109,7 @@ struct curl_slist* curl_slist_remove(struct curl_slist* list, const char* key)
std::string strkey = trim(std::string(key)); std::string strkey = trim(std::string(key));
struct curl_slist **p = &list; struct curl_slist **p = &list;
for(;*p; p = &(*p)->next){ while(*p){
std::string strcur = (*p)->data; std::string strcur = (*p)->data;
size_t pos; size_t pos;
if(std::string::npos != (pos = strcur.find(':', 0))){ if(std::string::npos != (pos = strcur.find(':', 0))){
@ -122,6 +122,8 @@ struct curl_slist* curl_slist_remove(struct curl_slist* list, const char* key)
struct curl_slist *tmp = *p; struct curl_slist *tmp = *p;
*p = (*p)->next; *p = (*p)->next;
free(tmp); free(tmp);
}else{
p = &(*p)->next;
} }
} }

View File

@ -105,9 +105,55 @@ void test_sort_insert()
curl_slist_free_all(list); curl_slist_free_all(list);
} }
void test_slist_remove()
{
struct curl_slist* list = NULL;
// remove no elements
ASSERT_EQUALS(static_cast<size_t>(0), curl_slist_length(list));
list = curl_slist_remove(list, "1");
ASSERT_EQUALS(static_cast<size_t>(0), curl_slist_length(list));
// remove only element
list = NULL;
list = curl_slist_sort_insert(list, "1", "val");
ASSERT_EQUALS(static_cast<size_t>(1), curl_slist_length(list));
list = curl_slist_remove(list, "1");
ASSERT_EQUALS(static_cast<size_t>(0), curl_slist_length(list));
// remove head element
list = NULL;
list = curl_slist_sort_insert(list, "1", "val");
list = curl_slist_sort_insert(list, "2", "val");
ASSERT_EQUALS(static_cast<size_t>(2), curl_slist_length(list));
list = curl_slist_remove(list, "1");
ASSERT_EQUALS(static_cast<size_t>(1), curl_slist_length(list));
curl_slist_free_all(list);
// remove tail element
list = NULL;
list = curl_slist_sort_insert(list, "1", "val");
list = curl_slist_sort_insert(list, "2", "val");
ASSERT_EQUALS(static_cast<size_t>(2), curl_slist_length(list));
list = curl_slist_remove(list, "2");
ASSERT_EQUALS(static_cast<size_t>(1), curl_slist_length(list));
curl_slist_free_all(list);
// remove middle element
list = NULL;
list = curl_slist_sort_insert(list, "1", "val");
list = curl_slist_sort_insert(list, "2", "val");
list = curl_slist_sort_insert(list, "3", "val");
ASSERT_EQUALS(static_cast<size_t>(3), curl_slist_length(list));
list = curl_slist_remove(list, "2");
ASSERT_EQUALS(static_cast<size_t>(2), curl_slist_length(list));
curl_slist_free_all(list);
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
test_sort_insert(); test_sort_insert();
test_slist_remove();
return 0; return 0;
} }