Skip to content

Cap search query length to prevent crash on huge paste (fixes #1147) - #1394

Open
williamruiz1 wants to merge 1 commit into
p0deje:masterfrom
williamruiz1:fix/cap-search-query-length
Open

Cap search query length to prevent crash on huge paste (fixes #1147)#1394
williamruiz1 wants to merge 1 commit into
p0deje:masterfrom
williamruiz1:fix/cap-search-query-length

Conversation

@williamruiz1

Copy link
Copy Markdown

Summary

When a user accidentally pastes large clipboard contents into Maccy's search field (e.g. cmd+v while the popup is open), regex compilation and Fuse pattern construction can hang or crash the app. Caps the search query at 1,000 chars at the entry of Search.search so all search modes are protected.

Repro

See #1147 (comment)pbcopy < /usr/share/dict/words, open Maccy, cmd+v in the search bar.

Root cause

  • simpleSearch with .regularExpression compiles the entire query as an NSRegularExpression pattern — very slow / can OOM on multi-MB input.
  • fuzzySearch already caps the target string at fuzzySearchLimit = 5_000, but the pattern passed to fuse.createPattern is unbounded.

Fix

One-line guard in Search.search that truncates the query to 1,000 chars before dispatching to any backend. No real user is searching clipboard history with a 1,000+ char pattern, so this is safe.

Test plan

  • Built locally on Xcode 26.4.1, ran with a 1MB blob pasted into the search field — no crash, results render in <100ms.
  • Existing search behavior unchanged for queries under 1,000 chars.

Fixes #1147

When a user accidentally pastes large clipboard contents into the search
field (e.g. cmd+v in Maccy's search bar), regex compilation in
simpleSearch and pattern construction in Fuse can hang or crash the app.

The fuzzy search already caps the *target* string at 5,000 chars, but
the *pattern* (search query) was unbounded. Cap the query at 1,000 chars
at the entry of Search.search so all search modes are protected.

Refs p0deje#1147
Comment thread Maccy/Search.swift
// Cap query length so an accidental cmd+v of huge clipboard contents
// into the search field can't blow up regex compilation or fuzzy matching.
let string = string.count > queryLimit ? String(string.prefix(queryLimit)) : string

@prabhavagrawal7 prabhavagrawal7 May 5, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Instead of capping the query we should avoid searching blobs alltogether.
For example if someone copy a text then image (very large) then text, theoretically because of image fuzzy search won't be able to find text below the image.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Now you're thinking.

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.

Copy pasting inside maccy searching crashes it

2 participants