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,32 +3908,29 @@ 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; FILE* f = fopen( data, "rb" );
if( stat( data, &st ) == 0 && (uint64_t)st.st_mtime < m_exectime ) if( f )
{ {
if( st.st_size < ( TargetFrameSize - 16 ) ) struct stat st;
if( fstat( fileno( f ), &st ) == 0 && (uint64_t)st.st_mtime < m_exectime && st.st_size < ( TargetFrameSize - 16 ) )
{ {
FILE* f = fopen( data, "rb" ); auto ptr = (char*)tracy_malloc_fast( st.st_size );
if( f ) auto rd = fread( ptr, 1, st.st_size, f );
if( rd == (size_t)st.st_size )
{ {
auto ptr = (char*)tracy_malloc_fast( st.st_size ); TracyLfqPrepare( QueueType::SourceCodeMetadata );
auto rd = fread( ptr, 1, st.st_size, f ); MemWrite( &item->sourceCodeMetadata.ptr, (uint64_t)ptr );
fclose( f ); MemWrite( &item->sourceCodeMetadata.size, (uint32_t)rd );
if( rd == (size_t)st.st_size ) MemWrite( &item->sourceCodeMetadata.id, id );
{ TracyLfqCommit;
TracyLfqPrepare( QueueType::SourceCodeMetadata ); ok = true;
MemWrite( &item->sourceCodeMetadata.ptr, (uint64_t)ptr ); }
MemWrite( &item->sourceCodeMetadata.size, (uint32_t)rd ); else
MemWrite( &item->sourceCodeMetadata.id, id ); {
TracyLfqCommit; tracy_free_fast( ptr );
ok = true;
}
else
{
tracy_free_fast( ptr );
}
} }
} }
fclose( f );
} }
#ifdef TRACY_DEBUGINFOD #ifdef TRACY_DEBUGINFOD