Skip to content

Commit aa29fd7

Browse files
committed
Several minor issues fixed
1 parent e598b41 commit aa29fd7

File tree

4 files changed

+51
-39
lines changed

4 files changed

+51
-39
lines changed

src/cloudsync.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
#define CLOUDSYNC_MIN_DB_VERSION 0
5151

5252
#define CLOUDSYNC_PAYLOAD_SKIP_SCHEMA_HASH_CHECK 1
53-
#define CLOUDSYNC_PAYLOAD_MINBUF_SIZE 512*1024
54-
#define CLOUDSYNC_PAYLOAD_SIGNATURE 'CLSY'
53+
#define CLOUDSYNC_PAYLOAD_MINBUF_SIZE (512*1024)
54+
#define CLOUDSYNC_PAYLOAD_SIGNATURE 0x434C5359 /* 'C','L','S','Y' */
5555
#define CLOUDSYNC_PAYLOAD_VERSION_ORIGNAL 1
5656
#define CLOUDSYNC_PAYLOAD_VERSION_1 CLOUDSYNC_PAYLOAD_VERSION_ORIGNAL
5757
#define CLOUDSYNC_PAYLOAD_VERSION_2 2
@@ -978,7 +978,7 @@ bool table_add_to_context (cloudsync_context *data, table_algo algo, const char
978978
}
979979

980980
int ncols = database_count_nonpk(data, table_name);
981-
if (count < 0) {cloudsync_set_dberror(data); goto abort_add_table;}
981+
if (ncols < 0) {cloudsync_set_dberror(data); goto abort_add_table;}
982982
int rc = table_add_stmts(table, ncols);
983983
if (rc != DBRES_OK) goto abort_add_table;
984984

@@ -1508,6 +1508,9 @@ void cloudsync_context_free (void *ctx) {
15081508
DEBUG_SETTINGS("cloudsync_context_free %p", data);
15091509
if (!data) return;
15101510

1511+
// free all table contexts and prepared statements
1512+
cloudsync_terminate(data);
1513+
15111514
cloudsync_memory_free(data->tables);
15121515
cloudsync_memory_free(data);
15131516
}
@@ -1615,7 +1618,7 @@ int cloudsync_begin_alter (cloudsync_context *data, const char *table_name) {
16151618
}
16161619

16171620
// drop original triggers
1618-
database_delete_triggers(data, table_name);
1621+
rc = database_delete_triggers(data, table_name);
16191622
if (rc != DBRES_OK) {
16201623
char buffer[1024];
16211624
snprintf(buffer, sizeof(buffer), "Unable to delete triggers for table %s in cloudsync_begin_alter.", table_name);
@@ -2075,7 +2078,7 @@ int cloudsync_payload_encode_step (cloudsync_payload_context *payload, cloudsync
20752078
char *buffer = payload->buffer + payload->bused;
20762079
size_t bsize = payload->balloc - payload->bused;
20772080
char *p = pk_encode((dbvalue_t **)argv, argc, buffer, false, &bsize, data->skip_decode_idx);
2078-
if (!p) cloudsync_set_error(data, "An error occurred while encoding payload", DBRES_ERROR);
2081+
if (!p) return cloudsync_set_error(data, "An error occurred while encoding payload", DBRES_ERROR);
20792082

20802083
// update buffer
20812084
payload->bused += breq;
@@ -2224,6 +2227,9 @@ static int cloudsync_payload_decode_callback (void *xdata, int index, int type,
22242227
// #ifndef CLOUDSYNC_OMIT_RLS_VALIDATION
22252228

22262229
int cloudsync_payload_apply (cloudsync_context *data, const char *payload, int blen, int *pnrows) {
2230+
// sanity check
2231+
if (blen < (int)sizeof(cloudsync_payload_header)) return cloudsync_set_error(data, "Error on cloudsync_payload_apply: invalid payload length", DBRES_MISUSE);
2232+
22272233
// decode header
22282234
cloudsync_payload_header header;
22292235
memcpy(&header, payload, sizeof(cloudsync_payload_header));
@@ -2250,30 +2256,30 @@ int cloudsync_payload_apply (cloudsync_context *data, const char *payload, int b
22502256
}
22512257

22522258
const char *buffer = payload + sizeof(cloudsync_payload_header);
2253-
blen -= sizeof(cloudsync_payload_header);
2254-
2259+
size_t buf_len = (size_t)blen - sizeof(cloudsync_payload_header);
2260+
22552261
// sanity check checksum (only if version is >= 2)
22562262
if (header.version >= CLOUDSYNC_PAYLOAD_MIN_VERSION_WITH_CHECKSUM) {
2257-
uint64_t checksum = pk_checksum(buffer, blen);
2263+
uint64_t checksum = pk_checksum(buffer, buf_len);
22582264
if (cloudsync_payload_checksum_verify(&header, checksum) == false) {
22592265
return cloudsync_set_error(data, "Error on cloudsync_payload_apply: invalid checksum", DBRES_MISUSE);
22602266
}
22612267
}
2262-
2268+
22632269
// check if payload is compressed
22642270
char *clone = NULL;
22652271
if (header.expanded_size != 0) {
22662272
clone = (char *)cloudsync_memory_alloc(header.expanded_size);
22672273
if (!clone) return cloudsync_set_error(data, "Unable to allocate memory to uncompress payload", DBRES_NOMEM);
2268-
2269-
uint32_t rc = LZ4_decompress_safe(buffer, clone, blen, header.expanded_size);
2270-
if (rc <= 0 || rc != header.expanded_size) {
2274+
2275+
int lz4_rc = LZ4_decompress_safe(buffer, clone, (int)buf_len, (int)header.expanded_size);
2276+
if (lz4_rc <= 0 || (uint32_t)lz4_rc != header.expanded_size) {
22712277
if (clone) cloudsync_memory_free(clone);
22722278
return cloudsync_set_error(data, "Error on cloudsync_payload_apply: unable to decompress BLOB", DBRES_MISUSE);
22732279
}
2274-
2280+
22752281
buffer = (const char *)clone;
2276-
blen = header.expanded_size;
2282+
buf_len = (size_t)header.expanded_size;
22772283
}
22782284

22792285
// precompile the insert statement
@@ -2298,7 +2304,7 @@ int cloudsync_payload_apply (cloudsync_context *data, const char *payload, int b
22982304

22992305
for (uint32_t i=0; i<nrows; ++i) {
23002306
size_t seek = 0;
2301-
int res = pk_decode((char *)buffer, blen, ncols, &seek, data->skip_decode_idx, cloudsync_payload_decode_callback, &decoded_context);
2307+
int res = pk_decode((char *)buffer, buf_len, ncols, &seek, data->skip_decode_idx, cloudsync_payload_decode_callback, &decoded_context);
23022308
if (res == -1) {
23032309
if (in_savepoint) database_rollback_savepoint(data, "cloudsync_payload_apply");
23042310
rc = DBRES_ERROR;
@@ -2356,7 +2362,7 @@ int cloudsync_payload_apply (cloudsync_context *data, const char *payload, int b
23562362
}
23572363

23582364
buffer += seek;
2359-
blen -= seek;
2365+
buf_len -= seek;
23602366
dbvm_reset(vm);
23612367
}
23622368

@@ -2424,7 +2430,7 @@ int cloudsync_payload_get (cloudsync_context *data, char **blob, int *blob_size,
24242430
if (rc != DBRES_OK) return rc;
24252431

24262432
// exit if there is no data to send
2427-
if (blob == NULL || blob_size == 0) return DBRES_OK;
2433+
if (blob == NULL || *blob_size == 0) return DBRES_OK;
24282434
return rc;
24292435
}
24302436

@@ -2567,7 +2573,7 @@ int cloudsync_cleanup_internal (cloudsync_context *data, cloudsync_table_context
25672573
}
25682574

25692575
// drop original triggers
2570-
database_delete_triggers(data, table_name);
2576+
rc = database_delete_triggers(data, table_name);
25712577
if (rc != DBRES_OK) {
25722578
char buffer[1024];
25732579
snprintf(buffer, sizeof(buffer), "Unable to delete triggers for table %s", table_name);
@@ -2666,6 +2672,13 @@ int cloudsync_init_table (cloudsync_context *data, const char *table_name, const
26662672
snprintf(buffer, sizeof(buffer), "Unknown CRDT algorithm name %s", algo_name);
26672673
return cloudsync_set_error(data, buffer, DBRES_ERROR);
26682674
}
2675+
2676+
// DWS and AWS algorithms are not yet implemented in the merge logic
2677+
if (algo_new == table_algo_crdt_dws || algo_new == table_algo_crdt_aws) {
2678+
char buffer[1024];
2679+
snprintf(buffer, sizeof(buffer), "CRDT algorithm %s is not yet supported", algo_name);
2680+
return cloudsync_set_error(data, buffer, DBRES_ERROR);
2681+
}
26692682

26702683
// check if table name was already augmented
26712684
table_algo algo_current = dbutils_table_settings_get_algo(data, table_name);

src/dbutils.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,23 +165,22 @@ int dbutils_settings_get_value (cloudsync_context *data, const char *key, char *
165165
}
166166

167167
int dbutils_settings_set_key_value (cloudsync_context *data, const char *key, const char *value) {
168+
if (!key) return DBRES_MISUSE;
168169
DEBUG_SETTINGS("dbutils_settings_set_key_value key: %s value: %s", key, value);
169-
170+
170171
int rc = DBRES_OK;
171-
if (key && value) {
172+
if (value) {
172173
const char *values[] = {key, value};
173174
DBTYPE types[] = {DBTYPE_TEXT, DBTYPE_TEXT};
174175
int lens[] = {-1, -1};
175176
rc = database_write(data, SQL_SETTINGS_SET_KEY_VALUE_REPLACE, values, types, lens, 2);
176-
}
177-
178-
if (value == NULL) {
177+
} else {
179178
const char *values[] = {key};
180179
DBTYPE types[] = {DBTYPE_TEXT};
181180
int lens[] = {-1};
182181
rc = database_write(data, SQL_SETTINGS_SET_KEY_VALUE_DELETE, values, types, lens, 1);
183182
}
184-
183+
185184
if (rc == DBRES_OK && data) cloudsync_sync_key(data, key, value);
186185
return rc;
187186
}
@@ -336,34 +335,34 @@ table_algo dbutils_table_settings_get_algo (cloudsync_context *data, const char
336335

337336
int dbutils_settings_load_callback (void *xdata, int ncols, char **values, char **names) {
338337
cloudsync_context *data = (cloudsync_context *)xdata;
339-
340-
for (int i=0; i<ncols; i+=2) {
338+
339+
for (int i=0; i+1<ncols; i+=2) {
341340
const char *key = values[i];
342341
const char *value = values[i+1];
343342
cloudsync_sync_key(data, key, value);
344343
DEBUG_SETTINGS("key: %s value: %s", key, value);
345344
}
346-
345+
347346
return 0;
348347
}
349348

350349
int dbutils_settings_table_load_callback (void *xdata, int ncols, char **values, char **names) {
351350
cloudsync_context *data = (cloudsync_context *)xdata;
352351

353-
for (int i=0; i<ncols; i+=4) {
352+
for (int i=0; i+3<ncols; i+=4) {
354353
const char *table_name = values[i];
355354
// const char *col_name = values[i+1];
356355
const char *key = values[i+2];
357356
const char *value = values[i+3];
358357
if (strcmp(key, "algo")!=0) continue;
359-
358+
360359
table_algo algo = cloudsync_algo_from_name(value);
361360
if (database_create_triggers(data, table_name, algo) != DBRES_OK) return DBRES_MISUSE;
362361
if (table_add_to_context(data, algo, table_name) == false) return DBRES_MISUSE;
363-
362+
364363
DEBUG_SETTINGS("load tbl_name: %s value: %s", key, value);
365364
}
366-
365+
367366
return 0;
368367
}
369368

src/pk.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,14 @@ int pk_decode_print_callback (void *xdata, int index, int type, int64_t ival, do
144144
return DBRES_OK;
145145
}
146146

147-
uint64_t pk_checksum (const char *buffer, int blen) {
147+
uint64_t pk_checksum (const char *buffer, size_t blen) {
148148
const uint8_t *p = (const uint8_t *)buffer;
149-
uint64_t h = 14695981039346656037ULL;
150-
for (int i = 0; i < blen; i++) {
151-
h ^= p[i];
152-
h *= 1099511628211ULL;
153-
}
154-
return h;
149+
uint64_t h = 14695981039346656037ULL;
150+
for (size_t i = 0; i < blen; i++) {
151+
h ^= p[i];
152+
h *= 1099511628211ULL;
153+
}
154+
return h;
155155
}
156156

157157
// MARK: - Decoding -

src/pk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ int pk_decode (char *buffer, size_t blen, int count, size_t *seek, int skip_d
2323
int pk_decode_bind_callback (void *xdata, int index, int type, int64_t ival, double dval, char *pval);
2424
int pk_decode_print_callback (void *xdata, int index, int type, int64_t ival, double dval, char *pval);
2525
size_t pk_encode_size (dbvalue_t **argv, int argc, int reserved, int skip_idx);
26-
uint64_t pk_checksum (const char *buffer, int blen);
26+
uint64_t pk_checksum (const char *buffer, size_t blen);
2727

2828
#endif

0 commit comments

Comments
 (0)