Skip to content

util: fix infinite loop in trim_string_to_width - #1924

Open
jstefani wants to merge 1 commit into
monome:mainfrom
jstefani:norns-1636-jstefani
Open

jstefani wants to merge 1 commit into
monome:mainfrom
jstefani:norns-1636-jstefani

Conversation

@jstefani

@jstefani jstefani commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

fixes #1636

notes: my original fix was to add a check in the while condition. this works for most cases but on review the ai written original branch #1905 handled the edge case of a string that starts with stray UTF-8 continuation bytes, so I changed approach. The local declare inside the loop feels wrong but research seems to indicate this is the Lua way vs what feels "right" to me so I left it.

Thoughts? My goal is to keep this as in line with Lua conventions and Norns codebase conventions as possible.

Tested on hardware via redefining the function in maiden and testing. Also updated file on two norns shields and rebooted but due to various customizations on each unit I'm not sure how valid that test was.

when width was less than "..." the loop never ended.
break out of loop when gsub cannot make progress.

note: avoided empty string test in while condition as this
misses edge case of unmatchable stray UTF-8 continuation bytes.

clarify in ldoc that width is in pixels.

fixes monome#1636
@Dewb

Dewb commented Oct 9, 2026

Copy link
Copy Markdown
Member

This looks good, thanks!

The one thing missing from my comment in #1636 is the first bullet, some handling around a width value that is too small. Neither my comment or the function contract specifies what "too small" is. The earlier PR attempted to include a partial ellipsis if the full ellipsis wouldn't fit, which is interesting but maybe not the desired behavior if the goal is to allow the script author to clearly communicate there was a value there that couldn't fit. We might assume that any width above zero could fit at least one period, but that isn't necessarily true depending on the font face/size.

Here's a proposal for what we could do around values of width smaller than the ellipsis:

  1. Document that the string will be measured in the current selected display font face and size.
  2. Provide an optional argument ellipsis or abbrev that defaults to "..."
  3. At the start of the function, if the measured width of the "ellipsis" argument is smaller than width, return empty string
  4. Document that the function will return empty string if the width is too small for the ellipsis/abbreviation.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

util.trim_string_to_width("asdf", 2) causes maiden to crash

2 participants