Coverage Report

Created: 2024-02-25 06:14

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