Skip to content

wolfSSL_PEM_X509_X509_CRL_X509_PKEY_read_bio: avoid quadratic PEM marker scanning - #11405

Open
per-allansson wants to merge 1 commit into
wolfSSL:masterfrom
per-allansson:pem-parse-expo-slow
Open

wolfSSL_PEM_X509_X509_CRL_X509_PKEY_read_bio: avoid quadratic PEM marker scanning#11405
per-allansson wants to merge 1 commit into
wolfSSL:masterfrom
per-allansson:pem-parse-expo-slow

Conversation

@per-allansson

Copy link
Copy Markdown
Contributor

The PEM reader rescanned previously examined data after each byte read, causing quadratic scanning time for large PEM objects.

Track the search position for BEGIN and END markers, retaining enough overlap to detect markers split across reads.

Also check the buffer bound before reading the newline after the header.

…ker scanning

The PEM reader rescanned previously examined data after each byte read,
causing quadratic scanning time for large PEM objects.

Track the search position for BEGIN and END markers, retaining enough
overlap to detect markers split across reads.

Also check the buffer bound before reading the newline after the header.
Copilot AI lite review requested due to automatic review settings September 9, 2026 05:28
@wolfSSL-Bot

Copy link
Copy Markdown

Can one of the admins verify this patch?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The changes are localized, consistent with existing parsing logic, and address both performance and a concrete buffer-bound safety check without altering the PEM parsing contract.

Pull request overview

This PR optimizes wolfSSL_PEM_X509_X509_CRL_X509_PKEY_read_bio()’s PEM header/footer detection to avoid repeated rescanning of already-processed bytes, preventing quadratic-time behavior when reading large PEM objects from a BIO.

Changes:

  • Track and advance a searchPos offset so -----BEGIN / -----END marker searches only scan a small overlapping window instead of the entire accumulated buffer.
  • Add an explicit i >= l bound check before reading the post-header newline byte.
File summaries
File Description
src/x509.c Makes PEM marker scanning linear-time by keeping search position state and hardens the newline read with a buffer bound check.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Lite

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

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.

3 participants