Skip to content

Conversation

@Explorer09
Copy link
Contributor

The strnlen function does not calculate true display widths of the Unicode strings, but at least it works with ASCII strings. The function that can calculate display widths of Unicode strings is not implemented in htop yet (I have #1642, but the implementation of that is incomplete).

The goal of this commit is to prevent potential arithmetic overflows when calculating string widths and to allow a safe downcast from size_t to int type. This fixes some of the-Wshorten-64-to-32 warnings produced by Clang (see #1673) as a result.

@BenBE BenBE added code quality ♻️ Code quality enhancement build system 🔧 Affects the build system rather then the user experience labels May 22, 2025
@Explorer09 Explorer09 force-pushed the string-width branch 2 times, most recently from 0348583 to b73e050 Compare May 27, 2025 19:08
@Explorer09 Explorer09 marked this pull request as ready for review May 27, 2025 23:53
@Explorer09 Explorer09 marked this pull request as draft May 31, 2025 19:50
@Explorer09 Explorer09 force-pushed the string-width branch 2 times, most recently from 43bfd83 to d70cffe Compare June 3, 2025 22:04
@Explorer09 Explorer09 marked this pull request as ready for review June 3, 2025 22:04
@BenBE
Copy link
Member

BenBE commented Jun 4, 2025

When clicking the screen tabs there's a small off-by-one present:

  • Clicking the cell right before the tab selects that tab, while clicking the last cell of a tab does nothing (as expected of the gap between tabs).
  • Clicking the gap selects the tab following the gap.

A similar issue exists when trying to select a column header:

  • Clicking the last cell of a column header inverts and selects the column following it.
  • Except when you are too far to the left, where it actually selects the right column once. Subsequent clicks select the column right of it.

@Explorer09
Copy link
Contributor Author

Explorer09 commented Jun 4, 2025

When clicking the screen tabs there's a small off-by-one present:

  • Clicking the cell right before the tab selects that tab, while clicking the last cell of a tab does nothing (as expected of the gap between tabs).
  • Clicking the gap selects the tab following the gap.

A similar issue exists when trying to select a column header:

  • Clicking the last cell of a column header inverts and selects the column following it.
  • Except when you are too far to the left, where it actually selects the right column once. Subsequent clicks select the column right of it.

Can't reproduce in my terminal emulator. They work perfectly on my side. Both on macOS Terminal and xfce4-terminal (on Devuan GNU/Linux).

Explorer09 and others added 7 commits June 4, 2025 18:10
This avoids a '-Wshorten-64-to-32' warning produced by Clang.

Signed-off-by: Kang-Che Sung <[email protected]>
Make the code more robust with very small width of the terminal screen.
Also add assertions on x positions in the drawTab() subroutine.

Signed-off-by: Kang-Che Sung <[email protected]>
Add configure check for strnlen() function and provide our own
implementation for systems that doesn't support it.

Co-Authored-By: Benny Baumann <[email protected]>
Signed-off-by: Kang-Che Sung <[email protected]>
Specifically in these functions:
  Action_setScreenTab(), TextMeterMode_draw(), LEDMeterMode_draw()
  RowField_keyAt() and drawTab()

The strnlen() function does not calculate true display widths of the
Unicode strings, but at least it works with ASCII strings. The function
that can calculate display widths of Unicode strings is yet to be
implemented.

The goal of this commit is to prevent potential arithmetic overflows
when calculating string widths and to allow a safe downcast from
'size_t' to 'int' type. This fixes some of the '-Wshorten-64-to-32'
warnings produced by Clang (see issue htop-dev#1673) as a result.

Signed-off-by: Kang-Che Sung <[email protected]>
@BenBE
Copy link
Member

BenBE commented Jun 4, 2025

Merging for now, as this happens for me also with the previous version without this PR applied.

@BenBE BenBE merged commit 6f948d3 into htop-dev:main Jun 4, 2025
19 checks passed
@Explorer09 Explorer09 deleted the string-width branch June 4, 2025 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build system 🔧 Affects the build system rather then the user experience code quality ♻️ Code quality enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants