Skip to content

fix(stringutil): operate on runes instead of bytes in Truncate (#22388)#22468

Merged
johnstcn merged 1 commit intorelease/2.29from
cj/r229/stringutil-fix
Mar 2, 2026
Merged

fix(stringutil): operate on runes instead of bytes in Truncate (#22388)#22468
johnstcn merged 1 commit intorelease/2.29from
cj/r229/stringutil-fix

Conversation

@johnstcn
Copy link
Member

@johnstcn johnstcn commented Mar 2, 2026

Fixes #22375

Updates stringutil.Truncate to properly handle multi-byte UTF-8 characters.
Adds tests for multi-byte truncation with word boundary.

Created by Mux using Opus 4.6

(cherry picked from commit 0cfa037)

Fixes #22375

Updates `stringutil.Truncate` to properly handle multi-byte UTF-8
characters.
Adds tests for multi-byte truncation with word boundary.

Created by Mux using Opus 4.6

(cherry picked from commit 0cfa037)
@johnstcn johnstcn self-assigned this Mar 2, 2026
@johnstcn johnstcn added the cherry-pick/v2.29 Needs to be cherry-picked to the 2.29 release branch label Mar 2, 2026
@johnstcn johnstcn marked this pull request as ready for review March 2, 2026 10:56
@johnstcn johnstcn requested review from Copilot, f0ssel and matifali March 2, 2026 10:56
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates string utilities and task name generation to be UTF-8 safe, preventing invalid UTF-8 from being produced when truncating multi-byte characters (fixing the reported task creation failure).

Changes:

  • Update stringutil.Truncate to truncate by runes (not bytes) and keep full-word truncation rune-safe.
  • Add multi-byte truncation test cases (including full-word + ellipsis behavior).
  • Add stringutil.Capitalize and use it in task display name generation to avoid byte-slicing.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
coderd/util/strings/strings.go Make Truncate rune-aware; add Capitalize helper for rune-safe first-letter uppercasing.
coderd/util/strings/strings_test.go Add multi-byte truncation coverage and unit tests for Capitalize.
coderd/taskname/taskname.go Use strutil.Capitalize when finalizing display names.
coderd/taskname/taskname_test.go Add regression test ensuring multi-byte prompt display name is not corrupted.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +67 to 69
runes := []rune(s)
if len(runes) <= n {
return s
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

Truncate converts the entire input to []rune up-front, which allocates and walks the whole string even when n is small (common for call sites like task name/display name generation). Consider a rune-safe approach that finds the byte index for the first maxLen runes by iterating with for i := range s (or counting runes until maxLen) so you can slice the original string without allocating a full rune slice.

Copilot uses AI. Check for mistakes.
@johnstcn johnstcn merged commit 72d05f3 into release/2.29 Mar 2, 2026
53 of 55 checks passed
@johnstcn johnstcn deleted the cj/r229/stringutil-fix branch March 2, 2026 11:19
@github-actions github-actions bot locked and limited conversation to collaborators Mar 2, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cherry-pick/v2.29 Needs to be cherry-picked to the 2.29 release branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants