Skip to content

examples: clean up crawler link parsing resources - #23008

Closed
minnnjuuu wants to merge 1 commit into
curl:masterfrom
minnnjuuu:examples-crawler-resource-cleanup
Closed

minnnjuuu wants to merge 1 commit into
curl:masterfrom
minnnjuuu:examples-crawler-resource-cleanup

Conversation

@minnnjuuu

Copy link
Copy Markdown
Contributor

What is the purpose of the change?

follow_links() owns the libxml document returned by htmlReadMemory() and each xmlChar URL returned by xmlNodeListGetString() or xmlBuildURI(). The document was not released on several returns or after a successful scan, and short links and the link-limit exit could leave the current URL allocated.

The existing post-increment limit check also admitted one more HTTP(S) link than max_link_per_page. With the default limit of 5, the function could add 6 handles.

What does this change do?

  • Handle a failed XPath-context allocation.
  • Release the parsed document on all paths after a successful parse.
  • Release the current URL before short-link continue and limit break paths.
  • Check the link cap before adding a handle and increment the count once per accepted HTTP(S) link.

Testing

  • perl scripts/checksrc.pl docs/examples/crawler.c
  • Built crawler.c with GCC using -Wall -Wextra -Werror
  • Verified the link cap and cleanup paths with a focused ASan/UBSan harness

AI-assisted discovery disclosure

This issue was initially identified with AI-assisted tooling. The patch was then manually reviewed and validated with the tests described above.

Release all libxml objects on exit paths.

Enforce the configured link limit before adding a new handle.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The cleanup paths and link-counting logic are complete and consistent with libxml ownership requirements.

Review effort: Balanced
Findings: None

What changed in this PR

Fixes resource leaks and enforces the crawler’s per-page link limit correctly.

Changes:

  • Frees libxml documents and URL allocations on all exit paths.
  • Handles XPath context allocation failure.
  • Prevents exceeding max_link_per_page.
File Description
docs/​examples/​crawler.c Corrects link parsing cleanup and limit enforcement.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@bagder

bagder commented Sep 21, 2026

Copy link
Copy Markdown
Member

Thanks!

@bagder bagder closed this in 370367f Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants