Fix/table init stale config and other issues in case of init errors#36
Merged
Fix/table init stale config and other issues in case of init errors#36
Conversation
When a table was dropped without calling cloudsync_cleanup(), its settings entry persisted as an orphan. On next init, trigger creation failed with "no such table" error. Add a database_table_exists() check before creating triggers to gracefully skip stale entries.
The SQLite schema used PRIMARY KEY(tbl_name, key), while PostgreSQL correctly used PRIMARY KEY(tbl_name, col_name, key). The missing col_name caused REPLACE INTO to silently overwrite column-level settings sharing the same (tbl_name, key) pair.
col_merge_stmt and col_value_stmt were allocated with cloudsync_memory_alloc (unzeroed). If an error occurred before the prepared statements were assigned, the cleanup handler called databasevm_finalize on uninitialized garbage pointers, causing a SIGSEGV. Changed all four column arrays to cloudsync_memory_zeroalloc to match col_algo and col_delimiter.
When table_add_to_context_cb failed before incrementing ncols, the column arrays (col_name, col_merge_stmt, etc.) were allocated but never freed because table_free guarded their cleanup with if (ncols > 0). Removed the guard so arrays are always freed. The for loops safely execute zero iterations when ncols is 0. Added a test that uses sqlite3_set_authorizer to force databasevm_prepare to fail inside the callback, exercising the error cleanup path that previously crashed on uninitialized pointers.
marcobambini
approved these changes
Apr 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.