Skip to content

Trim an oversized device report's comment by bytes - #41

Merged
SorcRR merged 3 commits into
masterfrom
fix/device-report-trim-by-bytes
Oct 12, 2026
Merged

SorcRR merged 3 commits into
masterfrom
fix/device-report-trim-by-bytes

Conversation

@SorcRR

@SorcRR SorcRR commented Oct 3, 2026 •

Copy link
Copy Markdown
Owner

What

DeviceReportOutput.fitted cut the comment with unicodeScalars.dropLast(excess), where excess is a byte count. An emoji is four bytes, so it dropped up to four times what it had to. When the excess was bigger than the number of scalars, the comment was lost entirely. For example, 2000 emoji fitted to 4000 bytes came back with no comment.

Now device and crash reports share one trim, ReportComment.trimmed:

  • It keeps whole characters, so a family emoji is never left half built.
  • It counts each character as the report's encoder writes it. Quotes, backslashes, slashes and newlines are two bytes in the JSON, so a comment full of them is cut by what it really costs. Counting raw bytes would cut twice as much as needed.
  • Only characters JSON may escape go through the encoder. Everything else counts as its UTF-8 size, which keeps it cheap: the report windows fit the report on every keystroke.
  • One pass always fits. The loop in each fitted is only a guard.

Tests

  • ReportCommentTests: emoji cut by their bytes, escaped characters at their JSON size, whole characters only, nothing left is nil.
  • Device report: 2000 emoji fitted to 4000 bytes keep part of the comment and land within one emoji of the limit. A comment of escaped characters fits to the byte.
  • Crash report: the same escaped-character case.
  • swift test: 344 tests, 0 failures. No warnings.

SorcRR added 3 commits October 3, 2026 11:36
fitted dropped one unicode scalar per byte over the limit. An emoji is
four bytes, so it threw away up to four times what it had to, and when
the excess passed the scalar count the whole comment went (2000 emoji
fitted to 4000 bytes came back with no comment at all).

Keep the longest prefix of whole characters whose UTF-8 size fits
instead, so a multi-scalar emoji is never left half built. The loop
still re-checks for JSON escaping.
Device and crash reports had the same comment-trimming loop. It now
lives in `ReportComment.trimmed`, used by both.

It also measures each character as the report's encoder writes it, so a
comment full of quotes, backslashes, slashes or newlines (two bytes each
in the JSON) is cut by what it really costs. Before, those were counted
at one byte and twice as much was cut as needed. Only characters JSON
may escape go through the encoder; the rest count as their UTF-8 size,
which keeps it cheap for the windows that fit the report on every
keystroke. One pass now always fits; the loop is only a guard.
@SorcRR
SorcRR merged commit ffd6e50 into master Oct 12, 2026
1 check passed
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.

1 participant