From 568bf99213c90ad2e2c03cc73401232a82228774 Mon Sep 17 00:00:00 2001 From: Robin Linden Date: Thu, 8 Feb 2024 18:15:40 +0100 Subject: [PATCH] cleanup: Add and fix -Wformat-nonliteral --- Makefile | 2 +- src/api.c | 11 +++++------ src/chat.c | 4 ++-- src/chat_commands.c | 4 ++-- src/conference.c | 6 +++--- src/friendlist.c | 6 ++---- src/global_commands.c | 26 ++++++++++++-------------- src/line_info.h | 1 + src/name_lookup.c | 1 + src/notify.h | 4 ++++ src/prompt.c | 15 +++++++-------- src/toxic.c | 3 ++- 12 files changed, 42 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index 50fb1fb70..fb8b861ac 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ CFG_DIR = $(BASE_DIR)/cfg LIBS = toxcore ncursesw libconfig libcurl -CFLAGS ?= -std=c11 -pthread -Wall -Wpedantic -Wunused -fstack-protector-all -Wvla -Wno-missing-braces -Wmissing-prototypes -Wcast-align -Wcast-qual -Wmissing-declarations -Wshadow -Wunused-macros +CFLAGS ?= -std=c11 -pthread -Wall -Wpedantic -Wunused -fstack-protector-all -Wvla -Wno-missing-braces -Wmissing-prototypes -Wcast-align -Wcast-qual -Wmissing-declarations -Wshadow -Wunused-macros -Wformat-nonliteral CFLAGS += '-DTOXICVER="$(VERSION)"' -DHAVE_WIDECHAR -D_XOPEN_SOURCE_EXTENDED -D_FILE_OFFSET_BITS=64 CFLAGS += '-DPACKAGE_DATADIR="$(abspath $(DATADIR))"' CFLAGS += ${USER_CFLAGS} diff --git a/src/api.c b/src/api.c index fe18a9586..812d31f65 100644 --- a/src/api.c +++ b/src/api.c @@ -48,7 +48,7 @@ void api_display(const char *const msg) } self_window = get_active_window(); - line_info_add(self_window, user_toxic->c_config, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); + line_info_add(self_window, user_toxic->c_config, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", msg); } FriendsList api_get_friendslist(void) @@ -145,28 +145,27 @@ void cmd_run(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg const Client_Config *c_config = toxic->c_config; FILE *fp; - const char *error_str; cur_window = window; self_window = self; if (argc != 1) { + const char *error_str; + if (argc < 1) { error_str = "Path must be specified."; } else { error_str = "Only one argument allowed."; } - line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, error_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", error_str); return; } fp = fopen(argv[1], "r"); if (fp == NULL) { - error_str = "Path does not exist."; - - line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, error_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Path does not exist."); return; } diff --git a/src/chat.c b/src/chat.c index ad3c4cfd5..fd02fd47b 100644 --- a/src/chat.c +++ b/src/chat.c @@ -277,7 +277,7 @@ static void chat_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_t num, file_send_queue_check(self, toxic, self->num); msg = "has come online"; - line_info_add(self, c_config, true, nick, NULL, CONNECTION, 0, GREEN, msg); + line_info_add(self, c_config, true, nick, NULL, CONNECTION, 0, GREEN, "%s", msg); write_to_log(ctx->log, c_config, msg, nick, true); } else if (connection_status == TOX_CONNECTION_NONE) { Friends.list[num].is_typing = false; @@ -289,7 +289,7 @@ static void chat_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_t num, chat_pause_file_transfers(num); msg = "has gone offline"; - line_info_add(self, c_config, true, nick, NULL, DISCONNECTION, 0, RED, msg); + line_info_add(self, c_config, true, nick, NULL, DISCONNECTION, 0, RED, "%s", msg); write_to_log(ctx->log, c_config, msg, nick, true); } } diff --git a/src/chat_commands.c b/src/chat_commands.c index 597a8aba4..33103d6bc 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -58,7 +58,7 @@ void cmd_autoaccept_files(WINDOW *window, ToxWindow *self, Toxic *toxic, int arg msg = "Auto-file accept for this friend is disabled; type \"/autoaccept on\" to enable"; } - line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", msg); return; } @@ -74,7 +74,7 @@ void cmd_autoaccept_files(WINDOW *window, ToxWindow *self, Toxic *toxic, int arg msg = "Invalid option. Use \"/autoaccept on\" and \"/autoaccept off\" to toggle auto-file accept"; } - line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", msg); } void cmd_cancelfile(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) diff --git a/src/conference.c b/src/conference.c index a95451453..b05bf457b 100644 --- a/src/conference.c +++ b/src/conference.c @@ -753,7 +753,7 @@ static void update_peer_list(ToxWindow *self, Toxic *toxic, uint32_t conferencen if (new_peer && peer->name_length > 0 && timed_out(chat->start_time, CONFERENCE_EVENT_WAIT)) { const char *msg = "has joined the conference"; - line_info_add(self, c_config, true, peer->name, NULL, CONNECTION, 0, GREEN, msg); + line_info_add(self, c_config, true, peer->name, NULL, CONNECTION, 0, GREEN, "%s", msg); write_to_log(ctx->log, c_config, msg, peer->name, true); } @@ -770,7 +770,7 @@ static void update_peer_list(ToxWindow *self, Toxic *toxic, uint32_t conferencen if (old_peer->active) { if (old_peer->name_length > 0 && !find_peer_by_pubkey(chat->peer_list, chat->num_peers, old_peer->pubkey, NULL)) { const char *msg = "has left the conference"; - line_info_add(self, c_config, true, old_peer->name, NULL, DISCONNECTION, 0, RED, msg); + line_info_add(self, c_config, true, old_peer->name, NULL, DISCONNECTION, 0, RED, "%s", msg); write_to_log(ctx->log, c_config, msg, old_peer->name, true); } @@ -850,7 +850,7 @@ static void conference_onConferencePeerNameChange(ToxWindow *self, Toxic *toxic, // this is kind of a hack; peers always join a group with no name set and then set it after } else if (timed_out(conferences[conferencenum].start_time, CONFERENCE_EVENT_WAIT)) { const char *msg = "has joined the conference"; - line_info_add(self, c_config, true, name, NULL, CONNECTION, 0, GREEN, msg); + line_info_add(self, c_config, true, name, NULL, CONNECTION, 0, GREEN, "%s", msg); write_to_log(ctx->log, c_config, msg, name, true); } } diff --git a/src/friendlist.c b/src/friendlist.c index f1ba13cf2..7ab61702e 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -1067,8 +1067,7 @@ static bool friendlist_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr Friends.list[f].chatwin = add_window(toxic, new_chat(tox, Friends.list[f].num)); set_active_window_index(Friends.list[f].chatwin); } else { - const char *msg = "* Warning: Too many windows are open."; - line_info_add(home_window, c_config, false, NULL, NULL, SYS_MSG, 0, RED, msg); + line_info_add(home_window, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "* Warning: Too many windows are open."); sound_notify(home_window, toxic, notif_error, NT_WNDALERT_1, NULL); } @@ -1465,8 +1464,7 @@ static void friendlist_onAV(ToxWindow *self, Toxic *toxic, uint32_t friend_numbe get_nick_truncate(tox, nick, Friends.list[friend_number].num); line_info_add(home_window, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Audio action from: %s!", nick); - const char *errmsg = "* Warning: Too many windows are open."; - line_info_add(home_window, c_config, false, NULL, NULL, SYS_MSG, 0, RED, errmsg); + line_info_add(home_window, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "* Warning: Too many windows are open."); sound_notify(home_window, toxic, notif_error, NT_WNDALERT_1, NULL); } diff --git a/src/global_commands.c b/src/global_commands.c index 38bd1821a..a7d7111e0 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -150,7 +150,7 @@ void cmd_add_helper(ToxWindow *self, Toxic *toxic, const char *id_bin, const cha break; } - line_info_add(self, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, 0, errmsg); + line_info_add(self, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", errmsg); } void cmd_add(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -719,17 +719,18 @@ void cmd_log(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg const Client_Config *c_config = toxic->c_config; - const char *msg; struct chatlog *log = self->chatwin->log; if (argc == 0) { + const char *msg; + if (log->log_on) { msg = "Logging for this window is ON; type \"/log off\" to disable. (Logs are not encrypted)"; } else { msg = "Logging for this window is OFF; type \"/log on\" to enable."; } - line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", msg); return; } @@ -739,7 +740,7 @@ void cmd_log(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg if (log_enable(log) == 0) { char e_msg[MAX_STR_SIZE]; snprintf(e_msg, sizeof(e_msg), "Logging to: %s", log->path); - line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, e_msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", e_msg); return; } @@ -754,13 +755,12 @@ void cmd_log(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg log_disable(log); - msg = "Logging disabled."; - line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Logging disabled."); return; } - msg = "Invalid option. Use \"/log on\" and \"/log off\" to toggle logging."; - line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Invalid option. Use \"/log on\" and \"/log off\" to toggle logging."); } void cmd_myid(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -1052,13 +1052,11 @@ void cmd_status(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* Tox *tox = toxic->tox; const Client_Config *c_config = toxic->c_config; - const char *errmsg = NULL; - lock_status(); if (argc < 1) { - errmsg = "Require a status. Statuses are: online, busy and away."; - line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, errmsg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Require a status. Statuses are: online, busy and away."); goto finish; } @@ -1072,8 +1070,8 @@ void cmd_status(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* } else if (!strcasecmp(status_str, "busy")) { status = TOX_USER_STATUS_BUSY; } else { - errmsg = "Invalid status. Valid statuses are: online, busy and away."; - line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, errmsg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Invalid status. Valid statuses are: online, busy and away."); goto finish; } diff --git a/src/line_info.h b/src/line_info.h index 0e0e796a1..fbed7eb99 100644 --- a/src/line_info.h +++ b/src/line_info.h @@ -86,6 +86,7 @@ struct history { * Returns the id of the new line. * Returns -1 on failure. */ +__attribute__((format(printf, 9, 10))) int line_info_add(ToxWindow *self, const Client_Config *c_config, bool show_timestamp, const char *name1, const char *name2, LINE_TYPE type, uint8_t bold, uint8_t colour, const char *msg, ...); diff --git a/src/name_lookup.c b/src/name_lookup.c index 032167497..e94425f14 100644 --- a/src/name_lookup.c +++ b/src/name_lookup.c @@ -69,6 +69,7 @@ static void clear_thread_data(void) }; } +__attribute__((format(printf, 3, 4))) static int lookup_error(ToxWindow *self, const Client_Config *c_config, const char *errmsg, ...) { char frmt_msg[MAX_STR_SIZE]; diff --git a/src/notify.h b/src/notify.h index 65b7291cb..2521e538a 100644 --- a/src/notify.h +++ b/src/notify.h @@ -71,13 +71,17 @@ int sound_notify2(ToxWindow *self, const Toxic *toxic, Notification notif, uint6 void stop_sound(int id); +__attribute__((format(printf, 7, 8))) int box_notify(ToxWindow *self, const Toxic *toxic, Notification notif, uint64_t flags, int *id_indicator, const char *title, const char *format, ...); +__attribute__((format(printf, 6, 7))) int box_notify2(ToxWindow *self, const Toxic *toxic, Notification notif, uint64_t flags, int id, const char *format, ...); +__attribute__((format(printf, 6, 7))) int box_silent_notify(ToxWindow *self, const Toxic *toxic, uint64_t flags, int *id_indicator, const char *title, const char *format, ...); +__attribute__((format(printf, 5, 6))) int box_silent_notify2(ToxWindow *self, const Toxic *toxic, uint64_t flags, int id, const char *format, ...); diff --git a/src/prompt.c b/src/prompt.c index 27cfc5efd..714e34719 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -520,7 +520,7 @@ static void prompt_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_t fr if (connection_status != TOX_CONNECTION_NONE && Friends.list[friendnum].connection_status == TOX_CONNECTION_NONE) { msg = "has come online"; - line_info_add(self, c_config, true, nick, NULL, CONNECTION, 0, GREEN, msg); + line_info_add(self, c_config, true, nick, NULL, CONNECTION, 0, GREEN, "%s", msg); write_to_log(ctx->log, c_config, msg, nick, true); if (self->active_box != -1) { @@ -532,7 +532,7 @@ static void prompt_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_t fr } } else if (connection_status == TOX_CONNECTION_NONE) { msg = "has gone offline"; - line_info_add(self, c_config, true, nick, NULL, DISCONNECTION, 0, RED, msg); + line_info_add(self, c_config, true, nick, NULL, DISCONNECTION, 0, RED, "%s", msg); write_to_log(ctx->log, c_config, msg, nick, true); if (self->active_box != -1) { @@ -589,8 +589,7 @@ static void prompt_onFriendRequest(ToxWindow *self, Toxic *toxic, const char *ke const int n = add_friend_request(key, data); if (n == -1) { - const char *errmsg = "Friend request queue is full. Discarding request."; - line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, errmsg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Friend request queue is full. Discarding request."); return; } @@ -660,10 +659,10 @@ static void print_welcome_msg(ToxWindow *self, const Client_Config *c_config) " |_| \\___/_/\\_\\___\\____| v." TOXICVER); line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, ""); - const char *msg = "Welcome to Toxic, a free, open source Tox-based instant messaging client."; - line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 1, CYAN, msg); - msg = "Type \"/help\" for assistance. Further help may be found via the man page."; - line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 1, CYAN, msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 1, CYAN, + "Welcome to Toxic, a free, open source Tox-based instant messaging client."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 1, CYAN, + "Type \"/help\" for assistance. Further help may be found via the man page."); line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, ""); } diff --git a/src/toxic.c b/src/toxic.c index 530dbae54..2ab81789f 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -483,6 +483,7 @@ static struct _init_messages { } init_messages; /* One-time queue for messages created during init. Do not use after program init. */ +__attribute__((format(printf, 1, 2))) static void queue_init_message(const char *msg, ...) { char frmt_msg[MAX_STR_SIZE] = {0}; @@ -528,7 +529,7 @@ static void cleanup_init_messages(void) static void print_init_messages(ToxWindow *home_window, const Client_Config *c_config) { for (int i = 0; i < init_messages.num; ++i) { - line_info_add(home_window, c_config, NULL, NULL, NULL, SYS_MSG, 0, 0, init_messages.msgs[i]); + line_info_add(home_window, c_config, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", init_messages.msgs[i]); } }