Skip to content

Commit

Permalink
refactor: Make some fatal memory errors non-fatal
Browse files Browse the repository at this point in the history
These fatal errors aren't catastrophic and can be recovered from
  • Loading branch information
JFreegman committed Feb 28, 2024
1 parent c588c4b commit 3158ace
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 69 deletions.
132 changes: 114 additions & 18 deletions src/audio_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ void on_call(ToxAV *av, uint32_t friend_number, bool audio_enabled, bool video_e

Toxic *toxic = (Toxic *) user_data;

if (friend_number >= CallControl.max_calls) {
fprintf(stderr, "Failed to receive call: Insufficient memory\n");
return;
}

Call *call = &CallControl.calls[friend_number];
init_call(call);

Expand All @@ -345,6 +350,11 @@ void on_call_state(ToxAV *av, uint32_t friend_number, uint32_t state, void *user

UNUSED_VAR(av);

if (friend_number >= CallControl.max_calls) {
fprintf(stderr, "Failed to handle call state: Insufficient memory\n");
return;
}

Call *call = &CallControl.calls[friend_number];

if (call->status == cs_None) {
Expand Down Expand Up @@ -412,6 +422,10 @@ void audio_bit_rate_callback(ToxAV *av, uint32_t friend_number, uint32_t audio_b
{
UNUSED_VAR(user_data);

if (friend_number >= CallControl.max_calls) {
return;
}

Call *call = &CallControl.calls[friend_number];
call->audio_bit_rate = audio_bit_rate;
toxav_audio_set_bit_rate(av, friend_number, audio_bit_rate, NULL);
Expand All @@ -423,6 +437,10 @@ void callback_recv_invite(Toxic *toxic, uint32_t friend_number)
return;
}

if (friend_number >= CallControl.max_calls) {
return;
}

if (Friends.list[friend_number].window_id == -1) {
const int window_id = add_window(toxic, new_chat(toxic->tox, Friends.list[friend_number].num));

Expand All @@ -447,6 +465,10 @@ void callback_recv_invite(Toxic *toxic, uint32_t friend_number)
}
void callback_recv_ringing(Toxic *toxic, uint32_t friend_number)
{
if (friend_number >= CallControl.max_calls) {
return;
}

const Call *call = &CallControl.calls[friend_number];
Windows *windows = toxic->windows;

Expand All @@ -460,6 +482,10 @@ void callback_recv_ringing(Toxic *toxic, uint32_t friend_number)
}
void callback_recv_starting(Toxic *toxic, uint32_t friend_number)
{
if (friend_number >= CallControl.max_calls) {
return;
}

Call *call = &CallControl.calls[friend_number];
Windows *windows = toxic->windows;

Expand All @@ -474,6 +500,10 @@ void callback_recv_starting(Toxic *toxic, uint32_t friend_number)
}
void callback_call_started(Toxic *toxic, uint32_t friend_number)
{
if (friend_number >= CallControl.max_calls) {
return;
}

Call *call = &CallControl.calls[friend_number];
Windows *windows = toxic->windows;

Expand All @@ -488,6 +518,10 @@ void callback_call_started(Toxic *toxic, uint32_t friend_number)
}
void callback_call_canceled(Toxic *toxic, uint32_t friend_number)
{
if (friend_number >= CallControl.max_calls) {
return;
}

const Call *call = &CallControl.calls[friend_number];
Windows *windows = toxic->windows;

Expand All @@ -501,6 +535,10 @@ void callback_call_canceled(Toxic *toxic, uint32_t friend_number)
}
void callback_call_rejected(Toxic *toxic, uint32_t friend_number)
{
if (friend_number >= CallControl.max_calls) {
return;
}

const Call *call = &CallControl.calls[friend_number];
Windows *windows = toxic->windows;

Expand All @@ -514,6 +552,10 @@ void callback_call_rejected(Toxic *toxic, uint32_t friend_number)
}
void callback_call_ended(Toxic *toxic, uint32_t friend_number)
{
if (friend_number >= CallControl.max_calls) {
return;
}

const Call *call = &CallControl.calls[friend_number];
Windows *windows = toxic->windows;

Expand Down Expand Up @@ -560,6 +602,11 @@ void cmd_call(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar
return;
}

if (self->num >= CallControl.max_calls) {
print_err(self, c_config, "Invalid call index");
return;
}

Call *call = &CallControl.calls[self->num];

if (call->status != cs_None) {
Expand Down Expand Up @@ -595,6 +642,11 @@ void cmd_answer(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*
return;
}

if (self->num >= CallControl.max_calls) {
print_err(self, c_config, "Invalid call index");
return;
}

Call *call = &CallControl.calls[self->num];

if (call->status != cs_Pending) {
Expand Down Expand Up @@ -645,6 +697,11 @@ void cmd_reject(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*
return;
}

if (self->num >= CallControl.max_calls) {
print_err(self, c_config, "Invalid call index.");
return;
}

Call *call = &CallControl.calls[self->num];

if (call->status != cs_Pending) {
Expand Down Expand Up @@ -681,6 +738,11 @@ void cmd_hangup(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*
return;
}

if (self->num >= CallControl.max_calls) {
print_err(self, c_config, "Invalid call index.");
return;
}

Call *call = &CallControl.calls[self->num];

if (call->status == cs_None) {
Expand Down Expand Up @@ -815,6 +877,10 @@ void cmd_mute(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar
return;
}

if (self->num >= CallControl.max_calls) {
print_err(self, c_config, "Invalid call index.");
return;
}

/* If call is active, use this_call values */
Call *this_call = &CallControl.calls[self->num];
Expand Down Expand Up @@ -860,6 +926,11 @@ void cmd_sense(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*a
return;
}

if (self->num >= CallControl.max_calls) {
print_err(self, c_config, "Invalid call index.");
return;
}

const Call *call = &CallControl.calls[self->num];

/* Call must be active */
Expand All @@ -881,6 +952,11 @@ void cmd_bitrate(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (

const Client_Config *c_config = toxic->c_config;

if (self->num >= CallControl.max_calls) {
print_err(self, c_config, "Invalid call index.");
return;
}

Call *call = &CallControl.calls[self->num];

if (call->status != cs_Active) {
Expand Down Expand Up @@ -933,14 +1009,20 @@ void cmd_bitrate(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (

void place_call(ToxWindow *self, Toxic *toxic)
{
const Client_Config *c_config = toxic->c_config;

if (self->num >= CallControl.max_calls) {
print_err(self, c_config, "Invalid call index.");
return;
}

Call *call = &CallControl.calls[self->num];

if (call->status != cs_Pending) {
print_err(self, toxic->c_config, "No pending call.");
return;
}

const Client_Config *c_config = toxic->c_config;

Toxav_Err_Call error;

toxav_call(toxic->av, self->num, call->audio_bit_rate, call->video_bit_rate, &error);
Expand All @@ -967,11 +1049,15 @@ void place_call(ToxWindow *self, Toxic *toxic)

void stop_current_call(ToxWindow *self, Toxic *toxic)
{
if (self->num >= CallControl.max_calls) {
print_err(self, toxic->c_config, "Invalid call index.");
return;
}

Call *call = &CallControl.calls[self->num];

if (call->status == cs_Pending) {
toxav_call_control(toxic->av, self->num, TOXAV_CALL_CONTROL_CANCEL, NULL);

cancel_call(call);
callback_call_canceled(toxic, self->num);
} else {
Expand All @@ -989,36 +1075,42 @@ void stop_current_call(ToxWindow *self, Toxic *toxic)
/**
* Reallocates the Calls list according to n.
*/
static void realloc_calls(uint32_t n)
static bool realloc_calls(uint32_t n)
{
if (n == 0) {
free(CallControl.calls);
CallControl.calls = NULL;
return;
return true;
}

Call *temp = realloc(CallControl.calls, n * sizeof(Call));

if (temp == NULL) {
exit_toxic_err(FATALERR_MEMORY, "failed in realloc_calls");
return false;
}

CallControl.calls = temp;
return true;
}

/**
* Inits the call structure for a given friend. Called when a friend is added to the friends list.
* Index must be equivalent to the friend's friendlist index.
*/
void init_friend_AV(uint32_t index)
bool init_friend_AV(uint32_t index)
{
if (index == CallControl.max_calls) {
realloc_calls(CallControl.max_calls + 1);
CallControl.calls[CallControl.max_calls] = (Call) {
0
};
++CallControl.max_calls;
if (index != CallControl.max_calls) {
return false;
}

if (!realloc_calls(CallControl.max_calls + 1)) {
fprintf(stderr, "Warning: realloc_calls(%u) failed\n", CallControl.max_calls + 1);
return false;
}

CallControl.calls[CallControl.max_calls] = (Call) {
0
};

++CallControl.max_calls;

return true;
}

/**
Expand All @@ -1027,7 +1119,11 @@ void init_friend_AV(uint32_t index)
*/
void del_friend_AV(uint32_t index)
{
realloc_calls(index);
if (!realloc_calls(index)) {
fprintf(stderr, "Warning: realloc_calls(%u) failed\n", index);
return;
}

CallControl.max_calls = index;
}

Expand Down
8 changes: 7 additions & 1 deletion src/audio_call.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ bool init_call(Call *call);
void place_call(ToxWindow *self, Toxic *toxic);
void stop_current_call(ToxWindow *self, Toxic *toxic);

void init_friend_AV(uint32_t index);
/*
* Initializes the call structure for a given friend. Called when a friend is added
* to the friends list. Index must be equivalent to the friend's friendlist index.
*
* Returns true on success.
*/
bool init_friend_AV(uint32_t index);
void del_friend_AV(uint32_t index);

#endif /* AUDIO_CALL_H */
2 changes: 1 addition & 1 deletion src/autocomplete.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ static int complete_line_helper(ToxWindow *self, Toxic *toxic, const char *const
char *sub = calloc(1, strlen(ubuf) + 1);

if (sub == NULL) {
exit_toxic_err(FATALERR_MEMORY, "failed in complete_line_helper");
return -1;
}

if (!s && !dir_search) {
Expand Down
7 changes: 5 additions & 2 deletions src/avatars.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,16 @@ int avatar_set(Tox *tox, const char *path, size_t path_len)

fclose(fp);

off_t size = file_size(path);
const off_t size = file_size(path);

if (size == 0 || size > MAX_AVATAR_FILE_SIZE) {
return -1;
}

get_file_name(Avatar.name, sizeof(Avatar.name), path);
if (get_file_name(Avatar.name, sizeof(Avatar.name), path) < 0) {
return -1;
}

Avatar.name_len = strlen(Avatar.name);
snprintf(Avatar.path, sizeof(Avatar.path), "%s", path);
Avatar.path_len = path_len;
Expand Down
Loading

0 comments on commit 3158ace

Please sign in to comment.