{"record":{"id":"90dcb07b839d6ec3","repo":"pranshuparmar/witr","slug":"invalid-pid-d","errorCode":null,"errorMessage":"invalid pid %d","messagePattern":"invalid pid (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/proc/process_darwin.go","lineNumber":19,"sourceCode":"//go:build darwin\n\npackage proc\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\t\"os/exec\"\n\t\"strconv\"\n\t\"strings\"\n\t\"sync\"\n\t\"time\"\n\n\t\"github.com/pranshuparmar/witr/pkg/model\"\n)\n\nfunc ReadProcess(pid int) (model.Process, error) {\n\tif pid <= 0 {\n\t\treturn model.Process{}, fmt.Errorf(\"invalid pid %d\", pid)\n\t}\n\tpidStr := strconv.Itoa(pid)\n\n\t// Format: pid(0) ppid(1) uid(2) lstart(3-7) state(8) pcpu(9) rss(10) args(11+)\n\t// ucomm is excluded because it can contain spaces (e.g. \"Microsoft Teams\"),\n\t// which breaks strings.Fields parsing. The display name is derived from args instead.\n\tcmd := exec.Command(\"ps\", \"-p\", pidStr, \"-o\", \"pid=,ppid=,uid=,lstart=,state=,pcpu=,rss=,args=\")\n\tcmd.Env = buildEnvForPS()\n\tout, err := cmd.Output()\n\tif err != nil {\n\t\treturn model.Process{}, fmt.Errorf(\"process %d not found: %w\", pid, err)\n\t}\n\n\tline := strings.TrimSpace(string(out))\n\tif line == \"\" {\n\t\treturn model.Process{}, fmt.Errorf(\"process %d not found\", pid)\n\t}\n","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/pranshuparmar/witr/blob/dc4fa1da82d3e266fcbd928641b4f30b3077c64f/internal/proc/process_darwin.go#L1-L37","documentation":"On macOS, ReadProcess shells out to `ps` and refuses to run at all for non-positive PIDs. PID 0 is the kernel and negative values are never valid userland PIDs, so there is nothing to read. The error is a pure input-validation guard.","triggerScenarios":"Calling ReadProcess (directly or via pidIdentityChanged) with pid <= 0 — e.g. an uninitialized pid variable, a zero value from a struct field, or a sentinel used to mean 'no process'.","commonSituations":"Caller populates the PID from a lookup that returned nothing and passes 0 through; code paths that haven't discovered the parent yet and pass 0 as the parent PID.","solutions":["Check the pid value at the call site; fix the source that produced 0/negative.","Treat pid 0 as 'unknown process' in your logic and skip the ReadProcess call.","Validate PID > 0 before invoking witr APIs."],"exampleFix":"// before\nproc, err := proc.ReadProcess(parentPid) // parentPid == 0\n// after\nif parentPid > 0 {\n    proc, err = proc.ReadProcess(parentPid)\n}","handlingStrategy":"validation","validationCode":"if pid <= 0 {\n    return fmt.Errorf(\"cannot read process: pid %d is invalid\", pid)\n}","typeGuard":null,"tryCatchPattern":"proc, err := proc.ReadProcess(pid)\nif err != nil {\n    if strings.Contains(err.Error(), \"invalid pid\") {\n        // caller bug: fix the PID source rather than retrying\n    }\n}","preventionTips":["Never pass sentinel 0 for 'unknown parent'; use a separate found flag","Initialize PID fields explicitly and validate before lookup","Skip the lookup entirely when pid <= 0"],"tags":["macos","input-validation","pid"],"backgroundTag":"invalid-pid","analyzedSha":"dc4fa1da82d3e266fcbd928641b4f30b3077c64f","analyzedAt":"2026-09-01T12:17:08.767Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}