Repository navigation
Open
Description
Problem
Several queries find the *_cloudsync metadata tables with LIKE '%_cloudsync' and no ESCAPE. In LIKE, _ matches any single character. On SQLite, LIKE also ignores case. So a user table whose name merely ends in cloudsync (for example appcloudsync) is treated as a metadata table, and every query built from the list then fails on it.
Reproduction (SQLite)
CREATE TABLE t (id TEXT PRIMARY KEY NOT NULL, v TEXT);
SELECT cloudsync_init('t');
INSERT INTO t VALUES ('a', 'x');
SELECT count(*) FROM cloudsync_changes; -- 1
CREATE TABLE appcloudsync (x INTEGER);
SELECT count(*) FROM cloudsync_changes; -- SQL logic error
SELECT cloudsync_db_version(); -- Unable to retrieve db_version (no such column: db_version)
SELECT count(*) FROM cloudsync_payload_chunks(0, randomblob(16), NULL, 1);
-- no such column: t1.db_versionSync stops working for the whole database: local writes, /check and /apply all depend on these queries.
Affected call sites (main)
| File | Use |
|---|---|
src/sqlite/cloudsync_changes_sqlite.c:146 |
cloudsync_changes union |
src/sqlite/cloudsync_changes_sqlite.c:420 |
metadata-table existence check |
src/sqlite/sql_sqlite.c:78 |
SQL_DBVERSION_BUILD_QUERY |
src/network/network.c:2044 |
network_send_window |
src/sqlite/database_sqlite.c:671 |
database_cleanup (NOT LIKE patterns, so matching tables are skipped instead) |
src/postgresql/sql_postgresql.c:96 |
SQL_DBVERSION_BUILD_QUERY (case-sensitive, but _ is still a wildcard) |
build_union_sql in src/postgresql/cloudsync_postgresql.c already escapes the underscore (LIKE '%\_cloudsync' ESCAPE '\'). PR #90 adds one more SQLite lookup with the same pattern (payload_chunks_metadata_max).
Proposed fix
- Escape the underscore everywhere:
LIKE '%\_cloudsync' ESCAPE '\'. On SQLite also make the match case-sensitive, e.g.substr(tbl_name, -10) = '_cloudsync'orGLOB '*_cloudsync'. - Better: derive the list from the tables cloudsync actually manages (
cloudsync_table_settings, or the context's table list) instead of matching names. This also excludes a user table literally namedfoo_cloudsyncthat cloudsync didn't create. - Fix all call sites in one change so the scan, the db_version query and the watermark lookup always read the same set of tables.
- Add a test with decoy tables (
appcloudsync,X_CLOUDSYNC) on both backends.
Metadata
Metadata
Assignees
Labels
No labels
Activity