Skip to content

Fix memory and lifecycle clang-tidy warnings#13494

Open
Mic92 wants to merge 3 commits intoNixOS:masterfrom
Mic92:clang-tidy-memory-fixes
Open

Fix memory and lifecycle clang-tidy warnings#13494
Mic92 wants to merge 3 commits intoNixOS:masterfrom
Mic92:clang-tidy-memory-fixes

Conversation

@Mic92
Copy link
Copy Markdown
Member

@Mic92 Mic92 commented Jul 17, 2025

Summary

This PR fixes several memory and lifecycle issues found by clang-tidy:

  • Fix NULL argument to getcwd warning in run.cc by using a safer approach
  • Fix memory leak in nix_api_value.cc test by properly freeing allocated memory
  • Fix use-after-free warning in repl.cc by properly ordering operations
  • Remove vfork usage which can cause undefined behavior

These fixes improve code safety and prevent potential crashes or memory leaks.

@Mic92 Mic92 requested a review from edolstra as a code owner July 17, 2025 13:47
@github-actions github-actions bot added new-cli Relating to the "nix" command repl The Read Eval Print Loop, "nix repl" command and debugger c api Nix as a C library with a stable interface labels Jul 17, 2025
@Mic92 Mic92 force-pushed the clang-tidy-memory-fixes branch from dbbf5f2 to 40391d4 Compare July 17, 2025 15:36
@Mic92 Mic92 force-pushed the clang-tidy-memory-fixes branch from 40391d4 to 7b7f81a Compare July 17, 2025 15:42
@Mic92 Mic92 force-pushed the clang-tidy-memory-fixes branch from 7b7f81a to b7fbbf3 Compare August 11, 2025 07:04
Mic92 added 3 commits August 11, 2025 09:08
Clang-tidy warned about passing NULL to getcwd, which is a GNU extension
and not portable. This could lead to undefined behavior on non-GNU systems.

Introduced a getCurrentWorkingDirectory() helper function that:
- Uses the standard C++ std::filesystem::current_path()
- Wraps filesystem exceptions to throw SysError for consistency
- Returns a std::filesystem::path for modern C++ usage

Updated both absPath() and run.cc to use this new helper function,
eliminating the platform-specific code and the clang-tidy warning.

This fixes the warning:
  The 1st argument to 'getcwd' is NULL but should not be NULL
  [clang-analyzer-unix.StdCLibraryFunctions]
Replace malloc/free with std::unique_ptr to ensure memory is freed
even if test assertions fail early. This prevents memory leaks when
ASSERT_* macros cause early test exit.

Fixes clang-tidy warning: Potential leak of memory pointed to by
'out_name' [clang-analyzer-unix.Malloc]
Getting rid of vfork() potentially can cause Nix to start failing when
(for instance) a Nix process with a multi-gigabyte heap tries to run a
small child program like git.

Add NOLINT comments to suppress clang-tidy warnings about vfork being
insecure. The performance benefit of vfork can be significant in
memory-constrained situations.
@Mic92 Mic92 force-pushed the clang-tidy-memory-fixes branch from b7fbbf3 to 95a4cb8 Compare August 11, 2025 07:08
@Mic92 Mic92 enabled auto-merge August 11, 2025 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c api Nix as a C library with a stable interface new-cli Relating to the "nix" command repl The Read Eval Print Loop, "nix repl" command and debugger

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants