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

When maxLength is 1 or 2, maxLength - 3 is negative, and Array.prototype.slice treats negative end indices as offsets from the array end (unlike String.prototype.substring, which clamped them to 0). The result keeps almost all of the original code points and appends '...', producing output far longer than maxLength and even longer than the input string.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 7e41029. Configure here.

}

/**
Expand Down
Loading