Skip to content

Screens/ScreenTabs: do not pass event codes to isalpha() - #2116

Merged
BenBE merged 1 commit into
htop-dev:mainfrom
prownd:screens-ScreenTabs-do-not-pass-event-codes-to-isalpha-s01
Sep 20, 2026
Merged

BenBE merged 1 commit into
htop-dev:mainfrom
prownd:screens-ScreenTabs-do-not-pass-event-codes-to-isalpha-s01

Conversation

@prownd

@prownd prownd commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

The select-by-typing fallback in ScreensPanel_eventHandlerNormal(), ScreenTabsPanel_eventHandler() and ScreenNamesPanel_eventHandlerNormal() guards only the upper bound:

if (ch < 255 && isalpha(ch))

but the value reaching a panel event handler is not always a character. Panel.h defines EVENT_SET_SELECTED (-1), EVENT_PANEL_LOST_FOCUS (-2), EVENT_HEADER_CLICK(x) (-10000 + x) and EVENT_SCREEN_TAB_CLICK(x) (-20000 + x), and ScreenManager_run() dispatches all of them to the focused panel; ERR (-1) can arrive as well. Only some of those are matched by an explicit case, the rest fall through to the default branch.

C11 7.4p1 requires the argument of the <ctype.h> functions to be representable as an unsigned char or equal EOF, so this indexes glibc's ctype table far out of bounds. It is not merely theoretical - a standalone reproduction of the same expression:

ch=-2        table index -2        isalpha()=0
ch=-10000    table index -10000    isalpha()=0
ch=-19997    table index -19997    isalpha()=1

the last one making htop call Panel_selectByTyping() with -19997.

Use the same guard the other panels already use:

if (0 < ch && ch < 255 && isalpha((unsigned char)ch))

The select-by-typing fallback in ScreensPanel_eventHandlerNormal(),
ScreenTabsPanel_eventHandler() and ScreenNamesPanel_eventHandlerNormal()
guards only the upper bound:

    if (ch < 255 && isalpha(ch))

but the value reaching a panel event handler is not always a character.
Panel.h defines EVENT_SET_SELECTED (-1), EVENT_PANEL_LOST_FOCUS (-2),
EVENT_HEADER_CLICK(x) (-10000 + x) and EVENT_SCREEN_TAB_CLICK(x)
(-20000 + x), and ScreenManager_run() dispatches all of them to the
focused panel; ERR (-1) can arrive as well.  Only some of those are
matched by an explicit case, the rest fall through to the default
branch.

C11 7.4p1 requires the argument of the <ctype.h> functions to be
representable as an unsigned char or equal EOF, so this indexes glibc's
ctype table far out of bounds.  It is not merely theoretical - a
standalone reproduction of the same expression:

    ch=-2        table index -2        isalpha()=0
    ch=-10000    table index -10000    isalpha()=0
    ch=-19997    table index -19997    isalpha()=1

the last one making htop call Panel_selectByTyping() with -19997.

Use the same guard the other panels already use:

    if (0 < ch && ch < 255 && isalpha((unsigned char)ch))

Assisted-by: Claude Opus 5
Signed-off-by: hanjinpeng <hanjinpeng@kylinos.cn>
@coderabbitai

coderabbitai Bot commented Sep 20, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 38905c6d-43b3-435c-8cae-505e77254006

📥 Commits

Reviewing files that changed from the base of the PR and between 763e498 and 0003295.

📒 Files selected for processing (2)
  • ScreenTabsPanel.c
  • ScreensPanel.c

Included review availability: Your plan provides up to 8 included reviews per hour; 5 remain after this review.


📝 Walkthrough

Walkthrough

The tab and screen name handlers now call Panel_selectByTyping only for positive alphabetic characters below 255. Control and non-letter characters no longer trigger type-ahead selection. ScreensPanel_eventHandlerNormal now casts the character to unsigned char before calling isalpha.

Change Result
Type-ahead guards Accept alphabetic characters only
Character classification Uses an unsigned char argument

Priority: ⬇️ Low

Change: Bug fix


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Letters pass through the panel gate
Control keys now wait
Type-ahead follows a clearer path
Unsigned characters avoid mishap
Three small checks keep input straight

Comment @coderabbitai help to get the list of available commands.

@BenBE BenBE added this to the 3.6.0 milestone Sep 20, 2026
@BenBE BenBE added the code quality ♻️ Code quality enhancement label Sep 20, 2026

@BenBE BenBE left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.

@BenBE
BenBE merged commit ef9d177 into htop-dev:main Sep 20, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code quality ♻️ Code quality enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants