Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Give enum-from-int functions the ability to report errors. #2475

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions auto_tests/tox_dispatch_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ static void test_tox_events(void)
static void fake_test_unpack(void)
{
// TODO(Green-Sky): add proper unpack tests and/or implement ngc events
(void)tox_unpack_group_privacy_state;
(void)tox_unpack_group_privacy_state;
(void)tox_unpack_group_voice_state;
(void)tox_unpack_group_topic_lock;
(void)tox_unpack_group_join_fail;
(void)tox_unpack_group_mod_event;
(void)tox_unpack_group_exit_type;
(void)tox_group_privacy_state_unpack;
(void)tox_group_privacy_state_unpack;
(void)tox_group_voice_state_unpack;
(void)tox_group_topic_lock_unpack;
(void)tox_group_join_fail_unpack;
(void)tox_group_mod_event_unpack;
(void)tox_group_exit_type_unpack;
}

int main(void)
Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21cf23b1a2e46712663dc4f8daa322991af51b9e82626a127cf1bc8dc583b598 /usr/local/bin/tox-bootstrapd
f117aa0e2cf3f1b42f7e38beec55cb970025692c5753b1159aa22c6e52281393 /usr/local/bin/tox-bootstrapd
8 changes: 6 additions & 2 deletions testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ sh_test(
"-Wno-callback-names",
"-Wno-enum-names",
"+RTS",
"-N3",
"-N4",
"-RTS",
],
data = CIMPLE_FILES,
Expand All @@ -32,7 +32,11 @@ sh_test(
"-Iexternal/libvpx",
"-Iexternal/opus/include",
"-Ihs-tokstyle/include",
] + ["$(locations %s)" % f for f in CIMPLE_FILES],
] + ["$(locations %s)" % f for f in CIMPLE_FILES] + [
"+RTS",
"-N4",
"-RTS",
],
data = CIMPLE_FILES + [
"//c-toxcore/third_party:headers",
"//hs-tokstyle:headers",
Expand Down
44 changes: 30 additions & 14 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -757,17 +757,34 @@
return 0;
}

static Userstatus userstatus_from_int(uint8_t status)
non_null()
static bool userstatus_from_int(uint8_t status, Userstatus *out)
{
switch (status) {
case 0:
return USERSTATUS_NONE;
case 1:
return USERSTATUS_AWAY;
case 2:
return USERSTATUS_BUSY;
default:
return USERSTATUS_INVALID;
case USERSTATUS_NONE: {
*out = USERSTATUS_NONE;
return true;
}

case USERSTATUS_AWAY: {
*out = USERSTATUS_AWAY;
return true;

Check warning on line 771 in toxcore/Messenger.c

View check run for this annotation

Codecov / codecov/patch

toxcore/Messenger.c#L770-L771

Added lines #L770 - L771 were not covered by tests
}

case USERSTATUS_BUSY: {
*out = USERSTATUS_BUSY;
return true;

Check warning on line 776 in toxcore/Messenger.c

View check run for this annotation

Codecov / codecov/patch

toxcore/Messenger.c#L775-L776

Added lines #L775 - L776 were not covered by tests
}

case USERSTATUS_INVALID: {
*out = USERSTATUS_INVALID;
return true;

Check warning on line 781 in toxcore/Messenger.c

View check run for this annotation

Codecov / codecov/patch

toxcore/Messenger.c#L780-L781

Added lines #L780 - L781 were not covered by tests
}

default: {
*out = USERSTATUS_INVALID;
return false;

Check warning on line 786 in toxcore/Messenger.c

View check run for this annotation

Codecov / codecov/patch

toxcore/Messenger.c#L785-L786

Added lines #L785 - L786 were not covered by tests
}
}
}

Expand All @@ -781,7 +798,7 @@
return 0;
}

m->userstatus = userstatus_from_int(status);
userstatus_from_int(status, &m->userstatus);

Check warning on line 801 in toxcore/Messenger.c

View check run for this annotation

Codecov / codecov/patch

toxcore/Messenger.c#L801

Added line #L801 was not covered by tests

for (uint32_t i = 0; i < m->numfriends; ++i) {
m->friendlist[i].userstatus_sent = false;
Expand Down Expand Up @@ -937,7 +954,7 @@
non_null()
static void set_friend_userstatus(const Messenger *m, int32_t friendnumber, uint8_t status)
{
m->friendlist[friendnumber].userstatus = userstatus_from_int(status);
userstatus_from_int(status, &m->friendlist[friendnumber].userstatus);
}

non_null()
Expand Down Expand Up @@ -2095,9 +2112,8 @@
return 0;
}

const Userstatus status = userstatus_from_int(data[0]);

if (status == USERSTATUS_INVALID) {
Userstatus status;
if (!userstatus_from_int(data[0], &status)) {
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion toxcore/events/conference_invite.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
}

return bin_unpack_u32(bu, &event->friend_number)
&& tox_unpack_conference_type(bu, &event->type)
&& tox_conference_type_unpack(bu, &event->type)

Check warning on line 128 in toxcore/events/conference_invite.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_invite.c#L128

Added line #L128 was not covered by tests
&& bin_unpack_bin(bu, &event->cookie, &event->cookie_length);
}

Expand Down
2 changes: 1 addition & 1 deletion toxcore/events/conference_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@

return bin_unpack_u32(bu, &event->conference_number)
&& bin_unpack_u32(bu, &event->peer_number)
&& tox_unpack_message_type(bu, &event->type)
&& tox_message_type_unpack(bu, &event->type)

Check warning on line 144 in toxcore/events/conference_message.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_message.c#L144

Added line #L144 was not covered by tests
&& bin_unpack_bin(bu, &event->message, &event->message_length);
}

Expand Down
2 changes: 1 addition & 1 deletion toxcore/events/file_recv_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@

return bin_unpack_u32(bu, &event->friend_number)
&& bin_unpack_u32(bu, &event->file_number)
&& tox_unpack_file_control(bu, &event->control);
&& tox_file_control_unpack(bu, &event->control);

Check warning on line 108 in toxcore/events/file_recv_control.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/file_recv_control.c#L108

Added line #L108 was not covered by tests
}


Expand Down
2 changes: 1 addition & 1 deletion toxcore/events/friend_connection_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static bool tox_event_friend_connection_status_unpack(
}

return bin_unpack_u32(bu, &event->friend_number)
&& tox_unpack_connection(bu, &event->connection_status);
&& tox_connection_unpack(bu, &event->connection_status);
}


Expand Down
2 changes: 1 addition & 1 deletion toxcore/events/friend_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static bool tox_event_friend_message_unpack(
}

return bin_unpack_u32(bu, &event->friend_number)
&& tox_unpack_message_type(bu, &event->type)
&& tox_message_type_unpack(bu, &event->type)
&& bin_unpack_bin(bu, &event->message, &event->message_length);
}

Expand Down
2 changes: 1 addition & 1 deletion toxcore/events/friend_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static bool tox_event_friend_status_unpack(
}

return bin_unpack_u32(bu, &event->friend_number)
&& tox_unpack_user_status(bu, &event->status);
&& tox_user_status_unpack(bu, &event->status);
}


Expand Down
2 changes: 1 addition & 1 deletion toxcore/events/self_connection_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static bool tox_event_self_connection_status_unpack(
Tox_Event_Self_Connection_Status *event, Bin_Unpack *bu)
{
assert(event != nullptr);
return tox_unpack_connection(bu, &event->connection_status);
return tox_connection_unpack(bu, &event->connection_status);
}


Expand Down
4 changes: 2 additions & 2 deletions toxcore/group_chats.c
Original file line number Diff line number Diff line change
Expand Up @@ -1280,8 +1280,8 @@ static uint16_t unpack_gc_shared_state(GC_SharedState *shared_state, const uint8
memcpy(&voice_state, data + len_processed, sizeof(uint8_t));
len_processed += sizeof(uint8_t);

shared_state->voice_state = group_voice_state_from_int(voice_state);
shared_state->privacy_state = group_privacy_state_from_int(privacy_state);
group_voice_state_from_int(voice_state, &shared_state->voice_state);
group_privacy_state_from_int(privacy_state, &shared_state->privacy_state);

return len_processed;
}
Expand Down
55 changes: 37 additions & 18 deletions toxcore/group_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,48 @@
#include "ccompat.h"
#include "util.h"

Group_Privacy_State group_privacy_state_from_int(uint8_t value)
bool group_privacy_state_from_int(uint8_t value, Group_Privacy_State *out)
{
switch (value) {
case 0:
return GI_PUBLIC;
case 1:
return GI_PRIVATE;
default:
return GI_PUBLIC;
case GI_PUBLIC: {
*out = GI_PUBLIC;
return true;
}

case GI_PRIVATE: {
*out = GI_PRIVATE;
return true;
}

default: {
*out = GI_PUBLIC;
return false;

Check warning on line 36 in toxcore/group_pack.c

View check run for this annotation

Codecov / codecov/patch

toxcore/group_pack.c#L35-L36

Added lines #L35 - L36 were not covered by tests
}
}
}

Group_Voice_State group_voice_state_from_int(uint8_t value)
bool group_voice_state_from_int(uint8_t value, Group_Voice_State *out)
{
switch (value) {
case 0:
return GV_ALL;
case 1:
return GV_MODS;
case 2:
return GV_FOUNDER;
default:
return GV_ALL;
case GV_ALL: {
*out = GV_ALL;
return true;
}

case GV_MODS: {
*out = GV_MODS;
return true;
}

case GV_FOUNDER: {
*out = GV_FOUNDER;
return true;
}

default: {
*out = GV_ALL;
return false;

Check warning on line 61 in toxcore/group_pack.c

View check run for this annotation

Codecov / codecov/patch

toxcore/group_pack.c#L60-L61

Added lines #L60 - L61 were not covered by tests
}
}
}

Expand Down Expand Up @@ -69,8 +88,8 @@
}

chat->connection_state = manually_disconnected ? CS_DISCONNECTED : CS_CONNECTING;
chat->shared_state.privacy_state = group_privacy_state_from_int(privacy_state);
chat->shared_state.voice_state = group_voice_state_from_int(voice_state);
group_privacy_state_from_int(privacy_state, &chat->shared_state.privacy_state);
group_voice_state_from_int(voice_state, &chat->shared_state.voice_state);

// we always load saved groups as private in case the group became private while we were offline.
// this will have no detrimental effect if the group is public, as the correct privacy
Expand Down
6 changes: 4 additions & 2 deletions toxcore/group_pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ void gc_save_pack_group(const GC_Chat *chat, Bin_Pack *bp);
non_null()
bool gc_load_unpack_group(GC_Chat *chat, Bin_Unpack *bu);

Group_Privacy_State group_privacy_state_from_int(uint8_t value);
Group_Voice_State group_voice_state_from_int(uint8_t value);
non_null()
bool group_privacy_state_from_int(uint8_t value, Group_Privacy_State *out);
non_null()
bool group_voice_state_from_int(uint8_t value, Group_Voice_State *out);

#endif // GROUP_PACK_H
4 changes: 2 additions & 2 deletions toxcore/tox_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ bool tox_events_pack(const Tox_Events *events, Bin_Pack *bp)
}

non_null()
static bool tox_event_unpack(Tox_Events *events, Bin_Unpack *bu)
static bool tox_events_unpack_event(Tox_Events *events, Bin_Unpack *bu)
{
uint32_t size;
if (!bin_unpack_array(bu, &size)) {
Expand Down Expand Up @@ -210,7 +210,7 @@ bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu)
}

for (uint32_t i = 0; i < size; ++i) {
if (!tox_event_unpack(events, bu)) {
if (!tox_events_unpack_event(events, bu)) {
return false;
}
}
Expand Down
Loading
Loading