Coverage Report

Created: 2026-10-15 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/slist.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#include "slist.h"
27
28
/* returns last node in linked list */
29
static struct curl_slist *slist_get_last(struct curl_slist *list)
30
63.3k
{
31
63.3k
  struct curl_slist *item;
32
33
  /* if caller passed us a NULL, return now */
34
63.3k
  if(!list)
35
0
    return NULL;
36
37
  /* loop through to find the last item */
38
63.3k
  item = list;
39
2.94M
  while(item->next) {
40
2.88M
    item = item->next;
41
2.88M
  }
42
63.3k
  return item;
43
63.3k
}
44
45
/*
46
 * Curl_slist_append_nodup() appends a string to the linked list. Rather than
47
 * copying the string in dynamic storage, it takes its ownership. The string
48
 * should have been malloc()ated. Curl_slist_append_nodup always returns
49
 * the address of the first record, so that you can use this function as an
50
 * initialization function as well as an append function.
51
 * If an error occurs, NULL is returned and the string argument is NOT
52
 * released.
53
 */
54
struct curl_slist *Curl_slist_append_nodup(struct curl_slist *list,
55
                                           const char *data)
56
291k
{
57
291k
  struct curl_slist *last;
58
291k
  struct curl_slist *new_item;
59
60
291k
  DEBUGASSERT(data);
61
62
291k
  new_item = curlx_malloc(sizeof(struct curl_slist));
63
291k
  if(!new_item)
64
0
    return NULL;
65
66
291k
  new_item->next = NULL;
67
291k
  new_item->data = CURL_UNCONST(data);
68
69
  /* if this is the first item, then new_item *is* the list */
70
291k
  if(!list)
71
228k
    return new_item;
72
73
63.3k
  last = slist_get_last(list);
74
63.3k
  last->next = new_item;
75
63.3k
  return list;
76
291k
}
77
78
/*
79
 * curl_slist_append() appends a string to the linked list. It always returns
80
 * the address of the first record, so that you can use this function as an
81
 * initialization function as well as an append function. If you find this
82
 * bothersome, then create a separate _init function and call it
83
 * appropriately from within the program.
84
 */
85
struct curl_slist *curl_slist_append(struct curl_slist *list, const char *data)
86
133k
{
87
133k
  char *dupdata = curlx_strdup(data);
88
89
133k
  if(!dupdata)
90
0
    return NULL;
91
92
133k
  list = Curl_slist_append_nodup(list, dupdata);
93
133k
  if(!list)
94
0
    curlx_free(dupdata);
95
96
133k
  return list;
97
133k
}
98
99
/*
100
 * Curl_slist_duplicate() duplicates a linked list. It always returns the
101
 * address of the first record of the cloned list or NULL in case of an
102
 * error (or if the input list was NULL).
103
 */
104
struct curl_slist *Curl_slist_duplicate(struct curl_slist *inlist)
105
0
{
106
0
  struct curl_slist *outlist = NULL;
107
0
  struct curl_slist **tail = &outlist;
108
109
0
  while(inlist) {
110
0
    struct curl_slist *tmp = curl_slist_append(NULL, inlist->data);
111
112
0
    if(!tmp) {
113
0
      curl_slist_free_all(outlist);
114
0
      return NULL;
115
0
    }
116
117
0
    *tail = tmp;
118
0
    tail = &tmp->next;
119
0
    inlist = inlist->next;
120
0
  }
121
0
  return outlist;
122
0
}
123
124
/* be nice and clean up resources */
125
void curl_slist_free_all(struct curl_slist *list)
126
369k
{
127
369k
  struct curl_slist *next;
128
369k
  struct curl_slist *item;
129
130
369k
  if(!list)
131
140k
    return;
132
133
228k
  item = list;
134
283k
  do {
135
283k
    next = item->next;
136
283k
    curlx_safefree(item->data);
137
283k
    curlx_free(item);
138
283k
    item = next;
139
283k
  } while(next);
140
228k
}