Skip to content

HandleHelper.GetProcessesUsingFile: int.Parse can throw FormatException #98

@Christophe-Rogiers

Description

@Christophe-Rogiers

Bug Description

In GetProcessesUsingFile(), int.Parse() is used to convert the PID from the regex match. Although the regex captures \d+, a match with an extremely large number could cause an OverflowException, and any future regex change could introduce FormatException. Using int.TryParse() would make this robust without relying on the regex pattern as the sole safety net.

Actual Behavior

// HandleHelper.cs lines 73-77
processes.Add(new ProcessHandleInfo
{
    ProcessName = match.Groups["name"].Value.Trim(),
    ProcessId = int.Parse(match.Groups["pid"].Value)
});

If handle.exe outputs a PID exceeding int.MaxValue (2,147,483,647), this throws an unhandled OverflowException and aborts the entire enumeration.

Suggested Fix

Use int.TryParse() and skip invalid entries instead of crashing:

foreach (Match match in regex.Matches(output))
{
    if (match.Success && int.TryParse(match.Groups["pid"].Value, out int pid))
    {
        processes.Add(new ProcessHandleInfo
        {
            ProcessName = match.Groups["name"].Value.Trim(),
            ProcessId = pid
        });
    }
}

Environment

  • File: src/Servy.Core/Helpers/HandleHelper.cs
  • Line: 76

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions