2
0

lib1502.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 #ifdefs 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. #include "test.h"
  32. #include <limits.h>
  33. #include "testutil.h"
  34. #include "warnless.h"
  35. #include "memdebug.h"
  36. #define TEST_HANG_TIMEOUT 60 * 1000
  37. CURLcode test(char *URL)
  38. {
  39. CURL *easy = NULL;
  40. CURL *dup;
  41. CURLM *multi = NULL;
  42. int still_running;
  43. CURLcode res = CURLE_OK;
  44. char redirect[160];
  45. /* DNS cache injection */
  46. struct curl_slist *dns_cache_list;
  47. res_global_init(CURL_GLOBAL_ALL);
  48. if(res) {
  49. return res;
  50. }
  51. msnprintf(redirect, sizeof(redirect), "google.com:%s:%s", libtest_arg2,
  52. libtest_arg3);
  53. start_test_timing();
  54. dns_cache_list = curl_slist_append(NULL, redirect);
  55. if(!dns_cache_list) {
  56. fprintf(stderr, "curl_slist_append() failed\n");
  57. curl_global_cleanup();
  58. return TEST_ERR_MAJOR_BAD;
  59. }
  60. easy_init(easy);
  61. easy_setopt(easy, CURLOPT_URL, URL);
  62. easy_setopt(easy, CURLOPT_HEADER, 1L);
  63. easy_setopt(easy, CURLOPT_RESOLVE, dns_cache_list);
  64. dup = curl_easy_duphandle(easy);
  65. if(dup) {
  66. curl_easy_cleanup(easy);
  67. easy = dup;
  68. }
  69. else {
  70. curl_slist_free_all(dns_cache_list);
  71. curl_easy_cleanup(easy);
  72. curl_global_cleanup();
  73. return CURLE_OUT_OF_MEMORY;
  74. }
  75. multi_init(multi);
  76. multi_add_handle(multi, easy);
  77. multi_perform(multi, &still_running);
  78. abort_on_test_timeout();
  79. while(still_running) {
  80. struct timeval timeout;
  81. fd_set fdread;
  82. fd_set fdwrite;
  83. fd_set fdexcep;
  84. int maxfd = -99;
  85. FD_ZERO(&fdread);
  86. FD_ZERO(&fdwrite);
  87. FD_ZERO(&fdexcep);
  88. timeout.tv_sec = 1;
  89. timeout.tv_usec = 0;
  90. multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
  91. /* At this point, maxfd is guaranteed to be greater or equal than -1. */
  92. select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  93. abort_on_test_timeout();
  94. multi_perform(multi, &still_running);
  95. abort_on_test_timeout();
  96. }
  97. test_cleanup:
  98. #ifdef LIB1502
  99. /* undocumented cleanup sequence - type UA */
  100. curl_multi_cleanup(multi);
  101. curl_easy_cleanup(easy);
  102. curl_global_cleanup();
  103. #endif
  104. #ifdef LIB1503
  105. /* proper cleanup sequence - type PA */
  106. curl_multi_remove_handle(multi, easy);
  107. curl_multi_cleanup(multi);
  108. curl_easy_cleanup(easy);
  109. curl_global_cleanup();
  110. #endif
  111. #ifdef LIB1504
  112. /* undocumented cleanup sequence - type UB */
  113. curl_easy_cleanup(easy);
  114. curl_multi_cleanup(multi);
  115. curl_global_cleanup();
  116. #endif
  117. #ifdef LIB1505
  118. /* proper cleanup sequence - type PB */
  119. curl_multi_remove_handle(multi, easy);
  120. curl_easy_cleanup(easy);
  121. curl_multi_cleanup(multi);
  122. curl_global_cleanup();
  123. #endif
  124. curl_slist_free_all(dns_cache_list);
  125. return res;
  126. }