Fix file access race condition.

This commit is contained in:
Bartosz Taudul 2024-02-07 17:14:31 +01:00
parent 312713b83c
commit 5037742ab0
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3

View File

@ -3908,17 +3908,14 @@ void Profiler::HandleSymbolCodeQuery( uint64_t symbol, uint32_t size )
void Profiler::HandleSourceCodeQuery( char* data, char* image, uint32_t id ) void Profiler::HandleSourceCodeQuery( char* data, char* image, uint32_t id )
{ {
bool ok = false; bool ok = false;
struct stat st;
if( stat( data, &st ) == 0 && (uint64_t)st.st_mtime < m_exectime )
{
if( st.st_size < ( TargetFrameSize - 16 ) )
{
FILE* f = fopen( data, "rb" ); FILE* f = fopen( data, "rb" );
if( f ) if( f )
{
struct stat st;
if( fstat( fileno( f ), &st ) == 0 && (uint64_t)st.st_mtime < m_exectime && st.st_size < ( TargetFrameSize - 16 ) )
{ {
auto ptr = (char*)tracy_malloc_fast( st.st_size ); auto ptr = (char*)tracy_malloc_fast( st.st_size );
auto rd = fread( ptr, 1, st.st_size, f ); auto rd = fread( ptr, 1, st.st_size, f );
fclose( f );
if( rd == (size_t)st.st_size ) if( rd == (size_t)st.st_size )
{ {
TracyLfqPrepare( QueueType::SourceCodeMetadata ); TracyLfqPrepare( QueueType::SourceCodeMetadata );
@ -3933,7 +3930,7 @@ void Profiler::HandleSourceCodeQuery( char* data, char* image, uint32_t id )
tracy_free_fast( ptr ); tracy_free_fast( ptr );
} }
} }
} fclose( f );
} }
#ifdef TRACY_DEBUGINFOD #ifdef TRACY_DEBUGINFOD