Skip to content

fix: searchFiles ReferenceError on stdout?.trim() #436

Description

@avoidwork

Summary

The searchFiles tool throws a ReferenceError when executing due to an unsafe optional chaining call on stdout?.trim() in the searchFilesImpl function.

Environment

  • OS: Linux 7.0.2-7-pve
  • Node.js: v25.8.1
  • madz version: 1.18.0
  • LLM provider: Unknown — user to confirm

Reproduction

  1. Call the searchFiles tool
  2. Observe ReferenceError on stdout?.trim()

Expected Behavior

The tool should search files and return matches without throwing an error.

Actual Behavior

A ReferenceError is thrown on the line const output = stdout?.trim() ?? stdout;

Additional Context

The problematic code is in ./src/tools/filesystem.js at line 436:

const output = stdout?.trim() ?? stdout;

Proposed fix: Remove the .trim() call from this line and apply it to the next line where output.split(" ") is called, ensuring stdout is handled safely before any method calls.


Audit Findings

  • File: src/tools/filesystem.js:436 — The searchFilesImpl function destructures { stdout } from execFile result, then applies optional chaining stdout?.trim(). If stdout is undefined (e.g., timeout or error case), stdout?.trim() returns undefined, and undefined ?? stdout evaluates to undefined (since stdout is also undefined). This causes output to be undefined, leading to a TypeError on output.split(n) at line 442.
  • File: src/tools/filesystem.js:435-442 — The execFile call has a 10s timeout. If the timeout triggers, the promise rejects and the catch block handles it. However, if stdout is undefined for other reasons (e.g., empty output, encoding issues), the optional chaining doesn't provide a fallback value.
  • Recommendation: Replace const output = stdout?.trim() ?? stdout; with const output = (stdout ?? ).trim(); to ensure output is always a string before calling split(). This handles undefined, null, and empty string cases gracefully.

Metadata

Metadata

Assignees

No one assigned

    Labels

    approvedAn identifier for Madz to take action.bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions