Skip to content

Commit fc623a5

Browse files
committed
tox_new() should return null when savedata loading fails
Returning a valid tox instance when loading a corrupt savefile is probably not desired behaviour
1 parent 06d949a commit fc623a5

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9bec65f2a3093ebb49c3751dfad267482bc80d4b29ef9171f11d5ba53058d713 /usr/local/bin/tox-bootstrapd
1+
8942735f04e41962bbcfaeccbfa6487424ad78c910e932d93b5b6f56a6e852b7 /usr/local/bin/tox-bootstrapd

toxcore/tox.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -858,12 +858,23 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
858858

859859
if (load_savedata_tox
860860
&& tox_load(tox, tox_options_get_savedata_data(opts), tox_options_get_savedata_length(opts)) == -1) {
861+
mono_time_free(tox->sys.mem, tox->mono_time);
862+
tox_options_free(default_options);
863+
tox_unlock(tox);
864+
865+
if (tox->mutex != nullptr) {
866+
pthread_mutex_destroy(tox->mutex);
867+
}
868+
869+
free(tox->mutex);
870+
free(tox);
871+
861872
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT);
862-
} else if (load_savedata_sk) {
873+
return nullptr;
874+
}
875+
876+
if (load_savedata_sk) {
863877
load_secret_key(tox->m->net_crypto, tox_options_get_savedata_data(opts));
864-
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK);
865-
} else {
866-
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK);
867878
}
868879

869880
m_callback_namechange(tox->m, tox_friend_name_handler);
@@ -913,6 +924,9 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
913924
tox_options_free(default_options);
914925

915926
tox_unlock(tox);
927+
928+
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK);
929+
916930
return tox;
917931
}
918932

toxcore/tox.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -890,9 +890,6 @@ typedef enum Tox_Err_New {
890890
* This function will bring the instance into a valid state. Running the event
891891
* loop with a new instance will operate correctly.
892892
*
893-
* If loading failed or succeeded only partially, the new or partially loaded
894-
* instance is returned and an error code is set.
895-
*
896893
* @param options An options object as described above. If this parameter is
897894
* NULL, the default options are used.
898895
*

0 commit comments

Comments
 (0)