Skip to content

net/mail: fix quadratic complexity in consumeComment#78393

Closed
mohammadmseet-hue wants to merge 2 commits intogolang:masterfrom
mohammadmseet-hue:fix/mail-consumeComment-quadratic
Closed

net/mail: fix quadratic complexity in consumeComment#78393
mohammadmseet-hue wants to merge 2 commits intogolang:masterfrom
mohammadmseet-hue:fix/mail-consumeComment-quadratic

Conversation

@mohammadmseet-hue
Copy link
Copy Markdown
Contributor

@mohammadmseet-hue mohammadmseet-hue commented Mar 27, 2026

consumeComment builds the comment string by repeated string
concatenation inside a loop. Each concatenation copies the
entire string built so far, making the function O(n^2) in the
depth of nested comments.

Replace the concatenation with a strings.Builder, which
amortizes allocation by doubling its internal buffer. This
reduces consumeComment from O(n^2) to O(n).

This is the same bug class as the consumeDomainLiteral fix
in CVE-2025-61725.

Benchmark results (benchstat, 8 runs):

name old time/op new time/op delta
ConsumeComment/depth10 2.481us 1.838us -25.92%
ConsumeComment/depth100 86.58us 6.498us -92.50%
ConsumeComment/depth1000 7.963ms 52.82us -99.34%
ConsumeComment/depth10000 897.8ms 521.3us -99.94%

The quadratic cost becomes visible at depth 100 and dominant
by depth 1000. At depth 10000, the fix is roughly 1700x
faster.

@gopherbot
Copy link
Copy Markdown
Contributor

This PR (HEAD: dab3c52) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/759940.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Gopher Robot:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/759940.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Sean Liao:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/759940.
After addressing review feedback, remember to publish your drafts!

consumeComment builds a string one byte at a time using string
concatenation (comment += p.s[:1]), which is O(n^2) because
each concatenation copies the entire accumulated string.

This is the same bug class as the consumeDomainLiteral fix in
CL 4631656 (CVE-2025-61725), which was not applied to
consumeComment.

Use strings.Builder to accumulate the comment in O(n) time.

                              |       old       |                 new                 |
                              |     sec/op      |    sec/op     vs base               |
ConsumeComment/depth-10-16         720.0n +-  7%   184.5n +-  6%  -74.38% (p=0.002 n=6)
ConsumeComment/depth-100-16       20.393u +- 12%   1.330u +- 19%  -93.48% (p=0.002 n=6)
ConsumeComment/depth-1000-16     1310.28u +-  5%   12.36u +- 14%  -99.06% (p=0.002 n=6)
ConsumeComment/depth-10000-16   107001.2u +-  8%   148.5u +- 10%  -99.86% (p=0.002 n=6)
geomean                            213.0u         4.606u          -97.84%

Change-Id: Iebf12c81f3c64e3a80f6428a4b27fdaf1aca81d0
@mohammadmseet-hue mohammadmseet-hue force-pushed the fix/mail-consumeComment-quadratic branch from dab3c52 to b456a66 Compare April 3, 2026 15:04
@gopherbot
Copy link
Copy Markdown
Contributor

Message from Mohammad Seet:

Patch Set 1:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/759940.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

This PR (HEAD: 7742dad) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/759940.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Mohammad Seet:

Patch Set 2:

Done. Removed duplicated title from body and ensured clean formatting.


Please don’t reply on this GitHub thread. Visit golang.org/cl/759940.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Sean Liao:

Patch Set 3: Auto-Submit+1 Code-Review+2 Commit-Queue+1

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/759940.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Go LUCI:

Patch Set 3:

Dry run: CV is trying the patch.

Bot data: {"action":"start","triggered_at":"2026-04-06T11:13:37Z","revision":"3cbd06d00250569e976d8593ae85318dd963f384"}


Please don’t reply on this GitHub thread. Visit golang.org/cl/759940.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Sean Liao:

Patch Set 3: -Commit-Queue

(Performed by <GERRIT_ACCOUNT_60063> on behalf of <GERRIT_ACCOUNT_34788>)


Please don’t reply on this GitHub thread. Visit golang.org/cl/759940.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Go LUCI:

Patch Set 3:

This CL has passed the run


Please don’t reply on this GitHub thread. Visit golang.org/cl/759940.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Go LUCI:

Patch Set 3: LUCI-TryBot-Result+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/759940.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from David Chase:

Patch Set 3: Code-Review+2


Please don’t reply on this GitHub thread. Visit golang.org/cl/759940.
After addressing review feedback, remember to publish your drafts!

gopherbot pushed a commit that referenced this pull request Apr 6, 2026
consumeComment builds the comment string by repeated string
concatenation inside a loop. Each concatenation copies the
entire string built so far, making the function O(n^2) in the
depth of nested comments.

Replace the concatenation with a strings.Builder, which
amortizes allocation by doubling its internal buffer. This
reduces consumeComment from O(n^2) to O(n).

This is the same bug class as the consumeDomainLiteral fix
in CVE-2025-61725.

Benchmark results (benchstat, 8 runs):

  name                        old time/op  new time/op  delta
  ConsumeComment/depth10      2.481us      1.838us      -25.92%
  ConsumeComment/depth100     86.58us      6.498us      -92.50%
  ConsumeComment/depth1000    7.963ms      52.82us      -99.34%
  ConsumeComment/depth10000   897.8ms      521.3us      -99.94%

The quadratic cost becomes visible at depth 100 and dominant
by depth 1000. At depth 10000, the fix is roughly 1700x
faster.

Change-Id: I3c927f02646fcab7bab167cb82fd46d3327d6d34
GitHub-Last-Rev: 7742dad
GitHub-Pull-Request: #78393
Reviewed-on: https://go-review.googlesource.com/c/go/+/759940
Reviewed-by: Sean Liao <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Auto-Submit: Sean Liao <[email protected]>
Reviewed-by: David Chase <[email protected]>
Reviewed-by: Junyang Shao <[email protected]>
@gopherbot
Copy link
Copy Markdown
Contributor

This PR is being closed because golang.org/cl/759940 has been merged.

@gopherbot gopherbot closed this Apr 6, 2026
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