Skip to content

truncateWithEllipsis slices backwards and produces long string when passed non-positive maxLength #23194

Open

Description

@prudhvirajkonda8-bot

The problem

In app/src/lib/truncate-with-ellipsis.ts, truncateWithEllipsis truncates strings to a specified maxLength:

export function truncateWithEllipsis(str: string, maxLength: number) {
  if (str.length <= maxLength) {
    return str
  }
  ...
  const result = characters.slice(0, maxLength).join('')
  return `${result}…`
}

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.

For example:

  • truncateWithEllipsis("hello world", -1) produces "hello worl…"
  • truncateWithEllipsis("hello world", -3) produces "hello wo…"

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

  1. Call truncateWithEllipsis("hello world", -1)
  2. Observe output "hello worl…" instead of ""
  3. Call truncateWithEllipsis("hello world", -3)
  4. 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.

Activity

  1. added
    bug
    priority-3
    on Nov 17, 2026
  2. desktop-triage commented on Nov 17, 2026

    @desktop-triage

    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.

    Generated by Issue Triage (skills-driven) for #23194 · auto · 23.6 AIC · ⊞ 4.7K · ◷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpriority-3

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions