Skip to content

Commit e7d6a3e

Browse files
committed
Fixed a couple of potential leaks in case of error and renamed internal structs.
1 parent bc673f4 commit e7d6a3e

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

src/cloudsync.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ typedef struct {
206206
size_t bused;
207207
uint64_t nrows;
208208
uint16_t ncols;
209-
} cloudsync_network_payload;
209+
} cloudsync_data_payload;
210210

211211
#ifdef _MSC_VER
212212
#pragma pack(push, 1) // For MSVC: pack struct with 1-byte alignment
@@ -224,7 +224,7 @@ typedef struct PACKED {
224224
uint32_t nrows;
225225
uint64_t schema_hash;
226226
uint8_t unused[6]; // padding to ensure the struct is exactly 32 bytes
227-
} cloudsync_network_header;
227+
} cloudsync_payload_header;
228228

229229
typedef struct {
230230
sqlite3_value *table_name;
@@ -1920,16 +1920,16 @@ int local_update_move_meta (sqlite3 *db, cloudsync_table_context *table, const c
19201920

19211921
// MARK: - Payload Encode / Decode -
19221922

1923-
bool cloudsync_buffer_free (cloudsync_network_payload *payload) {
1923+
bool cloudsync_buffer_free (cloudsync_data_payload *payload) {
19241924
if (payload) {
19251925
if (payload->buffer) cloudsync_memory_free(payload->buffer);
1926-
memset(payload, 0, sizeof(cloudsync_network_payload));
1926+
memset(payload, 0, sizeof(cloudsync_data_payload));
19271927
}
19281928

19291929
return false;
19301930
}
19311931

1932-
bool cloudsync_buffer_check (cloudsync_network_payload *payload, size_t needed) {
1932+
bool cloudsync_buffer_check (cloudsync_data_payload *payload, size_t needed) {
19331933
// alloc/resize buffer
19341934
if (payload->bused + needed > payload->balloc) {
19351935
if (needed < CLOUDSYNC_PAYLOAD_MINBUF_SIZE) needed = CLOUDSYNC_PAYLOAD_MINBUF_SIZE;
@@ -1940,15 +1940,15 @@ bool cloudsync_buffer_check (cloudsync_network_payload *payload, size_t needed)
19401940

19411941
payload->buffer = buffer;
19421942
payload->balloc = balloc;
1943-
if (payload->nrows == 0) payload->bused = sizeof(cloudsync_network_header);
1943+
if (payload->nrows == 0) payload->bused = sizeof(cloudsync_payload_header);
19441944
}
19451945

19461946
return true;
19471947
}
19481948

1949-
void cloudsync_network_header_init (cloudsync_network_header *header, uint32_t expanded_size, uint16_t ncols, uint32_t nrows, uint64_t hash) {
1950-
memset(header, 0, sizeof(cloudsync_network_header));
1951-
assert(sizeof(cloudsync_network_header)==32);
1949+
void cloudsync_payload_header_init (cloudsync_payload_header *header, uint32_t expanded_size, uint16_t ncols, uint32_t nrows, uint64_t hash) {
1950+
memset(header, 0, sizeof(cloudsync_payload_header));
1951+
assert(sizeof(cloudsync_payload_header)==32);
19521952

19531953
int major, minor, patch;
19541954
sscanf(CLOUDSYNC_VERSION, "%d.%d.%d", &major, &minor, &patch);
@@ -1969,7 +1969,7 @@ void cloudsync_payload_encode_step (sqlite3_context *context, int argc, sqlite3_
19691969
// debug_values(argc, argv);
19701970

19711971
// allocate/get the session context
1972-
cloudsync_network_payload *payload = (cloudsync_network_payload *)sqlite3_aggregate_context(context, sizeof(cloudsync_network_payload));
1972+
cloudsync_data_payload *payload = (cloudsync_data_payload *)sqlite3_aggregate_context(context, sizeof(cloudsync_data_payload));
19731973
if (!payload) return;
19741974

19751975
// check if the step function is called for the first time
@@ -1993,7 +1993,7 @@ void cloudsync_payload_encode_final (sqlite3_context *context) {
19931993
DEBUG_FUNCTION("cloudsync_payload_encode_final");
19941994

19951995
// get the session context
1996-
cloudsync_network_payload *payload = (cloudsync_network_payload *)sqlite3_aggregate_context(context, sizeof(cloudsync_network_payload));
1996+
cloudsync_data_payload *payload = (cloudsync_data_payload *)sqlite3_aggregate_context(context, sizeof(cloudsync_data_payload));
19971997
if (!payload) return;
19981998

19991999
if (payload->nrows == 0) {
@@ -2002,7 +2002,7 @@ void cloudsync_payload_encode_final (sqlite3_context *context) {
20022002
}
20032003

20042004
// encode payload
2005-
int header_size = (int)sizeof(cloudsync_network_header);
2005+
int header_size = (int)sizeof(cloudsync_payload_header);
20062006
int real_buffer_size = (int)(payload->bused - header_size);
20072007
int zbound = LZ4_compressBound(real_buffer_size);
20082008
char *buffer = cloudsync_memory_alloc(zbound + header_size);
@@ -2013,15 +2013,15 @@ void cloudsync_payload_encode_final (sqlite3_context *context) {
20132013
}
20142014

20152015
// adjust buffer to compress to skip the reserved header
2016-
char *src_buffer = payload->buffer + sizeof(cloudsync_network_header);
2016+
char *src_buffer = payload->buffer + sizeof(cloudsync_payload_header);
20172017
int zused = LZ4_compress_default(src_buffer, buffer+header_size, real_buffer_size, zbound);
20182018
bool use_uncompressed_buffer = (!zused || zused > real_buffer_size);
20192019
CHECK_FORCE_UNCOMPRESSED_BUFFER();
20202020

2021-
// setup payload network header
2021+
// setup payload header
20222022
cloudsync_context *data = (cloudsync_context *)sqlite3_user_data(context);
2023-
cloudsync_network_header header;
2024-
cloudsync_network_header_init(&header, (use_uncompressed_buffer) ? 0 : real_buffer_size, payload->ncols, (uint32_t)payload->nrows, data->schema_hash);
2023+
cloudsync_payload_header header;
2024+
cloudsync_payload_header_init(&header, (use_uncompressed_buffer) ? 0 : real_buffer_size, payload->ncols, (uint32_t)payload->nrows, data->schema_hash);
20252025

20262026
// if compression fails or if compressed size is bigger than original buffer, then use the uncompressed buffer
20272027
if (use_uncompressed_buffer) {
@@ -2031,8 +2031,8 @@ void cloudsync_payload_encode_final (sqlite3_context *context) {
20312031
}
20322032

20332033
// copy header and data to SQLite BLOB
2034-
memcpy(buffer, &header, sizeof(cloudsync_network_header));
2035-
int blob_size = zused+sizeof(cloudsync_network_header);
2034+
memcpy(buffer, &header, sizeof(cloudsync_payload_header));
2035+
int blob_size = zused+sizeof(cloudsync_payload_header);
20362036
sqlite3_result_blob(context, buffer, blob_size, SQLITE_TRANSIENT);
20372037

20382038
// cleanup memory
@@ -2105,8 +2105,8 @@ int cloudsync_pk_decode_bind_callback (void *xdata, int index, int type, int64_t
21052105

21062106
int cloudsync_payload_apply (sqlite3_context *context, const char *payload, int blen) {
21072107
// decode header
2108-
cloudsync_network_header header;
2109-
memcpy(&header, payload, sizeof(cloudsync_network_header));
2108+
cloudsync_payload_header header;
2109+
memcpy(&header, payload, sizeof(cloudsync_payload_header));
21102110

21112111
header.signature = ntohl(header.signature);
21122112
header.expanded_size = ntohl(header.expanded_size);
@@ -2131,8 +2131,8 @@ int cloudsync_payload_apply (sqlite3_context *context, const char *payload, int
21312131
return -1;
21322132
}
21332133

2134-
const char *buffer = payload + sizeof(cloudsync_network_header);
2135-
blen -= sizeof(cloudsync_network_header);
2134+
const char *buffer = payload + sizeof(cloudsync_payload_header);
2135+
blen -= sizeof(cloudsync_payload_header);
21362136

21372137
// check if payload is compressed
21382138
char *clone = NULL;
@@ -2245,7 +2245,7 @@ void cloudsync_payload_decode (sqlite3_context *context, int argc, sqlite3_value
22452245

22462246
// sanity check payload size
22472247
int blen = sqlite3_value_bytes(argv[0]);
2248-
if (blen < (int)sizeof(cloudsync_network_header)) {
2248+
if (blen < (int)sizeof(cloudsync_payload_header)) {
22492249
dbutils_context_result_error(context, "Error on cloudsync_payload_decode: invalid input size.");
22502250
sqlite3_result_error_code(context, SQLITE_MISUSE);
22512251
return;

src/cloudsync.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
extern "C" {
2121
#endif
2222

23-
#define CLOUDSYNC_VERSION "0.8.31"
23+
#define CLOUDSYNC_VERSION "0.8.32"
2424

2525
int sqlite3_cloudsync_init (sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi);
2626

src/network.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,11 +716,12 @@ int cloudsync_network_send_changes_internal (sqlite3_context *context, int argc,
716716
return rc;
717717
}
718718

719-
// exit if there are no data to send
719+
// exit if there is no data to send
720720
if (blob == NULL || blob_size == 0) return SQLITE_OK;
721721

722722
NETWORK_RESULT res = network_receive_buffer(data, data->upload_endpoint, data->authentication, true, false, NULL, CLOUDSYNC_HEADER_SQLITECLOUD);
723723
if (res.code != CLOUDSYNC_NETWORK_BUFFER) {
724+
cloudsync_memory_free(blob);
724725
network_result_to_sqlite_error(context, res, "cloudsync_network_send_changes unable to receive upload URL");
725726
return SQLITE_ERROR;
726727
}
@@ -730,6 +731,7 @@ int cloudsync_network_send_changes_internal (sqlite3_context *context, int argc,
730731
cloudsync_memory_free(blob);
731732
if (sent == false) {
732733
network_result_to_sqlite_error(context, res, "cloudsync_network_send_changes unable to upload BLOB changes to remote host.");
734+
network_result_cleanup(&res);
733735
return SQLITE_ERROR;
734736
}
735737

@@ -743,6 +745,7 @@ int cloudsync_network_send_changes_internal (sqlite3_context *context, int argc,
743745
res = network_receive_buffer(data, data->upload_endpoint, data->authentication, true, true, json_payload, CLOUDSYNC_HEADER_SQLITECLOUD);
744746
if (res.code != CLOUDSYNC_NETWORK_OK) {
745747
network_result_to_sqlite_error(context, res, "cloudsync_network_send_changes unable to notify BLOB upload to remote host.");
748+
network_result_cleanup(&res);
746749
return SQLITE_ERROR;
747750
}
748751

@@ -786,7 +789,7 @@ int cloudsync_network_check_internal(sqlite3_context *context) {
786789
NETWORK_RESULT result = network_receive_buffer(data, endpoint, data->authentication, true, true, NULL, CLOUDSYNC_HEADER_SQLITECLOUD);
787790
int rc = SQLITE_OK;
788791
if (result.code == CLOUDSYNC_NETWORK_BUFFER) {
789-
rc = network_download_changes (context, result.buffer);
792+
rc = network_download_changes(context, result.buffer);
790793
} else {
791794
rc = network_set_sqlite_result(context, &result);
792795
}

0 commit comments

Comments
 (0)