1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2018, 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.haxx.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   msnprintf(redirect, sizeof(redirect), "google.com:%s:%s", libtest_arg2,
54             libtest_arg3);
55 
56   start_test_timing();
57 
58   dns_cache_list = curl_slist_append(NULL, redirect);
59   if(!dns_cache_list) {
60     fprintf(stderr, "curl_slist_append() failed\n");
61     return TEST_ERR_MAJOR_BAD;
62   }
63 
64   res_global_init(CURL_GLOBAL_ALL);
65   if(res) {
66     curl_slist_free_all(dns_cache_list);
67     return res;
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     return CURLE_OUT_OF_MEMORY;
85   }
86 
87   multi_init(multi);
88 
89   multi_add_handle(multi, easy);
90 
91   multi_perform(multi, &still_running);
92 
93   abort_on_test_timeout();
94 
95   while(still_running) {
96     struct timeval timeout;
97     fd_set fdread;
98     fd_set fdwrite;
99     fd_set fdexcep;
100     int maxfd = -99;
101 
102     FD_ZERO(&fdread);
103     FD_ZERO(&fdwrite);
104     FD_ZERO(&fdexcep);
105     timeout.tv_sec = 1;
106     timeout.tv_usec = 0;
107 
108     multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
109 
110     /* At this point, maxfd is guaranteed to be greater or equal than -1. */
111 
112     select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
113 
114     abort_on_test_timeout();
115 
116     multi_perform(multi, &still_running);
117 
118     abort_on_test_timeout();
119   }
120 
121 test_cleanup:
122 
123 #ifdef LIB1502
124   /* undocumented cleanup sequence - type UA */
125   curl_multi_cleanup(multi);
126   curl_easy_cleanup(easy);
127   curl_global_cleanup();
128 #endif
129 
130 #ifdef LIB1503
131   /* proper cleanup sequence - type PA */
132   curl_multi_remove_handle(multi, easy);
133   curl_multi_cleanup(multi);
134   curl_easy_cleanup(easy);
135   curl_global_cleanup();
136 #endif
137 
138 #ifdef LIB1504
139   /* undocumented cleanup sequence - type UB */
140   curl_easy_cleanup(easy);
141   curl_multi_cleanup(multi);
142   curl_global_cleanup();
143 #endif
144 
145 #ifdef LIB1505
146   /* proper cleanup sequence - type PB */
147   curl_multi_remove_handle(multi, easy);
148   curl_easy_cleanup(easy);
149   curl_multi_cleanup(multi);
150   curl_global_cleanup();
151 #endif
152 
153   curl_slist_free_all(dns_cache_list);
154 
155   return res;
156 }
157