Skip to content

Fix under-allocation when many zero-width characters are present - #1152

Merged
flyingmutant merged 2 commits into
cmus:masterfrom
gavtroy:dev_gbuf_zw
Oct 20, 2022
Merged

flyingmutant merged 2 commits into
cmus:masterfrom
gavtroy:dev_gbuf_zw

Conversation

@gavtroy

@gavtroy gavtroy commented Dec 31, 2021

Copy link
Copy Markdown
Collaborator

Previously we were allocating a "worst case" of 4 per number of printed
characters, which ignores combining characters (for example).

Noticed through a segfault when restarting cmus and immediately playing
the first track of the album discussed in #997.

Other places in the code (ui_curses) also don't account for zero-wide
characters when setting bounds but I'm not sure how to go about fixing
that.

@gavtroy
gavtroy marked this pull request as draft January 2, 2022 20:47
...by taking better advantage of gbufs, and using them only through the
gbuf_* functions (for the most part).

Previously we budgeted for an imagined worst case of 4 per printed
column, which in fact is not the worst case when considering combining
characters (which can be stacked).

However this issue is very rare, almost theoretical, for a few reasons:
- While zero-width characters are not accounted for at all, many other
  characters are significantly over-budgeted, e.g. every Latin character
  is over-accounted by 3 bytes.
- Zero-width characters are relatively scarce, excepting "zalgo text".
- gbufs only grow and are reused for the life of the program
@gavtroy gavtroy changed the title Grow gbufs correctly when encountering zero-width characters Fix under-allocation when many zero-width characters are present Jan 5, 2022
@gavtroy
gavtroy marked this pull request as ready for review January 5, 2022 21:22
@nefthy nefthy mentioned this pull request Jan 10, 2022
@gavtroy

gavtroy commented Jan 10, 2022

Copy link
Copy Markdown
Collaborator Author

This PR is probably oversold by the current headline. To quote the updated commit message:

... this issue is very rare, almost theoretical, for a few reasons:
- While zero-width characters are not accounted for at all, many other
  characters are significantly over-budgeted, e.g. every Latin character
  is over-accounted by 3 bytes.
- Zero-width characters are relatively scarce, excepting "zalgo text".
- gbufs only grow and are reused for the life of the program

Instead this change can be considered refactoring that happens to fix rare crashes with zalgo text. If I had realized how niche the issue is I might not have bothered fixing it, but at least the refactoring ends up improving the oveall readability of the code.

@flyingmutant

Copy link
Copy Markdown
Member

A bit scary to merge a large refactoring like this, but less code is definitely better -- lets do it now and hope that people running master will help catch any possible bugs.

@flyingmutant
flyingmutant merged commit dc2752c into cmus:master Oct 20, 2022
bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this pull request May 27, 2024
https://build.opensuse.org/request/show/1176840
by user mia + anag+factory
- Update to 2.11.0
  * Add support for .nomedia file
    gh#cmus/cmus#1190
  * Fix a typo in an error message
    gh#cmus/cmus#1206
  * Take opus output gain into account when checking
    replaygain_limit peak
    gh#cmus/cmus#1201
  * #1219: Update Unofficial IRC channel
    gh#cmus/cmus#1220
  * Recognize artist/album sort keys from ffmpeg plugin
    gh#cmus/cmus#1165
  * Fix missing combining marks on last character in a field
    gh#cmus/cmus#1151
  * Use wcwidth for u_char_width
    gh#cmus/cmus#1198
  * Fix under-allocation when many zero-width characters are
    present
    gh#cmus/cmus#1152
  * Draw title and command lines on first update
    gh#cmus/cmus#1257
  * Switch to -latest os in build workflow
    gh#cmus/cmus#1265
  * ip/mp4: Use MP4FindTrackId() rather than assuming it's
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.

2 participants