cleanup: Add more null checks in tox_new.

So the rest of the code can assume non-null more.
This commit is contained in:
iphydf 2022-03-28 14:52:38 +00:00
parent f13029417e
commit 6cc8ef3771
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9

View File

@ -550,7 +550,9 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
m_options.proxy_info.ip_port.ip.family = net_family_unspec;
}
if (!addr_resolve_or_parse_ip(tox_options_get_proxy_host(opts), &m_options.proxy_info.ip_port.ip, nullptr)) {
const char *const proxy_host = tox_options_get_proxy_host(opts);
if (proxy_host == nullptr || !addr_resolve_or_parse_ip(proxy_host, &m_options.proxy_info.ip_port.ip, nullptr)) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_HOST);
// TODO(irungentoo): TOX_ERR_NEW_PROXY_NOT_FOUND if domain.
tox_options_free(default_options);
@ -595,11 +597,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
Messenger_Error m_error;
tox->m = new_messenger(tox->mono_time, &m_options, &m_error);
// TODO(iphydf): Clarify this code, check for NULL before new_groupchats, so
// new_groupchats can assume m is non-NULL.
if (new_groupchats(tox->mono_time, tox->m) == nullptr) {
kill_messenger(tox->m);
if (tox->m == nullptr) {
if (m_error == MESSENGER_ERROR_PORT) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PORT_ALLOC);
} else if (m_error == MESSENGER_ERROR_TCP_SERVER) {
@ -621,6 +619,24 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
return nullptr;
}
if (new_groupchats(tox->mono_time, tox->m) == nullptr) {
kill_messenger(tox->m);
mono_time_free(tox->mono_time);
tox_options_free(default_options);
unlock(tox);
if (tox->mutex != nullptr) {
pthread_mutex_destroy(tox->mutex);
}
free(tox->mutex);
free(tox);
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC);
return nullptr;
}
if (load_savedata_tox
&& tox_load(tox, tox_options_get_savedata_data(opts), tox_options_get_savedata_length(opts)) == -1) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT);