Skip to content

Commit 1424229

Browse files
committed
fix: prevent nomem error if the cloudsync_set_apikey or set_token methods are called before cloudsync_network_init
1 parent c278fcb commit 1424229

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/network.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,15 @@ bool network_compute_endpoints (sqlite3_context *context, network_data *data, co
412412
}
413413

414414
if (result) {
415-
data->authentication = authentication;
415+
if (authentication) {
416+
if (data->authentication) cloudsync_memory_free(data->authentication);
417+
data->authentication = authentication;
418+
}
419+
420+
if (data->check_endpoint) cloudsync_memory_free(data->check_endpoint);
416421
data->check_endpoint = check_endpoint;
422+
423+
if (data->upload_endpoint) cloudsync_memory_free(data->upload_endpoint);
417424
data->upload_endpoint = upload_endpoint;
418425
}
419426

@@ -437,14 +444,23 @@ void network_result_to_sqlite_error (sqlite3_context *context, NETWORK_RESULT re
437444

438445
// MARK: - Init / Cleanup -
439446

447+
network_data *cloudsync_network_data(sqlite3_context *context) {
448+
network_data *data = (network_data *)cloudsync_get_auxdata(context);
449+
if (data) return data;
450+
451+
data = (network_data *)cloudsync_memory_zeroalloc(sizeof(network_data));
452+
if (data) cloudsync_set_auxdata(context, data);
453+
return data;
454+
}
455+
440456
void cloudsync_network_init (sqlite3_context *context, int argc, sqlite3_value **argv) {
441457
DEBUG_FUNCTION("cloudsync_network_init");
442458

443459
curl_global_init(CURL_GLOBAL_ALL);
444460

445461
// no real network operations here
446462
// just setup the network_data struct
447-
network_data *data = (network_data *)cloudsync_memory_zeroalloc(sizeof(network_data));
463+
network_data *data = cloudsync_network_data(context);
448464
if (!data) goto abort_memory;
449465

450466
// init context
@@ -508,7 +524,7 @@ void cloudsync_network_cleanup (sqlite3_context *context, int argc, sqlite3_valu
508524
// MARK: - Public -
509525

510526
bool cloudsync_network_set_authentication_token (sqlite3_context *context, const char *value, bool is_token) {
511-
network_data *data = (network_data *)cloudsync_get_auxdata(context);
527+
network_data *data = cloudsync_network_data(context);
512528
if (!data) return false;
513529

514530
const char *key = (is_token) ? "token" : "apikey";

0 commit comments

Comments
 (0)