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 index produces longer-than-original output

Medium Severity

When maxLength is 1 or 2, the expression maxLength - 3 becomes negative. Array.prototype.slice interprets a negative second argument as an offset from the end of the array, so it returns most of the array rather than an empty prefix. The resulting "truncated" string can be significantly longer than the input — the opposite of the function's purpose. The old code using substring didn't have this problem because substring clamps negatives to 0.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 20b876a. Configure here.

}

/**
Expand Down
Loading