Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/cross-platform/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,21 @@ function formatSection(title, content) {
*/

/**
* Truncate text to limit with ellipsis
* Truncate text to limit with ellipsis.
*
* Slices on Unicode code points (not UTF-16 code units) so multi-byte
* chars like emoji never end up as orphan surrogates. Non-positive
* maxLength returns the original string unchanged.
*
* @param {string} text - Text to truncate
* @param {number} maxLength - Maximum length
* @param {number} maxLength - Maximum length (in code points)
* @returns {string} Truncated text
*/
function truncate(text, maxLength) {
if (text.length <= maxLength) return text;
return text.substring(0, maxLength - 3) + '...';
if (maxLength <= 0) return text;
const codePoints = [...text];
if (codePoints.length <= maxLength) return text;
return codePoints.slice(0, maxLength - 3).join('') + '...';
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Negative slice end produces oversized output

Medium Severity

The truncate function doesn't correctly truncate when maxLength is 1 or 2. When maxLength - 3 is negative, Array.prototype.slice treats it as an offset from the end of the array, unlike String.prototype.substring. This returns nearly the entire original string plus an ellipsis, violating the truncation contract.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 01d7166. Configure here.

}

/**
Expand Down
Loading