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:
Viktor Szakats 2025-02-27 11:32:43 +01:00
parent 2e585f5640
commit 08c7c937dc
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
22 changed files with 222 additions and 150 deletions

View file

@ -482,7 +482,7 @@ AC_DEFUN([CURL_COMPILER_WORKS_IFELSE], [
#endif
]],[[
int i = 0;
exit(i);
return i;
]])
],[
tmp_compiler_works="yes"

View file

@ -1270,11 +1270,12 @@ AC_DEFUN([CURL_CHECK_FUNC_GETADDRINFO], [
struct addrinfo hints;
struct addrinfo *ai = 0;
int error;
int exitcode;
#ifdef _WIN32
WSADATA wsa;
if(WSAStartup(MAKEWORD(2, 2), &wsa))
exit(2);
return 2;
#endif
memset(&hints, 0, sizeof(hints));
@ -1283,9 +1284,15 @@ AC_DEFUN([CURL_CHECK_FUNC_GETADDRINFO], [
hints.ai_socktype = SOCK_STREAM;
error = getaddrinfo("127.0.0.1", 0, &hints, &ai);
if(error || !ai)
exit(1); /* fail */
else
exit(0);
exitcode = 1; /* fail */
else {
freeaddrinfo(ai);
exitcode = 0;
}
#ifdef _WIN32
WSACleanup();
#endif
return exitcode;
]])
],[
AC_MSG_RESULT([yes])
@ -1883,9 +1890,11 @@ AC_DEFUN([CURL_CHECK_FUNC_GETIFADDRS], [
error = getifaddrs(&ifa);
if(error || !ifa)
exit(1); /* fail */
else
exit(0);
return 1; /* fail */
else {
freeifaddrs(ifa);
return 0;
}
]])
],[
AC_MSG_RESULT([yes])
@ -2003,9 +2012,9 @@ AC_DEFUN([CURL_CHECK_FUNC_GMTIME_R], [
gmt = gmtime_r(&local, &result);
(void)result;
if(gmt)
exit(0);
return 0;
else
exit(1);
return 1;
]])
],[
AC_MSG_RESULT([yes])
@ -2134,13 +2143,13 @@ AC_DEFUN([CURL_CHECK_FUNC_INET_NTOP], [
/* - */
ipv4ptr = inet_ntop(AF_INET, ipv4a, ipv4res, sizeof(ipv4res));
if(!ipv4ptr)
exit(1); /* fail */
return 1; /* fail */
if(ipv4ptr != ipv4res)
exit(1); /* fail */
return 1; /* fail */
if(!ipv4ptr[0])
exit(1); /* fail */
return 1; /* fail */
if(memcmp(ipv4res, "192.168.100.1", 13) != 0)
exit(1); /* fail */
return 1; /* fail */
/* - */
ipv6res[0] = '\0';
memset(ipv6a, 0, sizeof(ipv6a));
@ -2158,15 +2167,15 @@ AC_DEFUN([CURL_CHECK_FUNC_INET_NTOP], [
/* - */
ipv6ptr = inet_ntop(AF_INET6, ipv6a, ipv6res, sizeof(ipv6res));
if(!ipv6ptr)
exit(1); /* fail */
return 1; /* fail */
if(ipv6ptr != ipv6res)
exit(1); /* fail */
return 1; /* fail */
if(!ipv6ptr[0])
exit(1); /* fail */
return 1; /* fail */
if(memcmp(ipv6res, "fe80::214:4fff:fe0b:76c8", 24) != 0)
exit(1); /* fail */
return 1; /* fail */
/* - */
exit(0);
return 0;
]])
],[
AC_MSG_RESULT([yes])
@ -2286,18 +2295,18 @@ AC_DEFUN([CURL_CHECK_FUNC_INET_PTON], [
/* - */
memset(ipv4a, 1, sizeof(ipv4a));
if(1 != inet_pton(AF_INET, ipv4src, ipv4a))
exit(1); /* fail */
return 1; /* fail */
/* - */
if( (ipv4a[0] != 0xc0) ||
(ipv4a[1] != 0xa8) ||
(ipv4a[2] != 0x64) ||
(ipv4a[3] != 0x01) ||
(ipv4a[4] != 0x01) )
exit(1); /* fail */
return 1; /* fail */
/* - */
memset(ipv6a, 1, sizeof(ipv6a));
if(1 != inet_pton(AF_INET6, ipv6src, ipv6a))
exit(1); /* fail */
return 1; /* fail */
/* - */
if( (ipv6a[0] != 0xfe) ||
(ipv6a[1] != 0x80) ||
@ -2310,7 +2319,7 @@ AC_DEFUN([CURL_CHECK_FUNC_INET_PTON], [
(ipv6a[14] != 0x76) ||
(ipv6a[15] != 0xc8) ||
(ipv6a[16] != 0x01) )
exit(1); /* fail */
return 1; /* fail */
/* - */
if( (ipv6a[2] != 0x0) ||
(ipv6a[3] != 0x0) ||
@ -2318,9 +2327,9 @@ AC_DEFUN([CURL_CHECK_FUNC_INET_PTON], [
(ipv6a[5] != 0x0) ||
(ipv6a[6] != 0x0) ||
(ipv6a[7] != 0x0) )
exit(1); /* fail */
return 1; /* fail */
/* - */
exit(0);
return 0;
]])
],[
AC_MSG_RESULT([yes])
@ -3809,11 +3818,11 @@ AC_DEFUN([CURL_CHECK_FUNC_STRERROR_R], [
buffer[0] = '\0';
string = strerror_r(EACCES, buffer, sizeof(buffer));
if(!string)
exit(1); /* fail */
return 1; /* fail */
if(!string[0])
exit(1); /* fail */
return 1; /* fail */
else
exit(0);
return 0;
]])
],[
AC_MSG_RESULT([yes])
@ -3872,11 +3881,11 @@ AC_DEFUN([CURL_CHECK_FUNC_STRERROR_R], [
buffer[0] = '\0';
error = strerror_r(EACCES, buffer, sizeof(buffer));
if(error)
exit(1); /* fail */
return 1; /* fail */
if(buffer[0] == '\0')
exit(1); /* fail */
return 1; /* fail */
else
exit(0);
return 0;
]])
],[
AC_MSG_RESULT([yes])