@@ -244,7 +244,7 @@ static int map_spi_result (int rc) {
244244 }
245245}
246246
247- static void clear_fetch_batch (pg_stmt_t * stmt ) {
247+ static void clear_fetch_batch (pg_stmt_t * stmt ) {
248248 if (!stmt ) return ;
249249 if (stmt -> last_tuptable ) {
250250 SPI_freetuptable (stmt -> last_tuptable );
@@ -255,7 +255,7 @@ static void clear_fetch_batch(pg_stmt_t *stmt) {
255255 if (stmt -> row_mcxt ) MemoryContextReset (stmt -> row_mcxt );
256256}
257257
258- static void close_portal (pg_stmt_t * stmt ) {
258+ static void close_portal (pg_stmt_t * stmt ) {
259259 if (!stmt ) return ;
260260 if (stmt -> portal ) {
261261 SPI_cursor_close (stmt -> portal );
@@ -442,13 +442,9 @@ bool database_system_exists (cloudsync_context *data, const char *name, const ch
442442 bool exists = false;
443443
444444 if (strcmp (type , "table" ) == 0 ) {
445- snprintf (query , sizeof (query ),
446- "SELECT 1 FROM pg_tables WHERE schemaname = 'public' AND tablename = '%s'" ,
447- name );
445+ snprintf (query , sizeof (query ), "SELECT 1 FROM pg_tables WHERE schemaname = 'public' AND tablename = '%s'" , name );
448446 } else if (strcmp (type , "trigger" ) == 0 ) {
449- snprintf (query , sizeof (query ),
450- "SELECT 1 FROM pg_trigger WHERE tgname = '%s'" ,
451- name );
447+ snprintf (query , sizeof (query ), "SELECT 1 FROM pg_trigger WHERE tgname = '%s'" , name );
452448 } else {
453449 return false;
454450 }
@@ -540,7 +536,9 @@ int database_exec_callback (cloudsync_context *data, const char *sql, int (*call
540536
541537 // Allocate arrays for column names and values
542538 char * * names = cloudsync_memory_alloc (ncols * sizeof (char * ));
539+ if (!names ) return DBRES_NOMEM ;
543540 char * * values = cloudsync_memory_alloc (ncols * sizeof (char * ));
541+ if (!values ) {cloudsync_memory_free (names ); return DBRES_NOMEM ;}
544542
545543 // Get column names
546544 for (int i = 0 ; i < ncols ; i ++ ) {
@@ -949,8 +947,11 @@ int database_pk_names (cloudsync_context *data, const char *table_name, char ***
949947 Datum datum = SPI_getbinval (tuple , SPI_tuptable -> tupdesc , 1 , & isnull );
950948 if (!isnull ) {
951949 text * txt = DatumGetTextP (datum );
950+ MemoryContext old = MemoryContextSwitchTo (TopMemoryContext );
952951 char * name = text_to_cstring (txt );
953- pk_names [i ] = cloudsync_string_dup (name );
952+ MemoryContextSwitchTo (old );
953+ pk_names [i ] = (name ) ? cloudsync_string_dup (name ) : NULL ;
954+ if (name ) pfree (name );
954955 } else {
955956 pk_names [i ] = NULL ;
956957 }
@@ -1305,20 +1306,21 @@ int databasevm_bind_value (dbvm_t *vm, int index, dbvalue_t *value) {
13051306
13061307 pg_stmt_t * stmt = (pg_stmt_t * )vm ;
13071308 pgvalue_t * v = (pgvalue_t * )value ;
1308- if (!v ) {
1309+ if (!v || v -> isnull ) {
13091310 stmt -> values [idx ] = (Datum )0 ;
13101311 stmt -> types [idx ] = TEXTOID ;
13111312 stmt -> nulls [idx ] = 'n' ;
13121313 } else {
13131314 int16 typlen ;
13141315 bool typbyval ;
1315- MemoryContext old = MemoryContextSwitchTo ( stmt -> bind_mcxt );
1316+
13161317 get_typlenbyval (v -> typeid , & typlen , & typbyval );
1318+ MemoryContext old = MemoryContextSwitchTo (stmt -> bind_mcxt );
13171319 Datum dcopy = typbyval ? v -> datum : datumCopy (v -> datum , typbyval , typlen );
1318- stmt -> values [idx ] = v -> isnull ? ( Datum ) 0 : dcopy ;
1320+ stmt -> values [idx ] = dcopy ;
13191321 MemoryContextSwitchTo (old );
13201322 stmt -> types [idx ] = OidIsValid (v -> typeid ) ? v -> typeid : TEXTOID ;
1321- stmt -> nulls [idx ] = v -> isnull ? 'n' : ' ' ;
1323+ stmt -> nulls [idx ] = ' ' ;
13221324 }
13231325
13241326 if (stmt -> nparams < idx + 1 ) stmt -> nparams = idx + 1 ;
0 commit comments