Skip to content

fix: prevent nil pointer panic on ProcessState and improve CGI environment compliance - #122

Open
mazkev wants to merge 1 commit into
msoap:masterfrom
mazkev:fix/nil-process-state-and-cgi-env
Open

mazkev wants to merge 1 commit into
msoap:masterfrom
mazkev:fix/nil-process-state-and-cgi-env

Conversation

@mazkev

@mazkev mazkev commented Nov 16, 2026

Copy link
Copy Markdown

Summary of Changes

  1. Fix Nil Pointer Panic on osExecCommand.ProcessState (execShellCommand):

    • When a command fails before execution (e.g., binary not found in $PATH when -shell="" is configured, invalid permissions, or bad executable path), osExecCommand.ProcessState remains nil.
    • Accessing osExecCommand.ProcessState.ExitCode() previously caused an unhandled nil pointer dereference panic, taking down the HTTP server process.
    • Added a safe nil check for osExecCommand.ProcessState and fallback exit code 1 when err != nil.
  2. Improve CGI Specification (RFC 3875) Compliance (setCGIEnv):

    • Multi-value Headers: RFC 3875 §4.1.18 requires multiple header values to be joined with a comma. Previously, only the first element headerValue[0] was used, silently discarding remaining values. Now joined using strings.Join(headerValue, ", ").
    • CONTENT_LENGTH & CONTENT_TYPE: Added standard CGI environment variables from req.ContentLength and req.Header.Get("Content-Type").
    • REMOTE_ADDR: If net.SplitHostPort(req.RemoteAddr) fails (e.g. Unix domain socket or raw IP), fall back to req.RemoteAddr instead of leaving remoteHost empty.
  3. Test Improvements:

    • Replaced hardcoded Unix "/dev/null" in tests with cross-platform os.DevNull.
    • Added Test_execShellCommand_ProcessStateNil to ensure non-existent commands fail gracefully without panicking.
    • Added Test_setCGIEnv_MultiHeadersAndContent to verify multi-value headers, content length, and content type environment variables.

Verification

  • go test -v ./... passed.
  • GOARCH=386 go test ./... passed.
  • go vet ./... passed cleanly.
  • gofmt -d . passed with 0 diffs.

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.

1 participant