Skip to content

Fix dangling suffix pointer in tracked library info#175

Draft
ViralBShah wants to merge 1 commit into
mainfrom
fix-suffix-lifetime
Draft

Fix dangling suffix pointer in tracked library info#175
ViralBShah wants to merge 1 commit into
mainfrom
fix-suffix-lifetime

Conversation

@ViralBShah

Copy link
Copy Markdown
Member

Problem

record_library_load() in src/config.c stored the autodetected suffix pointer directly, unlike libname which is deep-copied:

new_libinfo->libname = malloc(namelen); memcpy(...);  // copied
new_libinfo->suffix  = suffix;                         // NOT copied

This is safe only if suffix is always a static string literal. But autodetect_symbol_suffix() checks the caller-supplied suffix_hint first and returns that pointer when it matches. In the LBT_DEFAULT_LIBS code path that hint is a stack buffer in init(), so lbt_config.loaded_libs[i]->suffix dangles once init() returns — a later lbt_get_config() read of .suffix is a use-after-return.

Reproducible with e.g. LBT_DEFAULT_LIBS="libopenblas64.so!64_".

Fix

Deep-copy the suffix in record_library_load() and free it in clear_loaded_libraries(), mirroring the existing libname handling.

Notes

  • Draft for review.
  • Found during a structural review of the repo.

`record_library_load()` stored the autodetected `suffix` pointer directly,
unlike `libname` which is deep-copied. This is only safe when `suffix` is a
static string literal, but `autodetect_symbol_suffix()` checks the
caller-supplied `suffix_hint` first and returns that pointer when it matches.

In the `LBT_DEFAULT_LIBS` path that hint is a stack buffer in `init()`, so
`lbt_config.loaded_libs[i]->suffix` would dangle after `init()` returns and a
later `lbt_get_config()` read would be a use-after-return (triggered by e.g.
`LBT_DEFAULT_LIBS="libopenblas64.so!64_"`).

Deep-copy the suffix in `record_library_load()` and free it in
`clear_loaded_libraries()`, matching the existing handling of `libname`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant