1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-09-28 02:25:35 +02:00

enforce const correctess, fix undefined behaviour with string literals

This commit is contained in:
Jfreegman 2014-07-29 14:54:34 -04:00
parent cbe47b3660
commit 973f6206ee
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
9 changed files with 68 additions and 59 deletions

View File

@ -166,15 +166,15 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
return; return;
} }
char *path = argv[1]; if (argv[1][0] != '\"') {
if (path[0] != '\"') {
errmsg = "File path must be enclosed in quotes."; errmsg = "File path must be enclosed in quotes.";
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg);
return; return;
} }
++path; /* remove opening and closing quotes */
char path[MAX_STR_SIZE];
snprintf(path, sizeof(path), "%s", &argv[1][1]);
int path_len = strlen(path) - 1; int path_len = strlen(path) - 1;
path[path_len] = '\0'; path[path_len] = '\0';

View File

@ -289,7 +289,7 @@ void *dns3_lookup_thread(void *data)
} }
/* creates new thread for dns3 lookup. Only allows one lookup at a time. */ /* creates new thread for dns3 lookup. Only allows one lookup at a time. */
void dns3_lookup(ToxWindow *self, Tox *m, char *id_bin, char *addr, char *msg) void dns3_lookup(ToxWindow *self, Tox *m, const char *id_bin, const char *addr, const char *msg)
{ {
if (t_data.busy) { if (t_data.busy) {
const char *err = "Please wait for previous user lookup to finish."; const char *err = "Please wait for previous user lookup to finish.";

View File

@ -27,6 +27,6 @@
#define _dns_h #define _dns_h
/* creates new thread for dns3 lookup. Only allows one lookup at a time. */ /* creates new thread for dns3 lookup. Only allows one lookup at a time. */
void dns3_lookup(ToxWindow *self, Tox *m, char *id_bin, char *addr, char *msg); void dns3_lookup(ToxWindow *self, Tox *m, const char *id_bin, const char *addr, const char *msg);
#endif /* #define _dns_h */ #endif /* #define _dns_h */

View File

@ -92,7 +92,7 @@ void cmd_accept(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[
void cmd_add_helper(ToxWindow *self, Tox *m, char *id_bin, char *msg) void cmd_add_helper(ToxWindow *self, Tox *m, char *id_bin, char *msg)
{ {
char *errmsg; const char *errmsg;
int32_t f_num = tox_add_friend(m, (uint8_t *) id_bin, (uint8_t *) msg, (uint16_t) strlen(msg)); int32_t f_num = tox_add_friend(m, (uint8_t *) id_bin, (uint8_t *) msg, (uint16_t) strlen(msg));
switch (f_num) { switch (f_num) {
@ -135,7 +135,7 @@ void cmd_add_helper(ToxWindow *self, Tox *m, char *id_bin, char *msg)
void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
char *errmsg; const char *errmsg;
if (argc < 1) { if (argc < 1) {
errmsg = "Invalid syntax."; errmsg = "Invalid syntax.";
@ -143,21 +143,22 @@ void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
return; return;
} }
char *id = argv[1]; const char *id = argv[1];
char msg[MAX_STR_SIZE]; char msg[MAX_STR_SIZE];
if (argc > 1) { if (argc > 1) {
char *temp = argv[2]; if (argv[2][0] != '\"') {
if (temp[0] != '\"') {
errmsg = "Message must be enclosed in quotes."; errmsg = "Message must be enclosed in quotes.";
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg);
return; return;
} }
++temp; /* remove opening and closing quotes */
temp[strlen(temp) - 1] = '\0'; char tmp[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "%s", temp); snprintf(tmp, sizeof(tmp), "%s", &argv[2][1]);
int len = strlen(tmp) - 1;
tmp[len] = '\0';
snprintf(msg, sizeof(msg), "%s", tmp);
} else { } else {
char selfname[TOX_MAX_NAME_LENGTH]; char selfname[TOX_MAX_NAME_LENGTH];
uint16_t n_len = tox_get_self_name(m, (uint8_t *) selfname); uint16_t n_len = tox_get_self_name(m, (uint8_t *) selfname);
@ -204,7 +205,7 @@ void cmd_clear(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[M
void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
char *errmsg; const char *errmsg;
/* check arguments */ /* check arguments */
if (argc != 3) { if (argc != 3) {
@ -230,7 +231,7 @@ void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)
void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
char *errmsg; const char *errmsg;
if (get_num_active_windows() >= MAX_WINDOWS_NUM) { if (get_num_active_windows() >= MAX_WINDOWS_NUM) {
errmsg = " * Warning: Too many windows are open."; errmsg = " * Warning: Too many windows are open.";
@ -259,7 +260,7 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg
void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
char *msg; const char *msg;
struct chatlog *log = self->chatwin->log; struct chatlog *log = self->chatwin->log;
if (argc == 0) { if (argc == 0) {
@ -272,7 +273,7 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
return; return;
} }
char *swch = argv[1]; const char *swch = argv[1];
if (!strcmp(swch, "1") || !strcmp(swch, "on")) { if (!strcmp(swch, "1") || !strcmp(swch, "on")) {
@ -324,7 +325,7 @@ void cmd_myid(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
void cmd_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) void cmd_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
char *errmsg; const char *errmsg;
/* check arguments */ /* check arguments */
if (argc < 1) { if (argc < 1) {
@ -333,13 +334,16 @@ void cmd_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
return; return;
} }
char *nick = argv[1]; char nick[MAX_STR_SIZE];
int len = strlen(nick); int len = 0;
if (nick[0] == '\"') { if (argv[1][0] == '\"') { /* remove opening and closing quotes */
++nick; snprintf(nick, sizeof(nick), "%s", &argv[1][1]);
len -= 2; len = strlen(nick) - 1;
nick[len] = '\0'; nick[len] = '\0';
} else {
snprintf(nick, sizeof(nick), "%s", argv[1]);
len = strlen(nick);
} }
if (!valid_nick(nick)) { if (!valid_nick(nick)) {
@ -359,7 +363,7 @@ void cmd_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
void cmd_note(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) void cmd_note(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
char *errmsg; const char *errmsg;
if (argc < 1) { if (argc < 1) {
errmsg = "Wrong number of arguments."; errmsg = "Wrong number of arguments.";
@ -367,17 +371,18 @@ void cmd_note(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
return; return;
} }
char *msg = argv[1]; if (argv[1][0] != '\"') {
if (msg[0] != '\"') {
errmsg = "Note must be enclosed in quotes."; errmsg = "Note must be enclosed in quotes.";
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg);
return; return;
} }
++msg; /* remove opening and closing quotes */
char msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "%s", &argv[1][1]);
int len = strlen(msg) - 1; int len = strlen(msg) - 1;
msg[len] = '\0'; msg[len] = '\0';
tox_set_status_message(m, (uint8_t *) msg, (uint16_t) len); tox_set_status_message(m, (uint8_t *) msg, (uint16_t) len);
prompt_update_statusmessage(prompt, msg); prompt_update_statusmessage(prompt, msg);
} }
@ -393,25 +398,20 @@ void cmd_quit(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
} }
void cmd_status(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) void cmd_status(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
char *msg = NULL; bool have_note = false;
char *errmsg; const char *errmsg;
if (argc >= 2) { if (argc >= 2) {
msg = argv[2]; have_note = true;
if (msg[0] != '\"') {
errmsg = "Note must be enclosed in quotes.";
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg);
return;
}
} else if (argc != 1) { } else if (argc != 1) {
errmsg = "Wrong number of arguments."; errmsg = "Wrong number of arguments.";
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg);
return; return;
} }
char *status = argv[1]; char status[MAX_STR_SIZE];
snprintf(status, sizeof(status), "%s", argv[1]);
str_to_lower(status); str_to_lower(status);
TOX_USERSTATUS status_kind; TOX_USERSTATUS status_kind;
@ -431,10 +431,19 @@ void cmd_status(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[
tox_set_user_status(m, status_kind); tox_set_user_status(m, status_kind);
prompt_update_status(prompt, status_kind); prompt_update_status(prompt, status_kind);
if (msg != NULL) { if (have_note) {
++msg; if (argv[2][0] != '\"') {
errmsg = "Note must be enclosed in quotes.";
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg);
return;
}
/* remove opening and closing quotes */
char msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "%s", &argv[2][1]);
int len = strlen(msg) - 1; int len = strlen(msg) - 1;
msg[len] = '\0'; /* remove opening and closing quotes */ msg[len] = '\0';
tox_set_status_message(m, (uint8_t *) msg, (uint16_t) len); tox_set_status_message(m, (uint8_t *) msg, (uint16_t) len);
prompt_update_statusmessage(prompt, msg); prompt_update_statusmessage(prompt, msg);
} }

View File

@ -273,7 +273,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
char *event; const char *event;
char timefrmt[TIME_STR_SIZE]; char timefrmt[TIME_STR_SIZE];
get_time_str(timefrmt, sizeof(timefrmt)); get_time_str(timefrmt, sizeof(timefrmt));
@ -315,7 +315,7 @@ static void send_group_action(ToxWindow *self, ChatContext *ctx, Tox *m, char *a
} }
if (tox_group_action_send(m, self->num, (uint8_t *) action, strlen(action)) == -1) { if (tox_group_action_send(m, self->num, (uint8_t *) action, strlen(action)) == -1) {
char *errmsg = " * Failed to send action."; const char *errmsg = " * Failed to send action.";
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, errmsg); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, errmsg);
} }
} }
@ -398,7 +398,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
} }
} else if (!string_is_empty(line)) { } else if (!string_is_empty(line)) {
if (tox_group_message_send(m, self->num, (uint8_t *) line, strlen(line)) == -1) { if (tox_group_message_send(m, self->num, (uint8_t *) line, strlen(line)) == -1) {
char *errmsg = " * Failed to send message."; const char *errmsg = " * Failed to send message.";
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, errmsg); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, errmsg);
} }
} }

View File

@ -34,7 +34,7 @@
extern struct user_settings *user_settings_; extern struct user_settings *user_settings_;
/* Creates/fetches log file by appending to the config dir the name and a pseudo-unique identity */ /* Creates/fetches log file by appending to the config dir the name and a pseudo-unique identity */
void init_logging_session(char *name, char *key, struct chatlog *log) void init_logging_session(char *name, const char *key, struct chatlog *log)
{ {
if (!log->log_on) if (!log->log_on)
return; return;
@ -80,7 +80,7 @@ void init_logging_session(char *name, char *key, struct chatlog *log)
fprintf(log->file, "\n*** NEW SESSION ***\n\n"); fprintf(log->file, "\n*** NEW SESSION ***\n\n");
} }
void write_to_log(const char *msg, char *name, struct chatlog *log, bool event) void write_to_log(const char *msg, const char *name, struct chatlog *log, bool event)
{ {
if (!log->log_on) if (!log->log_on)
return; return;
@ -110,7 +110,7 @@ void write_to_log(const char *msg, char *name, struct chatlog *log, bool event)
} }
} }
void log_enable(char *name, char *key, struct chatlog *log) void log_enable(char *name, const char *key, struct chatlog *log)
{ {
log->log_on = true; log->log_on = true;

View File

@ -33,13 +33,13 @@ struct chatlog {
}; };
/* Creates/fetches log file by appending to the config dir the name and a pseudo-unique identity */ /* Creates/fetches log file by appending to the config dir the name and a pseudo-unique identity */
void init_logging_session(char *name, char *key, struct chatlog *log); void init_logging_session(char *name, const char *key, struct chatlog *log);
/* formats/writes line to log file */ /* formats/writes line to log file */
void write_to_log(const char *msg, char *name, struct chatlog *log, bool event); void write_to_log(const char *msg, const char *name, struct chatlog *log, bool event);
/* enables logging for specified log and creates/fetches file if necessary */ /* enables logging for specified log and creates/fetches file if necessary */
void log_enable(char *name, char *key, struct chatlog *log); void log_enable(char *name, const char *key, struct chatlog *log);
/* disables logging for specified log and closes file */ /* disables logging for specified log and closes file */
void log_disable(struct chatlog *log); void log_disable(struct chatlog *log);

View File

@ -96,7 +96,7 @@ void kill_prompt_window(ToxWindow *self)
} }
/* Updates own nick in prompt statusbar */ /* Updates own nick in prompt statusbar */
void prompt_update_nick(ToxWindow *prompt, char *nick) void prompt_update_nick(ToxWindow *prompt, const char *nick)
{ {
StatusBar *statusbar = prompt->stb; StatusBar *statusbar = prompt->stb;
snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick); snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick);
@ -104,7 +104,7 @@ void prompt_update_nick(ToxWindow *prompt, char *nick)
} }
/* Updates own statusmessage in prompt statusbar */ /* Updates own statusmessage in prompt statusbar */
void prompt_update_statusmessage(ToxWindow *prompt, char *statusmsg) void prompt_update_statusmessage(ToxWindow *prompt, const char *statusmsg)
{ {
StatusBar *statusbar = prompt->stb; StatusBar *statusbar = prompt->stb;
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
@ -334,7 +334,7 @@ static void prompt_onFriendRequest(ToxWindow *self, Tox *m, const char *key, con
int n = add_friend_request(key); int n = add_friend_request(key);
if (n == -1) { if (n == -1) {
char *errmsg = "Friend request queue is full. Discarding request."; const char *errmsg = "Friend request queue is full. Discarding request.";
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg);
write_to_log(errmsg, "", ctx->log, true); write_to_log(errmsg, "", ctx->log, true);
return; return;
@ -394,7 +394,7 @@ static void print_welcome_msg(ToxWindow *self)
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 1, BLUE, " |_| \\___/_/\\_\\___\\____|"); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 1, BLUE, " |_| \\___/_/\\_\\___\\____|");
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, ""); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "");
char *msg = "Welcome to Toxic, a free, open source Tox-based instant messenging client."; const char *msg = "Welcome to Toxic, a free, open source Tox-based instant messenging client.";
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 1, CYAN, msg); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 1, CYAN, msg);
msg = "Type \"/help\" for assistance. Further help may be found via the man page."; msg = "Type \"/help\" for assistance. Further help may be found via the man page.";
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 1, CYAN, msg); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 1, CYAN, msg);

View File

@ -35,8 +35,8 @@
ToxWindow new_prompt(void); ToxWindow new_prompt(void);
void prep_prompt_win(void); void prep_prompt_win(void);
void prompt_init_statusbar(ToxWindow *self, Tox *m); void prompt_init_statusbar(ToxWindow *self, Tox *m);
void prompt_update_nick(ToxWindow *prompt, char *nick); void prompt_update_nick(ToxWindow *prompt, const char *nick);
void prompt_update_statusmessage(ToxWindow *prompt, char *statusmsg); void prompt_update_statusmessage(ToxWindow *prompt, const char *statusmsg);
void prompt_update_status(ToxWindow *prompt, uint8_t status); void prompt_update_status(ToxWindow *prompt, uint8_t status);
void prompt_update_connectionstatus(ToxWindow *prompt, bool is_connected); void prompt_update_connectionstatus(ToxWindow *prompt, bool is_connected);
void kill_prompt_window(ToxWindow *self); void kill_prompt_window(ToxWindow *self);