Skip to content

Commit

Permalink
2023-11-15 15:57 UTC+0100 Phil Krylov (phil a t krylov.eu)
Browse files Browse the repository at this point in the history
  * contrib/hbsqlit3/tests/backup.prg
    + Simple change in test to provoke access to dangling pointer saved
      by SQLITE3_TRACE().
  * contrib/hbsqlit3/core.c
    ! Fixed dangling pointer access with SQLITE3_TRACE(), SQLITE3_PROFILE().
  • Loading branch information
tuffnatty committed Nov 15, 2023
1 parent 3939012 commit 45cbb25
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
7 changes: 7 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
Entries may not always be in chronological/commit order.
See license at the end of file. */

2023-11-15 15:57 UTC+0100 Phil Krylov (phil a t krylov.eu)
* contrib/hbsqlit3/tests/backup.prg
+ Simple change in test to provoke access to dangling pointer saved
by SQLITE3_TRACE().
* contrib/hbsqlit3/core.c
! Fixed dangling pointer access with SQLITE3_TRACE(), SQLITE3_PROFILE().

2023-11-14 22:27 UTC+0100 Phil Krylov (phil a t krylov.eu)
* .github/workflows/linux-ci.yml
* On Linux CI workflow, run tests with Valgrind.
Expand Down
62 changes: 56 additions & 6 deletions contrib/hbsqlit3/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ typedef struct
PHB_ITEM cbHookCommit;
PHB_ITEM cbHookRollback;
PHB_ITEM cbFunc;
PHB_ITEM sProfileFileName;
PHB_ITEM sTraceFileName;
} HB_SQLITE3, * PHB_SQLITE3;

typedef struct
Expand Down Expand Up @@ -154,6 +156,17 @@ static HB_GARBAGE_FUNC( hb_sqlite3_destructor )
pStructHolder->hbsqlite3->cbFunc = NULL;
}

if( pStructHolder->hbsqlite3->sProfileFileName )
{
hb_itemRelease( pStructHolder->hbsqlite3->sProfileFileName );
pStructHolder->hbsqlite3->sProfileFileName = NULL;
}
if( pStructHolder->hbsqlite3->sTraceFileName )
{
hb_itemRelease( pStructHolder->hbsqlite3->sTraceFileName );
pStructHolder->hbsqlite3->sTraceFileName = NULL;
}

hb_xfree( pStructHolder->hbsqlite3 );
pStructHolder->hbsqlite3 = NULL;
}
Expand Down Expand Up @@ -182,6 +195,15 @@ static HB_GARBAGE_FUNC( hb_sqlite3_mark )

if( pStructHolder->hbsqlite3->cbFunc )
hb_gcMark( pStructHolder->hbsqlite3->cbFunc );

if( pStructHolder->hbsqlite3->sProfileFileName )
{
hb_gcMark( pStructHolder->hbsqlite3->sProfileFileName );
}
if( pStructHolder->hbsqlite3->sTraceFileName )
{
hb_gcMark( pStructHolder->hbsqlite3->sTraceFileName );
}
}
}

Expand Down Expand Up @@ -1759,8 +1781,8 @@ HB_FUNC( SQLITE3_ENABLE_SHARED_CACHE )
/**
Tracing And Profiling Functions
sqlite3_trace( db, lOnOff )
sqlite3_profile( db, lOnOff )
sqlite3_trace( db, lOnOff, [ filename ] ) // Deprecated in 3.14.0
sqlite3_profile( db, lOnOff, [ filename ] ) // Deprecated in 3.14.0
*/
static void SQL3ProfileLog( void * sFile, const char * sProfileMsg, sqlite3_uint64 uint64 )
{
Expand Down Expand Up @@ -1795,8 +1817,22 @@ HB_FUNC( SQLITE3_PROFILE )
HB_SQLITE3 * pHbSqlite3 = ( HB_SQLITE3 * ) hb_sqlite3_param( 1, HB_SQLITE3_DB, HB_TRUE );

if( pHbSqlite3 && pHbSqlite3->db )
sqlite3_profile( pHbSqlite3->db, hb_parl( 2 ) ? SQL3ProfileLog : NULL,
HB_ISCHAR( 3 ) ? HB_UNCONST( hb_parcx( 3 ) ) : NULL );
{
HB_BOOL bFlag = hb_parl( 2 );
if( pHbSqlite3->sProfileFileName )
{
hb_itemRelease( pHbSqlite3->sProfileFileName );
pHbSqlite3->sProfileFileName = NULL;
}
if( bFlag && HB_ISCHAR( 3 ) )
{
pHbSqlite3->sProfileFileName = hb_itemNew( hb_param( 3, HB_IT_STRING ) );
hb_gcUnlock( pHbSqlite3->sProfileFileName );
}

sqlite3_profile( pHbSqlite3->db, bFlag ? SQL3ProfileLog : NULL,
pHbSqlite3->sProfileFileName ? HB_UNCONST( hb_itemGetCPtr( pHbSqlite3->sProfileFileName ) ) : NULL );
}
else
hb_errRT_BASE_SubstR( EG_ARG, 0, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
}
Expand All @@ -1806,8 +1842,22 @@ HB_FUNC( SQLITE3_TRACE )
HB_SQLITE3 * pHbSqlite3 = ( HB_SQLITE3 * ) hb_sqlite3_param( 1, HB_SQLITE3_DB, HB_TRUE );

if( pHbSqlite3 && pHbSqlite3->db )
sqlite3_trace( pHbSqlite3->db, hb_parl( 2 ) ? SQL3TraceLog : NULL,
HB_ISCHAR( 3 ) ? HB_UNCONST( hb_parcx( 3 ) ) : NULL );
{
HB_BOOL bFlag = hb_parl( 2 );
if( pHbSqlite3->sTraceFileName )
{
hb_itemRelease( pHbSqlite3->sTraceFileName );
pHbSqlite3->sTraceFileName = NULL;
}
if( bFlag && HB_ISCHAR( 3 ) )
{
pHbSqlite3->sTraceFileName = hb_itemNew( hb_param( 3, HB_IT_STRING ) );
hb_gcUnlock( pHbSqlite3->sTraceFileName );
}

sqlite3_trace( pHbSqlite3->db, bFlag ? SQL3TraceLog : NULL,
pHbSqlite3->sTraceFileName ? HB_UNCONST( hb_itemGetCPtr( pHbSqlite3->sTraceFileName ) ) : NULL );
}
else
hb_errRT_BASE_SubstR( EG_ARG, 0, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
}
Expand Down
6 changes: 5 additions & 1 deletion contrib/hbsqlit3/tests/backup.prg
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@

#require "hbsqlit3"

PROCEDURE init_trace( pDbDest, cPrefix )
sqlite3_trace( pDbDest, .T., cPrefix + ".log" )
RETURN

PROCEDURE Main()

LOCAL cFileSource := ":memory:", cFileDest := "backup.db", cSQLTEXT
Expand All @@ -85,7 +89,7 @@ PROCEDURE Main()
RETURN
ENDIF

sqlite3_trace( pDbDest, .T., "backup.log" )
init_trace( pDbDest, "backup" )

pBackup := sqlite3_backup_init( pDbDest, "main", pDbSource, "main" )
IF Empty( pBackup )
Expand Down

0 comments on commit 45cbb25

Please sign in to comment.