Coverage Report

Created: 2026-01-25 06:10

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
5.34k
{
31
5.34k
  struct curl_slist *item;
32
33
  /* if caller passed us a NULL, return now */
34
5.34k
  if(!list)
35
0
    return NULL;
36
37
  /* loop through to find the last item */
38
5.34k
  item = list;
39
2.52M
  while(item->next) {
40
2.52M
    item = item->next;
41
2.52M
  }
42
5.34k
  return item;
43
5.34k
}
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
17.8k
{
57
17.8k
  struct curl_slist *last;
58
17.8k
  struct curl_slist *new_item;
59
60
17.8k
  DEBUGASSERT(data);
61
62
17.8k
  new_item = curlx_malloc(sizeof(struct curl_slist));
63
17.8k
  if(!new_item)
64
0
    return NULL;
65
66
17.8k
  new_item->next = NULL;
67
17.8k
  new_item->data = CURL_UNCONST(data);
68
69
  /* if this is the first item, then new_item *is* the list */
70
17.8k
  if(!list)
71
12.4k
    return new_item;
72
73
5.34k
  last = slist_get_last(list);
74
5.34k
  last->next = new_item;
75
5.34k
  return list;
76
17.8k
}
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 simply 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
17.8k
{
87
17.8k
  char *dupdata = curlx_strdup(data);
88
89
17.8k
  if(!dupdata)
90
0
    return NULL;
91
92
17.8k
  list = Curl_slist_append_nodup(list, dupdata);
93
17.8k
  if(!list)
94
0
    curlx_free(dupdata);
95
96
17.8k
  return list;
97
17.8k
}
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 *tmp;
108
109
0
  while(inlist) {
110
0
    tmp = curl_slist_append(outlist, inlist->data);
111
112
0
    if(!tmp) {
113
0
      curl_slist_free_all(outlist);
114
0
      return NULL;
115
0
    }
116
117
0
    outlist = tmp;
118
0
    inlist = inlist->next;
119
0
  }
120
0
  return outlist;
121
0
}
122
123
/* be nice and clean up resources */
124
void curl_slist_free_all(struct curl_slist *list)
125
19.8k
{
126
19.8k
  struct curl_slist *next;
127
19.8k
  struct curl_slist *item;
128
129
19.8k
  if(!list)
130
7.38k
    return;
131
132
12.4k
  item = list;
133
17.8k
  do {
134
17.8k
    next = item->next;
135
17.8k
    Curl_safefree(item->data);
136
17.8k
    curlx_free(item);
137
17.8k
    item = next;
138
17.8k
  } while(next);
139
12.4k
}