Skip to content

Commit 17045ee

Browse files
committed
refactor: Add tox_options_set_savedata, deprecate old functions.
This one is simpler than the existing tox_options_set_savedata_data and therefore safer for clients to use. It will either fail (no change) or succeed (allocation+copy). Also setting data size without setting data makes no sense, so that function is now deprecated.
1 parent c834472 commit 17045ee

13 files changed

+141
-76
lines changed

auto_tests/auto_test_support.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33

44
#include "check_compat.h"
55
#include "../testing/misc_tools.h"
6-
#include "../toxcore/Messenger.h"
76
#include "../toxcore/mono_time.h"
87
#include "../toxcore/tox_dispatch.h"
98
#include "../toxcore/tox_events.h"
10-
#include "../toxcore/tox_struct.h"
9+
#include "../toxcore/tox_struct.h" // IWYU pragma: keep
1110

1211
#include "auto_test_support.h"
1312

@@ -211,7 +210,7 @@ void reload(AutoTox *autotox)
211210
ck_assert(options != nullptr);
212211
tox_options_set_ipv6_enabled(options, USE_IPV6);
213212
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
214-
tox_options_set_savedata_data(options, autotox->save_state, autotox->save_size);
213+
ck_assert(tox_options_set_savedata(options, autotox->save_state, autotox->save_size));
215214
autotox->tox = tox_new_log(options, nullptr, &autotox->index);
216215
ck_assert(autotox->tox != nullptr);
217216
autotox->dispatch = tox_dispatch_new(nullptr);

auto_tests/conference_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ static void run_conference_tests(AutoTox *autotoxes)
248248
struct Tox_Options *const options = tox_options_new(nullptr);
249249
ck_assert(options != nullptr);
250250
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
251-
tox_options_set_savedata_data(options, save[i], save_size[i]);
251+
ck_assert(tox_options_set_savedata(options, save[i], save_size[i]));
252252
autotoxes[i].tox = tox_new_log(options, nullptr, &autotoxes[i].index);
253253
ck_assert(autotoxes[i].tox != nullptr);
254254
tox_options_free(options);

auto_tests/encryptsave_test.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <string.h>
44
#include <sys/types.h>
55

6-
#include "../testing/misc_tools.h"
76
#include "../toxcore/ccompat.h"
87
#include "../toxcore/crypto_core.h"
98
#include "../toxcore/tox.h"
@@ -54,7 +53,7 @@ static void test_save_friend(void)
5453
struct Tox_Options *options = tox_options_new(nullptr);
5554
ck_assert(options != nullptr);
5655
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
57-
tox_options_set_savedata_data(options, enc_data, size2);
56+
ck_assert(tox_options_set_savedata(options, enc_data, size2));
5857

5958
Tox_Err_New err2;
6059
Tox *tox3 = tox_new_log(options, &err2, nullptr);
@@ -66,7 +65,7 @@ static void test_save_friend(void)
6665
Tox_Err_Decryption err3;
6766
ret = tox_pass_decrypt(enc_data, size2, (const uint8_t *)"correcthorsebatterystaple", 25, dec_data, &err3);
6867
ck_assert_msg(ret, "failed to decrypt save: %d", err3);
69-
tox_options_set_savedata_data(options, dec_data, size);
68+
ck_assert(tox_options_set_savedata(options, dec_data, size));
7069
tox3 = tox_new_log(options, &err2, nullptr);
7170
ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to load from decrypted data: %d", err2);
7271
uint8_t address2[TOX_PUBLIC_KEY_SIZE];
@@ -102,7 +101,7 @@ static void test_save_friend(void)
102101

103102
// and now with the code in use (I only bothered with manually to debug this, and it seems a waste
104103
// to remove the manual check now that it's there)
105-
tox_options_set_savedata_data(options, out1, size);
104+
ck_assert(tox_options_set_savedata(options, out1, size));
106105
Tox *tox4 = tox_new_log(options, &err2, nullptr);
107106
ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to new the third");
108107
uint8_t address5[TOX_PUBLIC_KEY_SIZE];

auto_tests/file_saving_test.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ static void load_data_decrypted(void)
9191
struct Tox_Options *options = tox_options_new(nullptr);
9292
ck_assert(options != nullptr);
9393

94-
tox_options_set_experimental_owned_data(options, true);
9594
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
96-
ck_assert(tox_options_set_savedata_data(options, clear, clear_size));
95+
ck_assert(tox_options_set_savedata(options, clear, clear_size));
9796
free(clear);
9897

9998
Tox_Err_New err;

auto_tests/group_save_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ static void group_save_test(AutoTox *autotoxes)
246246
ck_assert(options != nullptr);
247247

248248
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
249-
tox_options_set_savedata_data(options, save, save_length);
249+
ck_assert(tox_options_set_savedata(options, save, save_length));
250250
tox_options_set_experimental_groups_persistence(options, true);
251251

252252
Tox *new_tox = tox_new_log(options, nullptr, nullptr);

auto_tests/save_compatibility_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static void test_save_compatibility(const char *save_path)
7979
ck_assert_msg(save_data != nullptr, "error while reading save file '%s'", save_path);
8080

8181
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
82-
tox_options_set_savedata_data(options, save_data, size);
82+
ck_assert(tox_options_set_savedata(options, save_data, size));
8383

8484
size_t index = 0;
8585
Tox_Err_New err;

auto_tests/save_friend_test.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,12 @@ int main(void)
149149

150150
size_t save_size = tox_get_savedata_size(tox1);
151151
uint8_t *savedata = (uint8_t *)malloc(save_size);
152+
ck_assert(savedata != nullptr);
152153
tox_get_savedata(tox1, savedata);
153154

154155
struct Tox_Options *const options = tox_options_new(nullptr);
155156
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
156-
tox_options_set_savedata_data(options, savedata, save_size);
157+
ck_assert(tox_options_set_savedata(options, savedata, save_size));
157158

158159
Tox *const tox_to_compare = tox_new_log(options, nullptr, nullptr);
159160

auto_tests/save_load_test.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
#include "../testing/misc_tools.h"
1010
#include "../toxcore/ccompat.h"
1111
#include "../toxcore/tox.h"
12-
#include "../toxcore/tox_struct.h"
13-
#include "../toxcore/util.h"
12+
#include "../toxcore/tox_struct.h" // IWYU pragma: keep
1413
#include "auto_test_support.h"
1514
#include "check_compat.h"
1615

@@ -88,7 +87,7 @@ static void reload_tox(Tox **tox, struct Tox_Options *const in_opts, void *user_
8887

8988
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
9089

91-
tox_options_set_savedata_data(options, buffer + extra, save_size1);
90+
ck_assert(tox_options_set_savedata(options, buffer + extra, save_size1));
9291

9392
*tox = tox_new_log(options, nullptr, user_data);
9493

other/fun/create_savedata.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "../../toxcore/tox.h"
1818
#include "create_common.h"
1919

20-
static bool create_tox(const unsigned char *const secret_key, Tox **const tox)
20+
static bool create_tox(const uint8_t secret_key[TOX_SECRET_KEY_SIZE], Tox **const tox)
2121
{
2222
Tox_Err_Options_New options_error;
2323
struct Tox_Options *const options = tox_options_new(&options_error);
@@ -28,7 +28,10 @@ static bool create_tox(const unsigned char *const secret_key, Tox **const tox)
2828
}
2929

3030
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_SECRET_KEY);
31-
tox_options_set_savedata_data(options, secret_key, crypto_box_SECRETKEYBYTES);
31+
if (!tox_options_set_savedata(options, secret_key, TOX_SECRET_KEY_SIZE)) {
32+
tox_options_free(options);
33+
return false;
34+
}
3235
Tox_Err_New tox_error;
3336
*tox = tox_new(options, &tox_error);
3437

testing/fuzzing/toxsave_fuzz_test.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ void TestSaveDataLoading(Fuzz_Data &input)
2121
CONSUME_OR_RETURN(const uint8_t *savedata, input, savedata_size);
2222

2323
// pass test data to Tox
24-
tox_options_set_savedata_data(tox_options, savedata, savedata_size);
24+
if (!tox_options_set_savedata(tox_options, savedata, savedata_size)) {
25+
tox_options_free(tox_options);
26+
return;
27+
}
2528
tox_options_set_savedata_type(tox_options, TOX_SAVEDATA_TYPE_TOX_SAVE);
2629

2730
Tox_Options_Testing tox_options_testing;

toxcore/tox_options.c

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,6 @@ void tox_options_set_savedata_type(Tox_Options *options, Tox_Savedata_Type saved
123123
{
124124
options->savedata_type = savedata_type;
125125
}
126-
size_t tox_options_get_savedata_length(const Tox_Options *options)
127-
{
128-
return options->savedata_length;
129-
}
130-
void tox_options_set_savedata_length(Tox_Options *options, size_t savedata_length)
131-
{
132-
options->savedata_length = savedata_length;
133-
}
134126
tox_log_cb *tox_options_get_log_callback(const Tox_Options *options)
135127
{
136128
return options->log_callback;
@@ -199,50 +191,82 @@ void tox_options_set_experimental_owned_data(
199191
options->experimental_owned_data = experimental_owned_data;
200192
}
201193

202-
const uint8_t *tox_options_get_savedata_data(const Tox_Options *options)
194+
void tox_options_get_savedata(const Tox_Options *options, uint8_t *savedata)
203195
{
204-
return options->savedata_data;
196+
memcpy(savedata, options->savedata_data, options->savedata_length);
205197
}
206198

207-
bool tox_options_set_savedata_data(Tox_Options *options, const uint8_t *savedata_data, size_t length)
199+
size_t tox_options_get_savedata_size(const Tox_Options *options)
208200
{
209-
if (!options->experimental_owned_data) {
210-
options->savedata_data = savedata_data;
211-
options->savedata_length = length;
212-
return true;
213-
}
201+
return options->savedata_length;
202+
}
214203

204+
bool tox_options_set_savedata(Tox_Options *options, const uint8_t *savedata, size_t savedata_size)
205+
{
215206
if (options->owned_savedata_data != nullptr) {
216207
free(options->owned_savedata_data);
208+
options->savedata_data = nullptr;
209+
options->savedata_length = 0;
217210
options->owned_savedata_data = nullptr;
218211
}
219-
if (savedata_data == nullptr) {
212+
213+
if (savedata == nullptr) {
220214
options->savedata_data = nullptr;
221215
options->savedata_length = 0;
216+
options->owned_savedata_data = nullptr;
222217
return true;
223218
}
224219

225-
uint8_t *owned_ptr = (uint8_t *)malloc(length);
220+
uint8_t *owned_ptr = (uint8_t *)malloc(savedata_size);
226221
if (owned_ptr == nullptr) {
222+
return false;
223+
}
224+
225+
memcpy(owned_ptr, savedata, savedata_size);
226+
options->savedata_data = owned_ptr;
227+
options->savedata_length = savedata_size;
228+
options->owned_savedata_data = owned_ptr;
229+
return true;
230+
}
231+
232+
// Deprecated functions.
233+
bool tox_options_set_savedata_data(
234+
Tox_Options *options, const uint8_t savedata_data[], size_t length)
235+
{
236+
if (!options->experimental_owned_data) {
237+
options->savedata_data = savedata_data;
238+
options->savedata_length = length;
239+
return true;
240+
}
241+
242+
if (!tox_options_set_savedata(options, savedata_data, length)) {
227243
options->savedata_data = savedata_data;
228244
options->savedata_length = length;
229245
options->owned_savedata_data = nullptr;
230246
return false;
231247
}
232248

233-
memcpy(owned_ptr, savedata_data, length);
234-
options->savedata_data = owned_ptr;
235-
options->savedata_length = length;
236-
options->owned_savedata_data = owned_ptr;
237249
return true;
238250
}
251+
void tox_options_set_savedata_length(Tox_Options *options, size_t savedata_length)
252+
{
253+
options->savedata_length = savedata_length;
254+
}
255+
const uint8_t *tox_options_get_savedata_data(const Tox_Options *options)
256+
{
257+
return options->savedata_data;
258+
}
259+
size_t tox_options_get_savedata_length(const Tox_Options *options)
260+
{
261+
return tox_options_get_savedata_size(options);
262+
}
239263

240264
void tox_options_default(Tox_Options *options)
241265
{
242266
if (options != nullptr) {
243267
// Free any owned data.
244268
tox_options_set_proxy_host(options, nullptr);
245-
tox_options_set_savedata_data(options, nullptr, 0);
269+
tox_options_set_savedata(options, nullptr, 0);
246270

247271
// Set the rest to default values.
248272
const Tox_Options default_options = {false};
@@ -279,7 +303,7 @@ void tox_options_free(Tox_Options *options)
279303
if (options != nullptr) {
280304
// Free any owned data.
281305
tox_options_set_proxy_host(options, nullptr);
282-
tox_options_set_savedata_data(options, nullptr, 0);
306+
tox_options_set_savedata(options, nullptr, 0);
283307
free(options);
284308
}
285309
}

0 commit comments

Comments
 (0)