Coverage Report

Created: 2025-12-04 06:52

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
25
#include "curl_setup.h"
26
27
#include <curl/curl.h>
28
29
#include "slist.h"
30
31
/* returns last node in linked list */
32
static struct curl_slist *slist_get_last(struct curl_slist *list)
33
4.68k
{
34
4.68k
  struct curl_slist *item;
35
36
  /* if caller passed us a NULL, return now */
37
4.68k
  if(!list)
38
0
    return NULL;
39
40
  /* loop through to find the last item */
41
4.68k
  item = list;
42
2.11M
  while(item->next) {
43
2.10M
    item = item->next;
44
2.10M
  }
45
4.68k
  return item;
46
4.68k
}
47
48
/*
49
 * Curl_slist_append_nodup() appends a string to the linked list. Rather than
50
 * copying the string in dynamic storage, it takes its ownership. The string
51
 * should have been malloc()ated. Curl_slist_append_nodup always returns
52
 * the address of the first record, so that you can use this function as an
53
 * initialization function as well as an append function.
54
 * If an error occurs, NULL is returned and the string argument is NOT
55
 * released.
56
 */
57
struct curl_slist *Curl_slist_append_nodup(struct curl_slist *list,
58
                                           const char *data)
59
16.7k
{
60
16.7k
  struct curl_slist *last;
61
16.7k
  struct curl_slist *new_item;
62
63
16.7k
  DEBUGASSERT(data);
64
65
16.7k
  new_item = curlx_malloc(sizeof(struct curl_slist));
66
16.7k
  if(!new_item)
67
0
    return NULL;
68
69
16.7k
  new_item->next = NULL;
70
16.7k
  new_item->data = CURL_UNCONST(data);
71
72
  /* if this is the first item, then new_item *is* the list */
73
16.7k
  if(!list)
74
12.0k
    return new_item;
75
76
4.68k
  last = slist_get_last(list);
77
4.68k
  last->next = new_item;
78
4.68k
  return list;
79
16.7k
}
80
81
/*
82
 * curl_slist_append() appends a string to the linked list. It always returns
83
 * the address of the first record, so that you can use this function as an
84
 * initialization function as well as an append function. If you find this
85
 * bothersome, then simply create a separate _init function and call it
86
 * appropriately from within the program.
87
 */
88
struct curl_slist *curl_slist_append(struct curl_slist *list, const char *data)
89
16.7k
{
90
16.7k
  char *dupdata = curlx_strdup(data);
91
92
16.7k
  if(!dupdata)
93
0
    return NULL;
94
95
16.7k
  list = Curl_slist_append_nodup(list, dupdata);
96
16.7k
  if(!list)
97
0
    curlx_free(dupdata);
98
99
16.7k
  return list;
100
16.7k
}
101
102
/*
103
 * Curl_slist_duplicate() duplicates a linked list. It always returns the
104
 * address of the first record of the cloned list or NULL in case of an
105
 * error (or if the input list was NULL).
106
 */
107
struct curl_slist *Curl_slist_duplicate(struct curl_slist *inlist)
108
0
{
109
0
  struct curl_slist *outlist = NULL;
110
0
  struct curl_slist *tmp;
111
112
0
  while(inlist) {
113
0
    tmp = curl_slist_append(outlist, inlist->data);
114
115
0
    if(!tmp) {
116
0
      curl_slist_free_all(outlist);
117
0
      return NULL;
118
0
    }
119
120
0
    outlist = tmp;
121
0
    inlist = inlist->next;
122
0
  }
123
0
  return outlist;
124
0
}
125
126
/* be nice and clean up resources */
127
void curl_slist_free_all(struct curl_slist *list)
128
25.9k
{
129
25.9k
  struct curl_slist *next;
130
25.9k
  struct curl_slist *item;
131
132
25.9k
  if(!list)
133
13.9k
    return;
134
135
12.0k
  item = list;
136
16.7k
  do {
137
16.7k
    next = item->next;
138
16.7k
    Curl_safefree(item->data);
139
16.7k
    curlx_free(item);
140
16.7k
    item = next;
141
16.7k
  } while(next);
142
12.0k
}