Skip to content

fix: guard WinHttp URL port parsing exceptions#60

Open
H-Chris233 wants to merge 4 commits into
OpenSteam001:mainfrom
H-Chris233:fix-winhttp-port-parse-guard
Open

fix: guard WinHttp URL port parsing exceptions#60
H-Chris233 wants to merge 4 commits into
OpenSteam001:mainfrom
H-Chris233:fix-winhttp-port-parse-guard

Conversation

@H-Chris233
Copy link
Copy Markdown
Contributor

@H-Chris233 H-Chris233 commented May 26, 2026

Problem

WinHttp::ParseUrl calls std::stoi on the port substring extracted from URLs like host:port. If the port field is empty, non-numeric, or out of range, std::stoi throws std::invalid_argument or std::out_of_range, crashing the caller.

Changes

  • Wrapped std::stoi call in a try-catch block.
  • Added explicit port range validation: only values in [1, 65535] are accepted.
  • Added full-string consumption check via std::stol's pos parameter to reject partially-numeric port strings (e.g., host:12312abc).
  • On parse failure, returns an invalid ParsedUrl, which callers already handle as a "skip this URL" signal.

Testing

  • Tested with malformed URLs: host:, host:abc, host:999999, host:12312abc, and valid URLs to confirm no regression.

@JanitorialMess
Copy link
Copy Markdown
Contributor

std::stol has the same partial-parse behavior as std::stoi: host:12312abc is accepted as port 12312, so malformed ports with a numeric prefix still pass.
If the goal is to reject malformed ports, the parser needs to verify that the entire port substring was consumed.

Also, the PR description says invalid ports leave the port at default 0, but ParseUrl initializes the port to 80 / 443 before parsing, and on port parse failure it returns before setting valid. So the behavior is "return invalid ParsedUrl", not "port defaults to 0" unless I missed something.

std::stol accepts strings like "12312abc" by parsing only the numeric
prefix. Use the pos parameter to verify the entire port substring was
consumed, and treat trailing garbage as invalid.
@H-Chris233
Copy link
Copy Markdown
Contributor Author

Good catches, thanks for the review.

  1. Partial parse: Fixed by using std::stol's pos parameter — now verifies the entire port substring was consumed. Strings like "12312abc" are rejected correctly.

  2. PR description: Updated — the description now says "returns an invalid ParsedUrl" instead of "port defaults to 0".

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.

2 participants