You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
truncateWithEllipsis slices backwards and produces long string when passed non-positive maxLength #23194
If maxLength is non-positive or negative (for instance, arising from dynamic container width calculations such as availableWidth - padding - offset when layout space is constrained), characters.slice(0, maxLength) interprets negative values as an offset from the end of the array rather than an absolute length bound.
Instead of bounding the output to maxLength, negative lengths slice backwards from the end of the string and append an ellipsis.
Release version
Development / latest
Operating system
macOS, Windows, Linux
Steps to reproduce the behavior
Call truncateWithEllipsis("hello world", -1)
Observe output "hello worl…" instead of ""
Call truncateWithEllipsis("hello world", -3)
Observe output "hello wo…" instead of ""
Expected behavior
When maxLength <= 0, truncateWithEllipsis should guard against non-positive lengths and return "" (or clamp with Math.max(0, maxLength)), ensuring no string exceeds the requested maximum length.
Triage summary: Requesting bug (HIGH confidence) and priority-3 (MEDIUM confidence). The report demonstrates concrete, reproducible input/output pairs showing Array.prototype.slice semantics for negative indices produce unintended output, and no duplicate issue was found in a search of open/closed issues. This is edge-case/cosmetic (affects text truncation only under rare negative-width layout conditions), so priority-3 rather than a higher priority.
Initial hypothesis:truncateWithEllipsis only guards the non-negative case: str.length <= maxLength and characters.length <= maxLength both return the original string, but neither branch catches maxLength <= 0 when the string is longer than maxLength. Execution then falls through to const result = characters.slice(0, maxLength).join(''), where Array.prototype.slice treats a negative maxLength as an offset from the end of the array rather than a length bound, so slice(0, -1) returns all but the last element instead of an empty array. The appended ellipsis is then added regardless, producing a string longer than intended instead of "". The function's initial guard at line 3 (if (str.length <= maxLength) { return str }) does not trigger for non-positive maxLength when str is non-empty, since str.length is always >= 0 > maxLength in that case. A fix would clamp maxLength to Math.max(0, maxLength) before use, as already proposed in linked PR #23195.
Activity
desktop-triage commentedon Nov 17, 2026
Triage summary: Requesting
bug(HIGH confidence) andpriority-3(MEDIUM confidence). The report demonstrates concrete, reproducible input/output pairs showingArray.prototype.slicesemantics for negative indices produce unintended output, and no duplicate issue was found in a search of open/closed issues. This is edge-case/cosmetic (affects text truncation only under rare negative-width layout conditions), sopriority-3rather than a higher priority.Initial hypothesis:
truncateWithEllipsisonly guards the non-negative case:str.length <= maxLengthandcharacters.length <= maxLengthboth return the original string, but neither branch catchesmaxLength <= 0when the string is longer thanmaxLength. Execution then falls through toconst result = characters.slice(0, maxLength).join(''), whereArray.prototype.slicetreats a negativemaxLengthas an offset from the end of the array rather than a length bound, soslice(0, -1)returns all but the last element instead of an empty array. The appended ellipsis is then added regardless, producing a string longer than intended instead of"". The function's initial guard at line 3 (if (str.length <= maxLength) { return str }) does not trigger for non-positivemaxLengthwhenstris non-empty, sincestr.lengthis always>= 0 > maxLengthin that case. A fix would clampmaxLengthtoMath.max(0, maxLength)before use, as already proposed in linked PR #23195.