1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel (at) 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 #include "test.h" 23 24 /* 25 * This test uses these funny custom memory callbacks for the only purpose 26 * of verifying that curl_global_init_mem() functionality is present in 27 * libcurl and that it works unconditionally no matter how libcurl is built, 28 * nothing more. 29 * 30 * Do not include memdebug.h in this source file, and do not use directly 31 * memory related functions in this file except those used inside custom 32 * memory callbacks which should be calling 'the real thing'. 33 */ 34 35 /* 36 #include "memdebug.h" 37 */ 38 39 static int seen_malloc = 0; 40 static int seen_free = 0; 41 static int seen_realloc = 0; 42 static int seen_strdup = 0; 43 static int seen_calloc = 0; 44 45 void *custom_malloc(size_t size); 46 void custom_free(void *ptr); 47 void *custom_realloc(void *ptr, size_t size); 48 char *custom_strdup(const char *ptr); 49 void *custom_calloc(size_t nmemb, size_t size); 50 51 52 void *custom_calloc(size_t nmemb, size_t size) 53 { 54 if(!seen_calloc) { 55 printf("seen custom_calloc()\n"); 56 seen_calloc = 1; 57 } 58 return (calloc)(nmemb, size); 59 } 60 61 void *custom_malloc(size_t size) 62 { 63 if(!seen_malloc && seen_calloc) { 64 printf("seen custom_malloc()\n"); 65 seen_malloc = 1; 66 } 67 return (malloc)(size); 68 } 69 70 char *custom_strdup(const char *ptr) 71 { 72 if(!seen_strdup && seen_malloc) { 73 /* currently (2013.03.13), memory tracking enabled builds do not call 74 the strdup callback, in this case malloc callback and memcpy are used 75 instead. If some day this is changed the following printf() should be 76 uncommented, and a line added to test definition. 77 printf("seen custom_strdup()\n"); 78 */ 79 seen_strdup = 1; 80 } 81 return (strdup)(ptr); 82 } 83 84 void *custom_realloc(void *ptr, size_t size) 85 { 86 if(!seen_realloc && seen_malloc) { 87 printf("seen custom_realloc()\n"); 88 seen_realloc = 1; 89 } 90 return (realloc)(ptr, size); 91 } 92 93 void custom_free(void *ptr) 94 { 95 if(!seen_free && seen_realloc) { 96 printf("seen custom_free()\n"); 97 seen_free = 1; 98 } 99 (free)(ptr); 100 } 101 102 103 int test(char *URL) 104 { 105 unsigned char a[] = {0x2f, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 106 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7}; 107 CURLcode res; 108 CURL *curl; 109 int asize; 110 char *str = NULL; 111 112 (void)URL; 113 114 res = curl_global_init_mem(CURL_GLOBAL_ALL, 115 custom_malloc, 116 custom_free, 117 custom_realloc, 118 custom_strdup, 119 custom_calloc); 120 if(res != CURLE_OK) { 121 fprintf(stderr, "curl_global_init_mem() failed\n"); 122 return TEST_ERR_MAJOR_BAD; 123 } 124 125 curl = curl_easy_init(); 126 if(!curl) { 127 fprintf(stderr, "curl_easy_init() failed\n"); 128 curl_global_cleanup(); 129 return TEST_ERR_MAJOR_BAD; 130 } 131 132 test_setopt(curl, CURLOPT_USERAGENT, "test509"); /* uses strdup() */ 133 134 asize = (int)sizeof(a); 135 str = curl_easy_escape(curl, (char *)a, asize); /* uses realloc() */ 136 137 test_cleanup: 138 139 if(str) 140 curl_free(str); 141 142 curl_easy_cleanup(curl); 143 curl_global_cleanup(); 144 145 return (int)res; 146 } 147 148