libiberty: return a copy of the name when lrealpath has no realpath - #37
Merged
Merged
Conversation
On hosts without realpath, canonicalize_file_name or _WIN32 the function fell off its end, so gcc at -Os compiled it to a bare rts and "input file is the same as output file" fired on every -o.
There was a problem hiding this comment.
🟢 Approval recommended
The change is minimal, well-scoped, and resolves a clear undefined-behavior bug by aligning the final fallback behavior with existing failure paths that already return a duplicated filename.
Pull request overview
This PR fixes an undefined-behavior bug in libiberty’s lrealpath() on hosts that lack any supported canonicalization mechanism (realpath, canonicalize_file_name, or Win32 APIs). Previously, the function could fall off the end without returning a value, causing callers like canonical_filename_eq to behave incorrectly (e.g., treating unrelated paths as equal).
Changes:
- Add a final fallback in
lrealpath()toreturn strdup(filename)when no canonicalization method is available. - Document (in-code) why returning a copy is required to avoid undefined return values and incorrect equality comparisons.
File summaries
| File | Description |
|---|---|
| libiberty/lrealpath.c | Adds a safe final fallback return path to prevent undefined behavior when no canonicalization APIs exist on the host. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
MBeijer
approved these changes
Sep 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
lrealpathhas no code path for a host withoutrealpath,canonicalize_file_nameor Win32: it falls off the end of the function. At-Osgcc compiles that into a barerts, so on such a host both file names "canonicalize" to whatever happens to be in d0 andcanonical_filename_eqreports them equal. The AmigaOS-hosted driver then refused every-o:Return a copy of the name instead, which is what the other fallbacks do when their canonicalization fails.
Found while chasing the AmigaOS-hosted gcc crash reported for the AmigaOS-hosted package; libnix now provides
realpath, so with a current libnix this fallback is no longer reached on AmigaOS, but it keeps libiberty correct on any host that lacks it. Same fix for the binutils copy: AmigaPorts/binutils-gdb#20. libnix realpath: AmigaPorts/libnix#15.