@@ -630,6 +630,7 @@ int database_create_triggers (cloudsync_context *data, const char *table_name, t
630630 // UPDATE TRIGGER
631631 if (algo == table_algo_crdt_gos ) rc = database_create_update_trigger_gos (data , table_name );
632632 else rc = database_create_update_trigger (data , table_name , trigger_when );
633+ if (rc != SQLITE_OK ) return rc ;
633634
634635 // DELETE TRIGGER
635636 if (algo == table_algo_crdt_gos ) rc = database_create_delete_trigger_gos (data , table_name );
@@ -668,7 +669,7 @@ int database_delete_triggers (cloudsync_context *data, const char *table) {
668669 if (rc != SQLITE_OK ) goto finalize ;
669670
670671finalize :
671- if (rc != SQLITE_OK ) DEBUG_ALWAYS ("dbutils_delete_triggers error %s (%s)" , database_errmsg (cloudsync_db ( data ) ), sql );
672+ if (rc != SQLITE_OK ) DEBUG_ALWAYS ("dbutils_delete_triggers error %s (%s)" , database_errmsg (data ), sql );
672673 return rc ;
673674}
674675
@@ -694,7 +695,7 @@ bool database_check_schema_hash (cloudsync_context *data, uint64_t hash) {
694695 // the idea is to allow changes on stale peers and to be able to apply these changes on peers with newer schema,
695696 // but it requires alter table operation on augmented tables only add new columns and never drop columns for backward compatibility
696697 char sql [1024 ];
697- snprintf (sql , sizeof (sql ), "SELECT 1 FROM cloudsync_schema_versions WHERE hash = (%" PRId64 ")" , hash );
698+ snprintf (sql , sizeof (sql ), "SELECT 1 FROM cloudsync_schema_versions WHERE hash = (%" PRIu64 ")" , hash );
698699
699700 int64_t value = 0 ;
700701 database_select_int (data , sql , & value );
@@ -717,7 +718,7 @@ int database_update_schema_hash (cloudsync_context *data, uint64_t *hash) {
717718
718719 char sql [1024 ];
719720 snprintf (sql , sizeof (sql ), "INSERT INTO cloudsync_schema_versions (hash, seq) "
720- "VALUES (%" PRId64 ", COALESCE((SELECT MAX(seq) FROM cloudsync_schema_versions), 0) + 1) "
721+ "VALUES (%" PRIu64 ", COALESCE((SELECT MAX(seq) FROM cloudsync_schema_versions), 0) + 1) "
721722 "ON CONFLICT(hash) DO UPDATE SET "
722723 "seq = (SELECT COALESCE(MAX(seq), 0) + 1 FROM cloudsync_schema_versions);" , h );
723724 rc = database_exec (data , sql );
@@ -748,7 +749,9 @@ void databasevm_clear_bindings (dbvm_t *vm) {
748749}
749750
750751const char * databasevm_sql (dbvm_t * vm ) {
751- return sqlite3_expanded_sql ((sqlite3_stmt * )vm );
752+ return sqlite3_sql ((sqlite3_stmt * )vm );
753+ // the following allocates memory that needs to be freed
754+ // return sqlite3_expanded_sql((sqlite3_stmt *)vm);
752755}
753756
754757static int database_pk_rowid (sqlite3 * db , const char * table_name , char * * * names , int * count ) {
@@ -762,8 +765,9 @@ static int database_pk_rowid (sqlite3 *db, const char *table_name, char ***names
762765
763766 if (rc == SQLITE_OK ) {
764767 char * * r = (char * * )cloudsync_memory_alloc (sizeof (char * ));
765- if (!r ) return SQLITE_NOMEM ;
768+ if (!r ) { rc = SQLITE_NOMEM ; goto cleanup ;}
766769 r [0 ] = cloudsync_string_dup ("rowid" );
770+ if (!r [0 ]) {cloudsync_memory_free (r ); rc = SQLITE_NOMEM ; goto cleanup ;}
767771 * names = r ;
768772 * count = 1 ;
769773 } else {
@@ -805,21 +809,28 @@ int database_pk_names (cloudsync_context *data, const char *table_name, char ***
805809 if (rc != SQLITE_OK ) goto cleanup ;
806810
807811 // allocate array
808- char * * r = (char * * )cloudsync_memory_alloc (sizeof (char * ) * rows );
812+ char * * r = (char * * )cloudsync_memory_zeroalloc (sizeof (char * ) * rows );
809813 if (!r ) {rc = SQLITE_NOMEM ; goto cleanup ;}
810814
811815 int i = 0 ;
812816 while ((rc = sqlite3_step (vm )) == SQLITE_ROW ) {
813817 const char * txt = (const char * )sqlite3_column_text (vm , 0 );
814- if (!txt ) {rc = SQLITE_ERROR ; goto cleanup ;}
818+ if (!txt ) {rc = SQLITE_ERROR ; goto cleanup_r ;}
815819 r [i ] = cloudsync_string_dup (txt );
816- if (!r [i ]) { rc = SQLITE_NOMEM ; goto cleanup ;}
820+ if (!r [i ]) { rc = SQLITE_NOMEM ; goto cleanup_r ;}
817821 i ++ ;
818822 }
819823 if (rc == SQLITE_DONE ) rc = SQLITE_OK ;
820824
821825 * names = r ;
822826 * count = rows ;
827+ goto cleanup ;
828+
829+ cleanup_r :
830+ for (int j = 0 ; j < i ; j ++ ) {
831+ if (r [j ]) cloudsync_memory_free (r [j ]);
832+ }
833+ cloudsync_memory_free (r );
823834
824835cleanup :
825836 if (vm ) sqlite3_finalize (vm );
0 commit comments