lib1502.c 3.8 KB

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