Copy the filename in backtrace_create_state_for_file.

The state opens the file lazily on first use (fileline_initialize), so
it must own the filename: the caller's buffer may be freed as soon as
this returns. Pass a copy into backtrace_create_state.
This commit is contained in:
Bartosz Taudul
2026-08-30 16:06:18 +02:00
parent 8b41c38ed9
commit 601d57de35
2 changed files with 17 additions and 5 deletions

View File

@@ -94,7 +94,10 @@ extern struct backtrace_state *backtrace_create_state (
data to be loaded directly from the file with base_address=0.
The caller is responsible for converting runtime virtual addresses
to ELF virtual addresses before passing them to backtrace_pcinfo
or backtrace_syminfo. */
or backtrace_syminfo.
The filename is copied into state-owned memory (the file is opened
lazily on first use), so the caller's buffer need not outlive this
call. */
extern struct backtrace_state *backtrace_create_state_for_file (
const char *filename, int threaded,

View File

@@ -81,11 +81,20 @@ backtrace_create_state_for_file (const char *filename, int threaded,
backtrace_error_callback error_callback,
void *data)
{
struct backtrace_state *state;
/* The state opens the file lazily on first use (fileline_initialize),
so it must own the filename: the caller's buffer may be freed as
soon as this returns. */
const size_t len = strlen (filename) + 1;
char *copy = (char*)backtrace_alloc (NULL, len, error_callback, data);
if (copy == NULL)
return NULL;
memcpy (copy, filename, len);
state = backtrace_create_state (filename, threaded, error_callback, data);
if (state != NULL)
state->external_file = 1;
struct backtrace_state *state =
backtrace_create_state (copy, threaded, error_callback, data);
if (state == NULL)
return NULL;
state->external_file = 1;
return state;
}