mirror of
https://github.com/curl/curl.git
synced 2026-05-30 07:57:27 +03:00
tidy-up: prefer return over exit(), fix fallouts
To avoid breaking the control flow and align to majority of code
already using `return`.
`exit()` has the side-effect of suppressing leak detection in cases.
Fix fallouts detected after switching to `return`.
- configure:
- fix `getaddrinfo` run test to call `freeaddrinfo()` to pacify ASAN,
and call `WSACleanup()` to deinit winsock2.
- fix `getifaddrs` run test to call `freeifaddrs()` to pacify ASAN.
- tests/server:
- setup `atexit(win32_cleanup)` via `win32_init()`.
- return 2 instead of 1 on winsock2 init failures.
- sws: goto cleanup instead of `exit()` in `http_connect()`.
Follow-up to 02dfe71937 #7235
- tests/client/http:
- cleanup memory to pacify ASAN in `h2-upgrade-extreme`,
`tls-session-reuse`.
- examples:
- block_ip: fix memory leak reported by CI.
- http2-upload: avoid handle leaks.
Untouched `exit()` calls, made from callbacks:
- docs/examples: ephiperfifo.c, ghiper.c, hiperfifo.c
- tests/libtest: lib582.c, lib655.c, lib670.c
- tests/server: tftpd.c
Closes #16507
This commit is contained in:
parent
2e585f5640
commit
08c7c937dc
22 changed files with 222 additions and 150 deletions
|
|
@ -298,14 +298,19 @@ int main(void)
|
|||
|
||||
filter = (struct connection_filter *)calloc(1, sizeof(*filter));
|
||||
if(!filter)
|
||||
exit(1);
|
||||
return 1;
|
||||
|
||||
if(curl_global_init(CURL_GLOBAL_DEFAULT))
|
||||
exit(1);
|
||||
if(curl_global_init(CURL_GLOBAL_DEFAULT)) {
|
||||
free(filter);
|
||||
return 1;
|
||||
}
|
||||
|
||||
curl = curl_easy_init();
|
||||
if(!curl)
|
||||
exit(1);
|
||||
if(!curl) {
|
||||
curl_global_cleanup();
|
||||
free(filter);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Set the target URL */
|
||||
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost");
|
||||
|
|
|
|||
|
|
@ -79,12 +79,12 @@ int main(int argc, char *argv[])
|
|||
fprintf(stderr,
|
||||
"\rUsage: %s [-m=1|2|5|10|20|50|100] [-t] [-x] [url]\n",
|
||||
appname);
|
||||
exit(1);
|
||||
return 1;
|
||||
case 'v':
|
||||
case 'V':
|
||||
fprintf(stderr, "\r%s %s - %s\n",
|
||||
appname, CHKSPEED_VERSION, curl_version());
|
||||
exit(1);
|
||||
return 1;
|
||||
case 'a':
|
||||
case 'A':
|
||||
prtall = 1;
|
||||
|
|
|
|||
|
|
@ -34,8 +34,7 @@
|
|||
#include <curl/curl.h>
|
||||
#include <curl/mprintf.h>
|
||||
|
||||
static void
|
||||
print_cookies(CURL *curl)
|
||||
static int print_cookies(CURL *curl)
|
||||
{
|
||||
CURLcode res;
|
||||
struct curl_slist *cookies;
|
||||
|
|
@ -47,7 +46,7 @@ print_cookies(CURL *curl)
|
|||
if(res != CURLE_OK) {
|
||||
fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n",
|
||||
curl_easy_strerror(res));
|
||||
exit(1);
|
||||
return 1;
|
||||
}
|
||||
nc = cookies;
|
||||
i = 1;
|
||||
|
|
@ -60,6 +59,8 @@ print_cookies(CURL *curl)
|
|||
printf("(none)\n");
|
||||
}
|
||||
curl_slist_free_all(cookies);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
|
|
|
|||
|
|
@ -94,7 +94,7 @@ static bool init(CURL *&conn, const char *url)
|
|||
|
||||
if(conn == NULL) {
|
||||
fprintf(stderr, "Failed to create CURL connection\n");
|
||||
exit(EXIT_FAILURE);
|
||||
return false;
|
||||
}
|
||||
|
||||
code = curl_easy_setopt(conn, CURLOPT_ERRORBUFFER, errorBuffer);
|
||||
|
|
@ -270,7 +270,7 @@ int main(int argc, char *argv[])
|
|||
|
||||
if(argc != 2) {
|
||||
fprintf(stderr, "Usage: %s <url>\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
|
|
@ -279,7 +279,7 @@ int main(int argc, char *argv[])
|
|||
|
||||
if(!init(conn, argv[1])) {
|
||||
fprintf(stderr, "Connection initialization failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Retrieve content for the URL
|
||||
|
|
@ -289,7 +289,7 @@ int main(int argc, char *argv[])
|
|||
|
||||
if(code != CURLE_OK) {
|
||||
fprintf(stderr, "Failed to get '%s' [%s]\n", argv[1], errorBuffer);
|
||||
exit(EXIT_FAILURE);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Parse the (assumed) HTML code
|
||||
|
|
|
|||
|
|
@ -142,7 +142,7 @@ int my_trace(CURL *handle, curl_infotype type,
|
|||
return 0;
|
||||
}
|
||||
|
||||
static void setup(struct transfer *t, int num)
|
||||
static int setup(struct transfer *t, int num)
|
||||
{
|
||||
char filename[128];
|
||||
CURL *hnd;
|
||||
|
|
@ -155,7 +155,7 @@ static void setup(struct transfer *t, int num)
|
|||
if(!t->out) {
|
||||
fprintf(stderr, "error: could not open file %s for writing: %s\n",
|
||||
filename, strerror(errno));
|
||||
exit(1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* write to this file */
|
||||
|
|
@ -179,6 +179,7 @@ static void setup(struct transfer *t, int num)
|
|||
/* wait for pipe connection to confirm */
|
||||
curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -204,7 +205,8 @@ int main(int argc, char **argv)
|
|||
multi_handle = curl_multi_init();
|
||||
|
||||
for(i = 0; i < num_transfers; i++) {
|
||||
setup(&trans[i], i);
|
||||
if(setup(&trans[i], i))
|
||||
return 1;
|
||||
|
||||
/* add the individual transfer */
|
||||
curl_multi_add_handle(multi_handle, trans[i].easy);
|
||||
|
|
|
|||
|
|
@ -200,7 +200,7 @@ static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
|
|||
return retcode;
|
||||
}
|
||||
|
||||
static void setup(struct input *i, int num, const char *upload)
|
||||
static int setup(struct input *i, int num, const char *upload)
|
||||
{
|
||||
FILE *out;
|
||||
char url[256];
|
||||
|
|
@ -209,14 +209,15 @@ static void setup(struct input *i, int num, const char *upload)
|
|||
curl_off_t uploadsize;
|
||||
CURL *hnd;
|
||||
|
||||
hnd = i->hnd = curl_easy_init();
|
||||
hnd = i->hnd = NULL;
|
||||
|
||||
i->num = num;
|
||||
curl_msnprintf(filename, 128, "dl-%d", num);
|
||||
out = fopen(filename, "wb");
|
||||
if(!out) {
|
||||
fprintf(stderr, "error: could not open file %s for writing: %s\n", upload,
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
curl_msnprintf(url, 256, "https://localhost:8443/upload-%d", num);
|
||||
|
|
@ -225,7 +226,8 @@ static void setup(struct input *i, int num, const char *upload)
|
|||
if(stat(upload, &file_info)) {
|
||||
fprintf(stderr, "error: could not stat file %s: %s\n", upload,
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
fclose(out);
|
||||
return 1;
|
||||
}
|
||||
|
||||
uploadsize = file_info.st_size;
|
||||
|
|
@ -234,9 +236,12 @@ static void setup(struct input *i, int num, const char *upload)
|
|||
if(!i->in) {
|
||||
fprintf(stderr, "error: could not open file %s for reading: %s\n", upload,
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
fclose(out);
|
||||
return 1;
|
||||
}
|
||||
|
||||
hnd = i->hnd = curl_easy_init();
|
||||
|
||||
/* write to this file */
|
||||
curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
|
||||
|
||||
|
|
@ -269,6 +274,7 @@ static void setup(struct input *i, int num, const char *upload)
|
|||
/* wait for pipe connection to confirm */
|
||||
curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -301,7 +307,8 @@ int main(int argc, char **argv)
|
|||
multi_handle = curl_multi_init();
|
||||
|
||||
for(i = 0; i < num_transfers; i++) {
|
||||
setup(&trans[i], i, filename);
|
||||
if(setup(&trans[i], i, filename))
|
||||
return 1;
|
||||
|
||||
/* add the individual transfer */
|
||||
curl_multi_add_handle(multi_handle, trans[i].hnd);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue