Skip to content

Fix trailing env var and NULL handling in pgexporter_resolve_path - #708

Open
hardikkaurani wants to merge 1 commit into
pgexporter:mainfrom
hardikkaurani:fix/resolve-path-trailing-env
Open

hardikkaurani wants to merge 1 commit into
pgexporter:mainfrom
hardikkaurani:fix/resolve-path-trailing-env

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Summary

pgexporter_resolve_path() in src/libpgexporter/utils.c previously had four bugs when resolving configuration paths:

  1. Trailing environment variable dropped / NULL dereference: Environment variables were only flushed when a non-identifier character followed them (if (in_env && !valid_env_char)). A path ending with an env var such as "$HOME" left in_env == true and res == NULL, crashing on strlen(res) (SIGSEGV), while /prefix/$SUBDIR silently dropped $SUBDIR and leaked env_res.
  2. Empty/empty-quoted input crash: Inputs such as "" or "\"\"" left res == NULL, crashing on strlen(res).
  3. strlen(orig_path) before NULL check: strlen(orig_path) executed before if (orig_path == NULL).
  4. Leaked buffers on error paths: Early return 1; statements skipped cleanup and error: did not free res or env_res.

Fixes:

  • Validate orig_path and new_path before calling strlen(orig_path).
  • Extract append_resolved_env() to handle both mid-path and trailing environment variables consistently and safely free *env_res.
  • Ensure res is initialized to "" when empty before length checking, and free res/env_res in error:.
  • Add test_utils_resolve_path in test/testcases/test_utils.c.

Verification

  • Formatted all modified files with clang-format and verified git diff --check is clean.
  • Full C build and pgexporter-test suite will run in GitHub Actions CI.

Copilot AI balanced review requested due to automatic review settings November 27, 2026 06:27
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi @jesperpedersen,

I opened this PR to fix pgexporter_resolve_path() in src/libpgexporter/utils.c:

  • Trailing environment variables (e.g. "$HOME" or "/prefix/$SUBDIR") were never flushed after the loop, either silently dropping the variable or crashing in strlen(res) when res remained NULL.
  • Empty/empty-quoted paths ("", "\"\"") also hit strlen(NULL).
  • strlen(orig_path) ran before the NULL check, and error paths leaked res / env_res.
  • Added append_resolved_env(), fixed error cleanup and bounds ordering, and added test_utils_resolve_path in test/testcases/test_utils.c, formatted with clang-format.

Please let me know if any adjustments are needed.

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Pranav-error

Copy link
Copy Markdown
Collaborator

#692 is merged now. It fixes the Debug crash in test_connection_bind_unix_socket_permissions (#691), which is what fails both build-linux Debug jobs here. Please rebase on main.

@hardikkaurani
hardikkaurani force-pushed the fix/resolve-path-trailing-env branch from 7071a7a to 415fbda Compare December 2, 2026 21:27
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

@Pranav-error Rebased onto main (415fbda) to pick up #692.

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