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

cleanup: Make group packet entry creation less error-prone #2540

Merged
merged 1 commit into from
Jan 11, 2024
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
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 @@
423687bc6adc323174a2ab4e2ee8443e6c21c4d6509942af469b4bc16db6bfa4 /usr/local/bin/tox-bootstrapd
c98b35f18f351c9a3a05137e69a43e634ab5963d364b9337e89ad89469ebd8c2 /usr/local/bin/tox-bootstrapd
40 changes: 19 additions & 21 deletions toxcore/group_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,32 @@
}

/** @brief Puts packet data in array_entry.
*
* Requires an empty array entry to be passed, and must not modify the passed
* array entry on error.
*
* Return true on success.
*/
non_null(1, 2) nullable(3)
static bool create_array_entry(const Mono_Time *mono_time, GC_Message_Array_Entry *array_entry, const uint8_t *data,
uint16_t length, uint8_t packet_type, uint64_t message_id)
non_null(1, 2, 3) nullable(4)
static bool create_array_entry(const Logger *log, const Mono_Time *mono_time, GC_Message_Array_Entry *array_entry,
const uint8_t *data, uint16_t length, uint8_t packet_type, uint64_t message_id)
{
if (!array_entry_is_empty(array_entry)) {
LOGGER_WARNING(log, "Failed to create array entry; entry is not empty.");
return false;

Check warning on line 100 in toxcore/group_connection.c

View check run for this annotation

Codecov / codecov/patch

toxcore/group_connection.c#L99-L100

Added lines #L99 - L100 were not covered by tests
}

if (length == 0) {
if (data != nullptr) {
LOGGER_FATAL(log, "Got non-null data with zero length (type %d)", packet_type); // should never happen
return false;

Check warning on line 106 in toxcore/group_connection.c

View check run for this annotation

Codecov / codecov/patch

toxcore/group_connection.c#L105-L106

Added lines #L105 - L106 were not covered by tests
}

array_entry->data = nullptr;
array_entry->data_length = 0;
} else {
if (data == nullptr) {
LOGGER_FATAL(log, "Got null data with non-zero length (type %u)", packet_type); // should never happen

Check warning on line 113 in toxcore/group_connection.c

View check run for this annotation

Codecov / codecov/patch

toxcore/group_connection.c#L113

Added line #L113 was not covered by tests
return false;
}

Expand Down Expand Up @@ -138,13 +152,7 @@
const uint16_t idx = gcc_get_array_index(gconn->send_message_id);
GC_Message_Array_Entry *array_entry = &gconn->send_array[idx];

if (!array_entry_is_empty(array_entry)) {
LOGGER_DEBUG(log, "Send array entry isn't empty");
return false;
}

if (!create_array_entry(mono_time, array_entry, data, length, packet_type, gconn->send_message_id)) {
LOGGER_WARNING(log, "Failed to create array entry");
if (!create_array_entry(log, mono_time, array_entry, data, length, packet_type, gconn->send_message_id)) {
return false;
}

Expand Down Expand Up @@ -335,17 +343,7 @@
const uint16_t idx = gcc_get_array_index(message_id);
GC_Message_Array_Entry *ary_entry = &gconn->recv_array[idx];

if (!array_entry_is_empty(ary_entry)) {
LOGGER_DEBUG(log, "Recv array is not empty");
return false;
}

if (!create_array_entry(mono_time, ary_entry, data, length, packet_type, message_id)) {
LOGGER_WARNING(log, "Failed to create array entry");
return false;
}

return true;
return create_array_entry(log, mono_time, ary_entry, data, length, packet_type, message_id);
}

/**
Expand Down
Loading