quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

lib1502.c (3920B)


      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  * This source code is used for lib1502, lib1503, lib1504 and lib1505 with
     26  * only the testnum controlling the cleanup sequence.
     27  *
     28  * Test case 1502 converted from bug report #3575448, identifying a memory
     29  * leak in the CURLOPT_RESOLVE handling with the multi interface.
     30  */
     31 
     32 #include "first.h"
     33 
     34 #include "memdebug.h"
     35 
     36 static CURLcode test_lib1502(char *URL)
     37 {
     38   CURL *easy = NULL;
     39   CURL *dup;
     40   CURLM *multi = NULL;
     41   int still_running;
     42   CURLcode res = CURLE_OK;
     43   char redirect[160];
     44 
     45   /* DNS cache injection */
     46   struct curl_slist *dns_cache_list;
     47 
     48   res_global_init(CURL_GLOBAL_ALL);
     49   if(res) {
     50     return res;
     51   }
     52 
     53   curl_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     curl_mfprintf(stderr, "curl_slist_append() failed\n");
     61     curl_global_cleanup();
     62     return TEST_ERR_MAJOR_BAD;
     63   }
     64 
     65   easy_init(easy);
     66 
     67   easy_setopt(easy, CURLOPT_URL, URL);
     68   easy_setopt(easy, CURLOPT_HEADER, 1L);
     69   easy_setopt(easy, CURLOPT_RESOLVE, dns_cache_list);
     70 
     71   dup = curl_easy_duphandle(easy);
     72   if(dup) {
     73     curl_easy_cleanup(easy);
     74     easy = dup;
     75   }
     76   else {
     77     curl_slist_free_all(dns_cache_list);
     78     curl_easy_cleanup(easy);
     79     curl_global_cleanup();
     80     return CURLE_OUT_OF_MEMORY;
     81   }
     82 
     83   multi_init(multi);
     84 
     85   multi_add_handle(multi, easy);
     86 
     87   multi_perform(multi, &still_running);
     88 
     89   abort_on_test_timeout();
     90 
     91   while(still_running) {
     92     struct timeval timeout;
     93     fd_set fdread;
     94     fd_set fdwrite;
     95     fd_set fdexcep;
     96     int maxfd = -99;
     97 
     98     FD_ZERO(&fdread);
     99     FD_ZERO(&fdwrite);
    100     FD_ZERO(&fdexcep);
    101     timeout.tv_sec = 1;
    102     timeout.tv_usec = 0;
    103 
    104     multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
    105 
    106     /* At this point, maxfd is guaranteed to be greater or equal than -1. */
    107 
    108     select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
    109 
    110     abort_on_test_timeout();
    111 
    112     multi_perform(multi, &still_running);
    113 
    114     abort_on_test_timeout();
    115   }
    116 
    117 test_cleanup:
    118 
    119   switch(testnum) {
    120   case 1502:
    121   default:
    122     /* undocumented cleanup sequence - type UA */
    123     curl_multi_cleanup(multi);
    124     curl_easy_cleanup(easy);
    125     curl_global_cleanup();
    126     break;
    127   case 1503:
    128     /* proper cleanup sequence - type PA */
    129     curl_multi_remove_handle(multi, easy);
    130     curl_multi_cleanup(multi);
    131     curl_easy_cleanup(easy);
    132     curl_global_cleanup();
    133     break;
    134   case 1504:
    135     /* undocumented cleanup sequence - type UB */
    136     curl_easy_cleanup(easy);
    137     curl_multi_cleanup(multi);
    138     curl_global_cleanup();
    139     break;
    140   case 1505:
    141     /* proper cleanup sequence - type PB */
    142     curl_multi_remove_handle(multi, easy);
    143     curl_easy_cleanup(easy);
    144     curl_multi_cleanup(multi);
    145     curl_global_cleanup();
    146     break;
    147   }
    148 
    149   curl_slist_free_all(dns_cache_list);
    150 
    151   return res;
    152 }