binutils Newspaper
JUNE 15, 2026
ld Proposed

Ld: Refactor input_statement_is_archive_path() to not touch its argument

The linker's input_statement_is_archive_path function is refactored to avoid modifying its input argument, improving code safety.

This patch refactors the input_statement_is_archive_path function in the linker to avoid modifying its input argument file_spec. Alan Modra applied a modified version of Calvin Owens’ original patch, eliminating an unnecessary copy of the input string. The refactoring involves assigning the result of strchr to a const char* and ensuring archive_path returns a const char*. This prevents unintended side effects and improves the overall safety and maintainability of the code.

In the Thread 1 participant
  1. Calvin Owens <calvin@wbinvd.org> proposer

    Acknowledges Alan Modra's modifications and expresses gratitude for applying the patch.

    “It's very generous of you to credit me, but not necessary :)”

In Details

The patch refactors input_statement_is_archive_path in ld/ldlang.c to avoid modifying its input. Originally, a copy of file_spec was created using stat_strdup to allow for in-place modification by temporarily null-terminating the string. The refactored version avoids this copy by using const char* for the sep argument (the result of strchr) and ensuring that archive_path also returns const char*. This change eliminates a potential memory allocation and improves const correctness.

For Context

The input_statement_is_archive_path function in the linker checks if a given input statement refers to an archive file path. This patch improves the function by avoiding modifications to its input, making it safer and more predictable. Instead of copying the input string, the function now uses constant pointers (const char*) to work with the string without altering it. This reduces the risk of unintended side effects and memory allocation overhead.

Part of a Series

Filed Under: ldrefactoringconst correctnesslinkercode safety