mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-16 02:53:03 +01:00
enforce const correctess, fix undefined behaviour with string literals
This commit is contained in:
parent
cbe47b3660
commit
973f6206ee
@ -166,15 +166,15 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
|
||||
return;
|
||||
}
|
||||
|
||||
char *path = argv[1];
|
||||
|
||||
if (path[0] != '\"') {
|
||||
if (argv[1][0] != '\"') {
|
||||
errmsg = "File path must be enclosed in quotes.";
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg);
|
||||
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;
|
||||
path[path_len] = '\0';
|
||||
|
||||
|
@ -289,7 +289,7 @@ void *dns3_lookup_thread(void *data)
|
||||
}
|
||||
|
||||
/* 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) {
|
||||
const char *err = "Please wait for previous user lookup to finish.";
|
||||
|
@ -27,6 +27,6 @@
|
||||
#define _dns_h
|
||||
|
||||
/* 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 */
|
||||
|
@ -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)
|
||||
{
|
||||
char *errmsg;
|
||||
const char *errmsg;
|
||||
int32_t f_num = tox_add_friend(m, (uint8_t *) id_bin, (uint8_t *) msg, (uint16_t) strlen(msg));
|
||||
|
||||
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])
|
||||
{
|
||||
char *errmsg;
|
||||
const char *errmsg;
|
||||
|
||||
if (argc < 1) {
|
||||
errmsg = "Invalid syntax.";
|
||||
@ -143,21 +143,22 @@ void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
|
||||
return;
|
||||
}
|
||||
|
||||
char *id = argv[1];
|
||||
const char *id = argv[1];
|
||||
char msg[MAX_STR_SIZE];
|
||||
|
||||
if (argc > 1) {
|
||||
char *temp = argv[2];
|
||||
|
||||
if (temp[0] != '\"') {
|
||||
if (argv[2][0] != '\"') {
|
||||
errmsg = "Message must be enclosed in quotes.";
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg);
|
||||
return;
|
||||
}
|
||||
|
||||
++temp;
|
||||
temp[strlen(temp) - 1] = '\0';
|
||||
snprintf(msg, sizeof(msg), "%s", temp);
|
||||
/* remove opening and closing quotes */
|
||||
char tmp[MAX_STR_SIZE];
|
||||
snprintf(tmp, sizeof(tmp), "%s", &argv[2][1]);
|
||||
int len = strlen(tmp) - 1;
|
||||
tmp[len] = '\0';
|
||||
snprintf(msg, sizeof(msg), "%s", tmp);
|
||||
} else {
|
||||
char selfname[TOX_MAX_NAME_LENGTH];
|
||||
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])
|
||||
{
|
||||
char *errmsg;
|
||||
const char *errmsg;
|
||||
|
||||
/* check arguments */
|
||||
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])
|
||||
{
|
||||
char *errmsg;
|
||||
const char *errmsg;
|
||||
|
||||
if (get_num_active_windows() >= MAX_WINDOWS_NUM) {
|
||||
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])
|
||||
{
|
||||
char *msg;
|
||||
const char *msg;
|
||||
struct chatlog *log = self->chatwin->log;
|
||||
|
||||
if (argc == 0) {
|
||||
@ -272,7 +273,7 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
|
||||
return;
|
||||
}
|
||||
|
||||
char *swch = argv[1];
|
||||
const char *swch = argv[1];
|
||||
|
||||
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])
|
||||
{
|
||||
char *errmsg;
|
||||
const char *errmsg;
|
||||
|
||||
/* check arguments */
|
||||
if (argc < 1) {
|
||||
@ -333,13 +334,16 @@ void cmd_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
|
||||
return;
|
||||
}
|
||||
|
||||
char *nick = argv[1];
|
||||
int len = strlen(nick);
|
||||
char nick[MAX_STR_SIZE];
|
||||
int len = 0;
|
||||
|
||||
if (nick[0] == '\"') {
|
||||
++nick;
|
||||
len -= 2;
|
||||
if (argv[1][0] == '\"') { /* remove opening and closing quotes */
|
||||
snprintf(nick, sizeof(nick), "%s", &argv[1][1]);
|
||||
len = strlen(nick) - 1;
|
||||
nick[len] = '\0';
|
||||
} else {
|
||||
snprintf(nick, sizeof(nick), "%s", argv[1]);
|
||||
len = strlen(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])
|
||||
{
|
||||
char *errmsg;
|
||||
const char *errmsg;
|
||||
|
||||
if (argc < 1) {
|
||||
errmsg = "Wrong number of arguments.";
|
||||
@ -367,17 +371,18 @@ void cmd_note(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
|
||||
return;
|
||||
}
|
||||
|
||||
char *msg = argv[1];
|
||||
|
||||
if (msg[0] != '\"') {
|
||||
if (argv[1][0] != '\"') {
|
||||
errmsg = "Note must be enclosed in quotes.";
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg);
|
||||
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;
|
||||
msg[len] = '\0';
|
||||
|
||||
tox_set_status_message(m, (uint8_t *) msg, (uint16_t) len);
|
||||
prompt_update_statusmessage(prompt, msg);
|
||||
}
|
||||
@ -394,24 +399,19 @@ 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])
|
||||
{
|
||||
char *msg = NULL;
|
||||
char *errmsg;
|
||||
bool have_note = false;
|
||||
const char *errmsg;
|
||||
|
||||
if (argc >= 2) {
|
||||
msg = argv[2];
|
||||
|
||||
if (msg[0] != '\"') {
|
||||
errmsg = "Note must be enclosed in quotes.";
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg);
|
||||
return;
|
||||
}
|
||||
have_note = true;
|
||||
} else if (argc != 1) {
|
||||
errmsg = "Wrong number of arguments.";
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg);
|
||||
return;
|
||||
}
|
||||
|
||||
char *status = argv[1];
|
||||
char status[MAX_STR_SIZE];
|
||||
snprintf(status, sizeof(status), "%s", argv[1]);
|
||||
str_to_lower(status);
|
||||
|
||||
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);
|
||||
prompt_update_status(prompt, status_kind);
|
||||
|
||||
if (msg != NULL) {
|
||||
++msg;
|
||||
if (have_note) {
|
||||
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;
|
||||
msg[len] = '\0'; /* remove opening and closing quotes */
|
||||
msg[len] = '\0';
|
||||
|
||||
tox_set_status_message(m, (uint8_t *) msg, (uint16_t) len);
|
||||
prompt_update_statusmessage(prompt, msg);
|
||||
}
|
||||
|
@ -273,7 +273,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu
|
||||
|
||||
ChatContext *ctx = self->chatwin;
|
||||
|
||||
char *event;
|
||||
const char *event;
|
||||
char timefrmt[TIME_STR_SIZE];
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -398,7 +398,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
|
||||
}
|
||||
} else if (!string_is_empty(line)) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@
|
||||
extern struct user_settings *user_settings_;
|
||||
|
||||
/* 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)
|
||||
return;
|
||||
@ -80,7 +80,7 @@ void init_logging_session(char *name, char *key, struct chatlog *log)
|
||||
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)
|
||||
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;
|
||||
|
||||
|
@ -33,13 +33,13 @@ struct chatlog {
|
||||
};
|
||||
|
||||
/* 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 */
|
||||
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 */
|
||||
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 */
|
||||
void log_disable(struct chatlog *log);
|
||||
|
@ -96,7 +96,7 @@ void kill_prompt_window(ToxWindow *self)
|
||||
}
|
||||
|
||||
/* 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;
|
||||
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 */
|
||||
void prompt_update_statusmessage(ToxWindow *prompt, char *statusmsg)
|
||||
void prompt_update_statusmessage(ToxWindow *prompt, const char *statusmsg)
|
||||
{
|
||||
StatusBar *statusbar = prompt->stb;
|
||||
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);
|
||||
|
||||
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);
|
||||
write_to_log(errmsg, "", ctx->log, true);
|
||||
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, 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);
|
||||
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);
|
||||
|
@ -35,8 +35,8 @@
|
||||
ToxWindow new_prompt(void);
|
||||
void prep_prompt_win(void);
|
||||
void prompt_init_statusbar(ToxWindow *self, Tox *m);
|
||||
void prompt_update_nick(ToxWindow *prompt, char *nick);
|
||||
void prompt_update_statusmessage(ToxWindow *prompt, char *statusmsg);
|
||||
void prompt_update_nick(ToxWindow *prompt, const char *nick);
|
||||
void prompt_update_statusmessage(ToxWindow *prompt, const char *statusmsg);
|
||||
void prompt_update_status(ToxWindow *prompt, uint8_t status);
|
||||
void prompt_update_connectionstatus(ToxWindow *prompt, bool is_connected);
|
||||
void kill_prompt_window(ToxWindow *self);
|
||||
|
Loading…
Reference in New Issue
Block a user