lib1502.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #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. res_global_init(CURL_GLOBAL_ALL);
  46. if(res) {
  47. return res;
  48. }
  49. msnprintf(redirect, sizeof(redirect), "google.com:%s:%s", libtest_arg2,
  50. libtest_arg3);
  51. start_test_timing();
  52. dns_cache_list = curl_slist_append(NULL, redirect);
  53. if(!dns_cache_list) {
  54. fprintf(stderr, "curl_slist_append() failed\n");
  55. curl_global_cleanup();
  56. return TEST_ERR_MAJOR_BAD;
  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. curl_global_cleanup();
  71. return CURLE_OUT_OF_MEMORY;
  72. }
  73. multi_init(multi);
  74. multi_add_handle(multi, easy);
  75. multi_perform(multi, &still_running);
  76. abort_on_test_timeout();
  77. while(still_running) {
  78. struct timeval timeout;
  79. fd_set fdread;
  80. fd_set fdwrite;
  81. fd_set fdexcep;
  82. int maxfd = -99;
  83. FD_ZERO(&fdread);
  84. FD_ZERO(&fdwrite);
  85. FD_ZERO(&fdexcep);
  86. timeout.tv_sec = 1;
  87. timeout.tv_usec = 0;
  88. multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
  89. /* At this point, maxfd is guaranteed to be greater or equal than -1. */
  90. select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  91. abort_on_test_timeout();
  92. multi_perform(multi, &still_running);
  93. abort_on_test_timeout();
  94. }
  95. test_cleanup:
  96. #ifdef LIB1502
  97. /* undocumented cleanup sequence - type UA */
  98. curl_multi_cleanup(multi);
  99. curl_easy_cleanup(easy);
  100. curl_global_cleanup();
  101. #endif
  102. #ifdef LIB1503
  103. /* proper cleanup sequence - type PA */
  104. curl_multi_remove_handle(multi, easy);
  105. curl_multi_cleanup(multi);
  106. curl_easy_cleanup(easy);
  107. curl_global_cleanup();
  108. #endif
  109. #ifdef LIB1504
  110. /* undocumented cleanup sequence - type UB */
  111. curl_easy_cleanup(easy);
  112. curl_multi_cleanup(multi);
  113. curl_global_cleanup();
  114. #endif
  115. #ifdef LIB1505
  116. /* proper cleanup sequence - type PB */
  117. curl_multi_remove_handle(multi, easy);
  118. curl_easy_cleanup(easy);
  119. curl_multi_cleanup(multi);
  120. curl_global_cleanup();
  121. #endif
  122. curl_slist_free_all(dns_cache_list);
  123. return res;
  124. }