build: enable missing OpenSSF-recommended warnings, with fixes

https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
as of 2023-11-29 [1].

Enable new recommended warnings (except `-Wsign-conversion`):

- enable `-Wformat=2` for clang (in both cmake and autotools).
- add `CURL_PRINTF()` internal attribute and mark functions accepting
  printf arguments with it. This is a copy of existing
  `CURL_TEMP_PRINTF()` but using `__printf__` to make it compatible
  with redefinting the `printf` symbol:
  https://gcc.gnu.org/onlinedocs/gcc-3.0.4/gcc_5.html#SEC94
- fix `CURL_PRINTF()` and existing `CURL_TEMP_PRINTF()` for
  mingw-w64 and enable it on this platform.
- enable `-Wimplicit-fallthrough`.
- enable `-Wtrampolines`.
- add `-Wsign-conversion` commented with a FIXME.
- cmake: enable `-pedantic-errors` the way we do it with autotools.
  Follow-up to d5c0351055 #2747
- lib/curl_trc.h: use `CURL_FORMAT()`, this also fixes it to enable format
  checks. Previously it was always disabled due to the internal `printf`
  macro.

Fix them:

- fix bug where an `set_ipv6_v6only()` call was missed in builds with
  `--disable-verbose` / `CURL_DISABLE_VERBOSE_STRINGS=ON`.
- add internal `FALLTHROUGH()` macro.
- replace obsolete fall-through comments with `FALLTHROUGH()`.
- fix fallthrough markups: Delete redundant ones (showing up as
  warnings in most cases). Add missing ones. Fix indentation.
- silence `-Wformat-nonliteral` warnings with llvm/clang.
- fix one `-Wformat-nonliteral` warning.
- fix new `-Wformat` and `-Wformat-security` warnings.
- fix `CURL_FORMAT_SOCKET_T` value for mingw-w64. Also move its
  definition to `lib/curl_setup.h` allowing use in `tests/server`.
- lib: fix two wrongly passed string arguments in log outputs.
  Co-authored-by: Jay Satiro
- fix new `-Wformat` warnings on mingw-w64.

[1] 56c0fde389/docs/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C%2B%2B.md

Closes #12489
This commit is contained in:
Viktor Szakats 2023-12-08 13:05:09 +00:00
parent ba8752e556
commit 3829759bd0
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
88 changed files with 531 additions and 318 deletions

View file

@ -217,7 +217,7 @@ int tool_debug_cb(CURL *handle, curl_infotype type,
switch(type) {
case CURLINFO_TEXT:
fprintf(output, "%s%s== Info: %.*s", timebuf, idsbuf, (int)size, data);
/* FALLTHROUGH */
FALLTHROUGH();
default: /* in case a new one is introduced to shock us */
return 0;

View file

@ -208,7 +208,14 @@ int tool_progress_cb(void *clientp,
memset(line, '#', num);
line[num] = '\0';
msnprintf(format, sizeof(format), "\r%%-%ds %%5.1f%%%%", barwidth);
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"
#endif
fprintf(bar->out, format, line, percent);
#ifdef __clang__
#pragma clang diagnostic pop
#endif
}
fflush(bar->out);
bar->prev = point;

View file

@ -113,7 +113,14 @@ CURLcode easysrc_addf(struct slist_wc **plist, const char *fmt, ...)
char *bufp;
va_list ap;
va_start(ap, fmt);
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"
#endif
bufp = curlx_mvaprintf(fmt, ap);
#ifdef __clang__
#pragma clang diagnostic pop
#endif
va_end(ap);
if(!bufp) {
ret = CURLE_OUT_OF_MEMORY;

View file

@ -40,7 +40,7 @@ extern int easysrc_slist_count; /* Number of curl_slist variables */
extern CURLcode easysrc_init(void);
extern CURLcode easysrc_add(struct slist_wc **plist, const char *bupf);
extern CURLcode easysrc_addf(struct slist_wc **plist,
const char *fmt, ...);
const char *fmt, ...) CURL_PRINTF(2, 3);
extern CURLcode easysrc_perform(void);
extern CURLcode easysrc_cleanup(void);

View file

@ -278,7 +278,7 @@ static CURLcode tool2curlparts(CURL *curl, struct tool_mime *m,
case TOOLMIME_STDIN:
if(!filename)
filename = "-";
/* FALLTHROUGH */
FALLTHROUGH();
case TOOLMIME_STDINDATA:
ret = curl_mime_data_cb(part, m->size,
(curl_read_callback) tool_mime_stdin_read,

View file

@ -2337,7 +2337,7 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
config->file_clobber_mode = toggle ? CLOBBER_ALWAYS : CLOBBER_NEVER;
break;
}
/* FALLTHROUGH */
FALLTHROUGH();
case 'o': /* --output */
/* output file */
{
@ -2640,7 +2640,7 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
switch(*nextarg) {
case '+':
nextarg++;
/* FALLTHROUGH */
FALLTHROUGH();
default:
/* If-Modified-Since: (section 14.28 in RFC2068) */
config->timecond = CURL_TIMECOND_IFMODSINCE;

View file

@ -275,22 +275,19 @@ clean:
curl_free(pathbuffer);
curl_url_cleanup(gatewayurl);
{
const char *msg = NULL;
switch(result) {
case CURLE_URL_MALFORMAT:
msg = "malformed target URL";
helpf(tool_stderr, "malformed target URL");
break;
case CURLE_FILE_COULDNT_READ_FILE:
msg = "IPFS automatic gateway detection failed";
helpf(tool_stderr, "IPFS automatic gateway detection failed");
break;
case CURLE_BAD_FUNCTION_ARGUMENT:
msg = "--ipfs-gateway was given a malformed URL";
helpf(tool_stderr, "--ipfs-gateway was given a malformed URL");
break;
default:
break;
}
if(msg)
helpf(tool_stderr, msg);
}
return result;
}

View file

@ -48,7 +48,14 @@ static void voutf(struct GlobalConfig *config,
char *ptr;
char *print_buffer;
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"
#endif
print_buffer = curlx_mvaprintf(fmt, ap);
#ifdef __clang__
#pragma clang diagnostic pop
#endif
if(!print_buffer)
return;
len = strlen(print_buffer);
@ -119,7 +126,14 @@ void helpf(FILE *errors, const char *fmt, ...)
va_start(ap, fmt);
DEBUGASSERT(!strchr(fmt, '\n'));
fputs("curl: ", errors); /* prefix it */
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"
#endif
vfprintf(errors, fmt, ap);
#ifdef __clang__
#pragma clang diagnostic pop
#endif
va_end(ap);
fputs("\n", errors); /* newline it */
}

View file

@ -26,9 +26,13 @@
#include "tool_setup.h"
#include "tool_cfgable.h"
void warnf(struct GlobalConfig *config, const char *fmt, ...);
void notef(struct GlobalConfig *config, const char *fmt, ...);
void helpf(FILE *errors, const char *fmt, ...);
void errorf(struct GlobalConfig *config, const char *fmt, ...);
void warnf(struct GlobalConfig *config, const char *fmt, ...)
CURL_PRINTF(2, 3);
void notef(struct GlobalConfig *config, const char *fmt, ...)
CURL_PRINTF(2, 3);
void helpf(FILE *errors, const char *fmt, ...)
CURL_PRINTF(2, 3);
void errorf(struct GlobalConfig *config, const char *fmt, ...)
CURL_PRINTF(2, 3);
#endif /* HEADER_CURL_TOOL_MSGS_H */

View file

@ -408,7 +408,7 @@ ParameterError proto2num(struct OperationConfig *config,
break;
case set:
protoset[0] = NULL;
/* FALLTHROUGH */
FALLTHROUGH();
case allow:
protoset_set(protoset, p);
break;

View file

@ -247,8 +247,15 @@ static char *c_escape(const char *str, curl_off_t len)
format = "\\%03o";
}
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"
#endif
result = curlx_dyn_addf(&escaped, format,
(unsigned int) *(unsigned char *) s);
#ifdef __clang__
#pragma clang diagnostic pop
#endif
}
}
}
@ -431,7 +438,7 @@ static CURLcode libcurl_generate_mime_part(CURL *curl,
case TOOLMIME_STDIN:
if(!filename)
filename = "-";
/* FALLTHROUGH */
FALLTHROUGH();
case TOOLMIME_STDINDATA:
/* Can only be reading stdin in the current context. */
CODE1("curl_mime_data_cb(part%d, -1, (curl_read_callback) fread, \\",

View file

@ -127,7 +127,7 @@ static CURLcode glob_set(struct URLGlob *glob, char **patternp,
if(multiply(amount, pat->content.Set.size + 1))
return GLOBERROR("range overflow", 0, CURLE_URL_MALFORMAT);
/* FALLTHROUGH */
FALLTHROUGH();
case ',':
*buf = '\0';
@ -171,7 +171,7 @@ static CURLcode glob_set(struct URLGlob *glob, char **patternp,
++pattern;
++(*posp);
}
/* FALLTHROUGH */
FALLTHROUGH();
default:
*buf++ = *pattern++; /* copy character to set element */
++(*posp);