Skip to content

add realpath() and fix setenv() to store the reallocated environ array - #15

Merged
codewiz merged 2 commits into
masterfrom
fix/setenv-environ
Sep 8, 2026
Merged

codewiz merged 2 commits into
masterfrom
fix/setenv-environ

Conversation

@codewiz

@codewiz codewiz commented Sep 7, 2026 •

Copy link
Copy Markdown
Member

The AmigaOS-hosted gcc and cpp from the 16.2 packages crash on startup. The driver inserts several environment variables with setenv, and libnix's insert path grows environ_ptr__data with realloc but never stores the new pointer. The second insert reallocs the already freed block, reads a garbage size from its header and hands exec a bogus FreeMem. Under vamos that logs deallocate: block outside of mem header! and then spins on the corrupted chunk list; on AmigaOS it corrupts the system memory list.

  • setenv: store the reallocated array, and return -1 with errno set instead of ENOMEM on failure.
  • realpath: new, in nix20 since it needs NameFromLock. Lock plus NameFromLock into the caller's buffer, or a malloc(MAXPATHLEN) one when it is NULL. gcc's libiberty uses it to compare file names.

With these two the hosted driver runs and compiles under vamos; what remains is a binutils bug in cc1's exit path: AmigaPorts/binutils-gdb#21. The libiberty fallback it used to hit without realpath is AmigaPorts/gcc#37.

@codewiz codewiz changed the title setenv: store the reallocated environ array; add realpath add realpath() and fix setenv() to store the reallocated environ array Sep 7, 2026
@codewiz
codewiz requested review from MBeijer and a lite review from Copilot September 7, 2026 14:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The setenv() insert path still reallocates the environment pointer array with an off-by-one size, which can overflow by one pointer and corrupt the heap.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR addresses AmigaOS-hosted toolchain startup crashes by fixing setenv()’s environment-array reallocation handling, and adds an AmigaOS implementation of realpath() needed by gcc/libiberty to canonicalize filenames.

Changes:

  • Fix setenv() to persist the (potentially moved) realloc()’d environ_ptr__data pointer and to return -1 with errno set on allocation failure.
  • Add sources/nix20/extra/realpath.c implementing realpath() via Lock() + NameFromLock(), allocating a buffer when resolved == NULL.
File summaries
File Description
sources/nix20/stdlib/putenv.c Adjusts setenv() failure semantics and stores the reallocated environ_ptr__data pointer.
sources/nix20/extra/realpath.c Introduces AmigaOS realpath() implementation using DOS locking/name resolution APIs.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread sources/nix20/stdlib/putenv.c Outdated
The insert path grew environ_ptr__data with realloc but kept the old
pointer, so the next setenv reallocated a freed block and corrupted the
heap. The new size was also one pointer short of the entry plus the
terminator. Return -1 with errno set instead of ENOMEM on failure.
Resolve the name with Lock and NameFromLock into the caller buffer, or a
malloc(MAXPATHLEN) one when it is NULL. Programs such as gcc use it to
compare file names.
@codewiz
codewiz merged commit 41ee42f into master Sep 8, 2026
@codewiz
codewiz deleted the fix/setenv-environ branch September 8, 2026 11:04
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.

3 participants