clang-tidy: add to CI, add cmake support, fix fallouts

build:
- autotools: fix to build generated sources for the `tidy` target.
- autotools: allow passing custom clang-tidy options via
  `CURL_CLANG_TIDYFLAGS` env.
- cmake: add `CURL_CLANG_TIDY` option to configure for `clang-tidy`.
  Also add:
  - `CLANG_TIDY` variable to customize the `clang-tidy` tool.
  - `CURL_CLANG_TIDYFLAGS` to pass custom options to `clang-tidy`.
- apply `--enable-werror` and `-DCURL_WERROR=ON` to `clang-tidy`.

CI/GHA:
- add clang-tidy job for Linux, using autotools and clang-tidy v18.
  This one needs to disable `clang-analyzer-valist.Uninitialized`
  to avoid false positives:
  https://github.com/llvm/llvm-project/issues/40656
  Duration: 5.5 minutes
- add clang-tidy job for macOS, using cmake and clang-tidy v19.
  This one also covers tests and examples, and doesn't hit the false
  positives seen with llvm v18 and earlier.
  Duration: 4.5 minutes
- Linux/macOS: skip installing test dependencies when not building or
  running tests.

fix fallouts reported by `clang-tidy`:
- lib:
  - cf-h2-proxy: unused assignment in non-debug builds.
  - cf-socket: silence warning.
    FIXME: https://github.com/curl/curl/pull/15825#issuecomment-2561867769
  - ftp: NULL passed to `strncmp()`.
  - http2: NULL-ptr deref.
  - mprintf: silence warning.
- src/tool_writeout: NULL passed to `fputs()`.
- examples:
  - invalid file pointers.
  - missing `fclose()`.
- tests:
  - http/clients/hx-download: memory leaks on error.
  - http/clients/hx-download: memory leak on repeat `-r` option.
  - server: double `fclose()`.
    https://www.man7.org/linux/man-pages/man3/fclose.3.html
  - server: invalid file pointer/handle.
  - server/getpart: unused assignments.
  - server/mqttd: leak on failed `realloc()`.
  - server/tftpd: NULL passed to `strcmp()`.

Closes #15825
This commit is contained in:
Viktor Szakats 2024-12-24 02:43:02 +01:00
parent 421e592db2
commit fabfa8e402
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
27 changed files with 134 additions and 68 deletions

View file

@ -349,9 +349,6 @@ int getpart(char **outbuf, size_t *outlen,
state = STATE_INMAIN;
csub[0] = '\0';
if(in_wanted_part) {
/* end of wanted part */
in_wanted_part = 0;
/* Do we need to base64 decode the data? */
if(base64) {
error = decodedata(outbuf, outlen);
@ -368,9 +365,6 @@ int getpart(char **outbuf, size_t *outlen,
state = STATE_OUTER;
cmain[0] = '\0';
if(in_wanted_part) {
/* end of wanted part */
in_wanted_part = 0;
/* Do we need to base64 decode the data? */
if(base64) {
error = decodedata(outbuf, outlen);
@ -386,11 +380,8 @@ int getpart(char **outbuf, size_t *outlen,
/* end of outermost file section */
state = STATE_OUTSIDE;
couter[0] = '\0';
if(in_wanted_part) {
/* end of wanted part */
in_wanted_part = 0;
if(in_wanted_part)
break;
}
}
}

View file

@ -542,12 +542,14 @@ static curl_socket_t mqttit(curl_socket_t fd)
break;
if(remaining_length >= buff_size) {
unsigned char *newbuffer;
buff_size = remaining_length;
buffer = realloc(buffer, buff_size);
if(!buffer) {
newbuffer = realloc(buffer, buff_size);
if(!newbuffer) {
logmsg("Failed realloc of size %zu", buff_size);
goto end;
}
buffer = newbuffer;
}
if(remaining_length) {

View file

@ -641,12 +641,10 @@ static void storerequest(char *reqbuf, size_t totalsize)
storerequest_cleanup:
do {
res = fclose(dump);
} while(res && ((error = errno) == EINTR));
res = fclose(dump);
if(res)
logmsg("Error closing file %s error: %d %s",
dumpfile, error, strerror(error));
dumpfile, errno, strerror(errno));
}
/* return 0 on success, non-zero on failure */
@ -976,12 +974,10 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
req->rtp_buffersize = 0;
}
do {
res = fclose(dump);
} while(res && ((error = errno) == EINTR));
res = fclose(dump);
if(res)
logmsg("Error closing file %s error: %d %s",
responsedump, error, strerror(error));
responsedump, errno, strerror(errno));
if(got_exit_signal) {
free(ptr);

View file

@ -1167,7 +1167,7 @@ socks5_cleanup:
sclose(sock);
#ifdef USE_UNIX_SOCKETS
if(unlink_socket && socket_domain == AF_UNIX) {
if(unlink_socket && socket_domain == AF_UNIX && unix_socket) {
error = unlink(unix_socket);
logmsg("unlink(%s) = %d (%s)", unix_socket, error, strerror(error));
}

View file

@ -832,12 +832,10 @@ static void storerequest(const char *reqbuf, size_t totalsize)
storerequest_cleanup:
do {
res = fclose(dump);
} while(res && ((error = errno) == EINTR));
res = fclose(dump);
if(res)
logmsg("Error closing file %s error: %d %s",
dumpfile, error, strerror(error));
dumpfile, errno, strerror(errno));
}
static void init_httprequest(struct httprequest *req)
@ -1217,12 +1215,10 @@ retry:
}
} while((count > 0) && !got_exit_signal);
do {
res = fclose(dump);
} while(res && ((error = errno) == EINTR));
res = fclose(dump);
if(res)
logmsg("Error closing file %s error: %d %s",
responsedump, error, strerror(error));
responsedump, errno, strerror(errno));
if(got_exit_signal) {
free(ptr);
@ -2527,7 +2523,7 @@ sws_cleanup:
sclose(sock);
#ifdef USE_UNIX_SOCKETS
if(unlink_socket && socket_domain == AF_UNIX) {
if(unlink_socket && socket_domain == AF_UNIX && unix_socket) {
rc = unlink(unix_socket);
logmsg("unlink(%s) = %d (%s)", unix_socket, rc, strerror(rc));
}

View file

@ -425,9 +425,9 @@ static int writeit(struct testcase *test, struct tftphdr * volatile *dpp,
bfs[current].counter = ct; /* set size of data to write */
current = !current; /* switch to other buffer */
if(bfs[current].counter != BF_FREE) /* if not free */
write_behind(test, convert); /* flush it */
write_behind(test, convert); /* flush it */
bfs[current].counter = BF_ALLOC; /* mark as alloc'd */
*dpp = &bfs[current].buf.hdr;
*dpp = &bfs[current].buf.hdr;
return ct; /* this is a lie of course */
}
@ -448,7 +448,7 @@ static ssize_t write_behind(struct testcase *test, int convert)
struct tftphdr *dp;
b = &bfs[nextone];
if(b->counter < -1) /* anything to flush? */
if(b->counter < -1) /* anything to flush? */
return 0; /* just nop if nothing to do */
if(!test->ofile) {
@ -464,6 +464,9 @@ static ssize_t write_behind(struct testcase *test, int convert)
return -1; /* failure! */
}
}
else if(test->ofile <= 0) {
return -1; /* failure! */
}
count = b->counter; /* remember byte count */
b->counter = BF_FREE; /* reset flag */
@ -952,7 +955,7 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
break;
} while(1);
if(*cp) {
if(*cp || !mode) {
nak(EBADOP);
fclose(server);
return 3;

View file

@ -341,12 +341,10 @@ void set_advisor_read_lock(const char *filename)
return;
}
do {
res = fclose(lockfile);
} while(res && ((error = errno) == EINTR));
res = fclose(lockfile);
if(res)
logmsg("Error closing lock file %s error: %d %s",
filename, error, strerror(error));
filename, errno, strerror(errno));
}
void clear_advisor_read_lock(const char *filename)