1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2021, 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  ***************************************************************************/
22 /*
23  * This source code is used for lib1502, lib1503, lib1504 and lib1505 with
24  * only #ifdefs controlling the cleanup sequence.
25  *
26  * Test case 1502 converted from bug report #3575448, identifying a memory
27  * leak in the CURLOPT_RESOLVE handling with the multi interface.
28  */
29 
30 #include "test.h"
31 
32 #include <limits.h>
33 
34 #include "testutil.h"
35 #include "warnless.h"
36 #include "memdebug.h"
37 
38 #define TEST_HANG_TIMEOUT 60 * 1000
39 
test(char * URL)40 int test(char *URL)
41 {
42   CURL *easy = NULL;
43   CURL *dup;
44   CURLM *multi = NULL;
45   int still_running;
46   int res = 0;
47 
48   char redirect[160];
49 
50   /* DNS cache injection */
51   struct curl_slist *dns_cache_list;
52 
53   res_global_init(CURL_GLOBAL_ALL);
54   if(res) {
55     return res;
56   }
57 
58   msnprintf(redirect, sizeof(redirect), "google.com:%s:%s", libtest_arg2,
59             libtest_arg3);
60 
61   start_test_timing();
62 
63   dns_cache_list = curl_slist_append(NULL, redirect);
64   if(!dns_cache_list) {
65     fprintf(stderr, "curl_slist_append() failed\n");
66     curl_global_cleanup();
67     return TEST_ERR_MAJOR_BAD;
68   }
69 
70   easy_init(easy);
71 
72   easy_setopt(easy, CURLOPT_URL, URL);
73   easy_setopt(easy, CURLOPT_HEADER, 1L);
74   easy_setopt(easy, CURLOPT_RESOLVE, dns_cache_list);
75 
76   dup = curl_easy_duphandle(easy);
77   if(dup) {
78     curl_easy_cleanup(easy);
79     easy = dup;
80   }
81   else {
82     curl_slist_free_all(dns_cache_list);
83     curl_easy_cleanup(easy);
84     curl_global_cleanup();
85     return CURLE_OUT_OF_MEMORY;
86   }
87 
88   multi_init(multi);
89 
90   multi_add_handle(multi, easy);
91 
92   multi_perform(multi, &still_running);
93 
94   abort_on_test_timeout();
95 
96   while(still_running) {
97     struct timeval timeout;
98     fd_set fdread;
99     fd_set fdwrite;
100     fd_set fdexcep;
101     int maxfd = -99;
102 
103     FD_ZERO(&fdread);
104     FD_ZERO(&fdwrite);
105     FD_ZERO(&fdexcep);
106     timeout.tv_sec = 1;
107     timeout.tv_usec = 0;
108 
109     multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
110 
111     /* At this point, maxfd is guaranteed to be greater or equal than -1. */
112 
113     select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
114 
115     abort_on_test_timeout();
116 
117     multi_perform(multi, &still_running);
118 
119     abort_on_test_timeout();
120   }
121 
122 test_cleanup:
123 
124 #ifdef LIB1502
125   /* undocumented cleanup sequence - type UA */
126   curl_multi_cleanup(multi);
127   curl_easy_cleanup(easy);
128   curl_global_cleanup();
129 #endif
130 
131 #ifdef LIB1503
132   /* proper cleanup sequence - type PA */
133   curl_multi_remove_handle(multi, easy);
134   curl_multi_cleanup(multi);
135   curl_easy_cleanup(easy);
136   curl_global_cleanup();
137 #endif
138 
139 #ifdef LIB1504
140   /* undocumented cleanup sequence - type UB */
141   curl_easy_cleanup(easy);
142   curl_multi_cleanup(multi);
143   curl_global_cleanup();
144 #endif
145 
146 #ifdef LIB1505
147   /* proper cleanup sequence - type PB */
148   curl_multi_remove_handle(multi, easy);
149   curl_easy_cleanup(easy);
150   curl_multi_cleanup(multi);
151   curl_global_cleanup();
152 #endif
153 
154   curl_slist_free_all(dns_cache_list);
155 
156   return res;
157 }
158