From 39ecf70bb88f71d5e13015ba143b946ccd8f02b4 Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Wed, 9 Aug 2017 15:42:48 +0100 Subject: [PATCH] Silence more gcc format-string warnings. By enabling more checks, gcc has found some real bugs, e.g. `printf(buf, "%d", i)'. Other changes required duplicating the formatting call with a constant format string; a local macro was used to avoid repeating all the many arguments. --- sbr/ext_hook.c | 2 +- sbr/message_id.c | 34 +++++++++++++++++++++------------- sbr/oauth.c | 9 +++++---- uip/annosbr.c | 4 ++-- uip/fmtdump.c | 6 +++--- uip/mhlogin.c | 8 ++++---- uip/sendsbr.c | 6 +++--- 7 files changed, 39 insertions(+), 30 deletions(-) diff --git a/sbr/ext_hook.c b/sbr/ext_hook.c index d29c046a..63869e57 100644 --- a/sbr/ext_hook.c +++ b/sbr/ext_hook.c @@ -50,7 +50,7 @@ ext_hook(char *hook_name, char *message_file_name_1, char *message_file_name_2) if (did_message == 0) { char *msghook; if ((msghook = context_find("msg-hook")) != NULL) - inform(msghook); + inform("%s", msghook); else { char errbuf[BUFSIZ]; snprintf(errbuf, sizeof(errbuf), "external hook \"%s\"", hook); diff --git a/sbr/message_id.c b/sbr/message_id.c index aaaab322..a66d8764 100644 --- a/sbr/message_id.c +++ b/sbr/message_id.c @@ -38,18 +38,19 @@ char * message_id (time_t tclock, int content_id) { switch (message_id_style) { case NMH_MESSAGE_ID_LOCALNAME: { - char *format = content_id ? "<%d.%ld.%%d@%s>" : "<%d.%ld@%s>"; - - snprintf (message_id_, sizeof message_id_, format, - (int) getpid (), (long) tclock, LocalName (1)); - +#define P(fmt) \ + snprintf(message_id_, sizeof message_id_, \ + (fmt), (int)getpid(), (long)tclock, LocalName(1)) + + if (content_id) + P("<%d.%ld.%%d@%s>"); + else + P("<%d.%ld@%s>"); +#undef P break; } case NMH_MESSAGE_ID_RANDOM: { - char *format = content_id - ? "<%d-%ld.%06ld%%d@%.*s.%.*s.%.*s>" - : "<%d-%ld.%06ld@%.*s.%.*s.%.*s>"; /* Use a sequence of digits divisible by 3 because that will expand to base64 without any waste. Must be shorter than 58, see below. */ @@ -95,11 +96,18 @@ message_id (time_t tclock, int content_id) { /* The format string inserts a couple of dots, for the benefit of spam filters that want to see a message id with a final part that resembles a hostname. */ - snprintf (message_id_, sizeof message_id_, format, - getpid (), (long) now.tv_sec, (long) now.tv_usec, - one_third, rnd_base64, - one_third, &rnd_base64[one_third], - one_third, &rnd_base64[2*one_third]); +#define P(fmt) \ + snprintf(message_id_, sizeof message_id_, (fmt), \ + getpid(), (long)now.tv_sec, (long)now.tv_usec, \ + (int)one_third, rnd_base64, \ + (int)one_third, &rnd_base64[one_third], \ + (int)one_third, &rnd_base64[2*one_third]) + + if (content_id) + P("<%d-%ld.%06ld%%d@%.*s.%.*s.%.*s>"); + else + P("<%d-%ld.%06ld@%.*s.%.*s.%.*s>"); +#undef P } break; diff --git a/sbr/oauth.c b/sbr/oauth.c index 76ff8794..89288c1e 100755 --- a/sbr/oauth.c +++ b/sbr/oauth.c @@ -140,7 +140,8 @@ mh_oauth_do_xoauth(const char *user, const char *svc, unsigned char **oauth_res, FILE *fp; char *client_res; - if (!mh_oauth_new (&ctx, svc)) adios(NULL, mh_oauth_get_err_string(ctx)); + if (!mh_oauth_new (&ctx, svc)) + adios(NULL, "%s", mh_oauth_get_err_string(ctx)); if (log != NULL) mh_oauth_log_to(stderr, ctx); @@ -157,7 +158,7 @@ mh_oauth_do_xoauth(const char *user, const char *svc, unsigned char **oauth_res, } if ((cred = mh_oauth_cred_load(fp, ctx, user)) == NULL) { - adios(NULL, mh_oauth_get_err_string(ctx)); + adios(NULL, "%s", mh_oauth_get_err_string(ctx)); } if (!mh_oauth_access_token_valid(time(NULL), cred)) { @@ -169,12 +170,12 @@ mh_oauth_do_xoauth(const char *user, const char *svc, unsigned char **oauth_res, adios(NULL, "credentials rejected -- run mhlogin -saslmech xoauth2 -authservice %s", svc); } inform("error refreshing OAuth2 token"); - adios(NULL, mh_oauth_get_err_string(ctx)); + adios(NULL, "%s", mh_oauth_get_err_string(ctx)); } fseek(fp, 0, SEEK_SET); if (!mh_oauth_cred_save(fp, cred, user)) { - adios(NULL, mh_oauth_get_err_string(ctx)); + adios(NULL, "%s", mh_oauth_get_err_string(ctx)); } } diff --git a/uip/annosbr.c b/uip/annosbr.c index 2c1ce75a..cf5ea55a 100644 --- a/uip/annosbr.c +++ b/uip/annosbr.c @@ -52,7 +52,7 @@ annotate (char *file, char *comp, char *text, int inplace, int datesw, int delet } if (stat(file, &s) == -1) { - advise("can't get access and modification times for %s", file); + inform("can't get access and modification times for %s", file); preserve_actime_and_modtime = 0; } @@ -62,7 +62,7 @@ annotate (char *file, char *comp, char *text, int inplace, int datesw, int delet i = annosbr (fd, file, comp, text, inplace, datesw, delete, append); if (preserve_actime_and_modtime && utime(file, &b) == -1) - advise("can't set access and modification times for %s", file); + inform("can't set access and modification times for %s", file); lkclosedata (fd, file); return i; diff --git a/uip/fmtdump.c b/uip/fmtdump.c index 6a6ef7cb..55d7dcc7 100644 --- a/uip/fmtdump.c +++ b/uip/fmtdump.c @@ -439,7 +439,7 @@ f_typestr(int t) case FT_V_MATCH: return("V_MATCH"); case FT_V_AMATCH: return("V_AMATCH"); default: - printf(buf, "/* ??? #%d */", t); + snprintf(buf, sizeof buf, "/* ??? #%d */", t); return(buf); } } @@ -457,7 +457,7 @@ c_typestr(int t) buf[0] = '\0'; if (t & ~(CT_ADDR|CT_DATE)) - printf(buf, "0x%x ", t); + snprintf(buf, sizeof buf, "0x%x ", t); strcat(buf, "<"); i = 0; FNORD(CT_ADDR, "ADDR"); @@ -474,7 +474,7 @@ c_flagsstr(int t) buf[0] = '\0'; if (t & ~(CF_TRUE|CF_PARSED|CF_DATEFAB|CF_TRIMMED)) - printf(buf, "0x%x ", t); + snprintf(buf, sizeof buf, "0x%x ", t); strcat(buf, "<"); i = 0; FNORD(CF_TRUE, "TRUE"); diff --git a/uip/mhlogin.c b/uip/mhlogin.c index 1cc1c23c..3e10f99e 100644 --- a/uip/mhlogin.c +++ b/uip/mhlogin.c @@ -64,7 +64,7 @@ do_login(const char *svc, const char *user, const char *browser, int snoop) } if (!mh_oauth_new(&ctx, svc)) { - adios(NULL, mh_oauth_get_err_string(ctx)); + adios(NULL, "%s", mh_oauth_get_err_string(ctx)); } if (snoop) { @@ -74,7 +74,7 @@ do_login(const char *svc, const char *user, const char *browser, int snoop) fn = mh_xstrdup(mh_oauth_cred_fn(svc)); if ((url = mh_oauth_get_authorize_url(ctx)) == NULL) { - adios(NULL, mh_oauth_get_err_string(ctx)); + adios(NULL, "%s", mh_oauth_get_err_string(ctx)); } if (browser) { @@ -110,7 +110,7 @@ do_login(const char *svc, const char *user, const char *browser, int snoop) } if (cred == NULL) { inform("error exchanging code for OAuth2 token"); - adios(NULL, mh_oauth_get_err_string(ctx)); + adios(NULL, "%s", mh_oauth_get_err_string(ctx)); } cred_file = lkfopendata(fn, "r+", &failed_to_lock); @@ -121,7 +121,7 @@ do_login(const char *svc, const char *user, const char *browser, int snoop) adios(fn, "oops"); } if (!mh_oauth_cred_save(cred_file, cred, user)) { - adios(NULL, mh_oauth_get_err_string(ctx)); + adios(NULL, "%s", mh_oauth_get_err_string(ctx)); } if (lkfclosedata(cred_file, fn) != 0) { adios (fn, "oops"); diff --git a/uip/sendsbr.c b/uip/sendsbr.c index 2d4d0a86..7d54f43b 100644 --- a/uip/sendsbr.c +++ b/uip/sendsbr.c @@ -135,7 +135,7 @@ sendsbr (char **vec, int vecp, char *program, char *draft, struct stat *st, #ifdef OAUTH_SUPPORT const char *errmsg; if (setup_oauth_params(vec, nvecsp, auth_svc, &errmsg) != OK) { - adios(NULL, errmsg); + adios(NULL, "%s", errmsg); } #else adios(NULL, "send built without OAUTH_SUPPORT, " @@ -751,7 +751,7 @@ handle_sendfrom(char **vec, int *vecp, char *draft, const char *auth_svc) { /* Extract address and host from From: header line in draft. */ if (get_from_header_info(draft, &addr, &host, &message) != OK) { - adios(draft, message); + adios(draft, "%s", message); } /* Merge in any address or host specific switches to post(1) from profile. */ @@ -768,7 +768,7 @@ handle_sendfrom(char **vec, int *vecp, char *draft, const char *auth_svc) { if (strcmp(*vp, "xoauth2") == 0) { #ifdef OAUTH_SUPPORT if (setup_oauth_params(vec, vecp, auth_svc, &message) != OK) { - adios(NULL, message); + adios(NULL, "%s", message); } break; #else -- 2.56.0