Skip to content

gh-152275: Add integer overflow guards to the curses chtype and color-pair packing path#152303

Merged
serhiy-storchaka merged 5 commits into
python:mainfrom
serhiy-storchaka:curses-chtype-overflow
Jun 27, 2026
Merged

gh-152275: Add integer overflow guards to the curses chtype and color-pair packing path#152303
serhiy-storchaka merged 5 commits into
python:mainfrom
serhiy-storchaka:curses-chtype-overflow

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

Some curses methods silently truncate a color pair or attributes that do not fit in a chtype, turning a range error into a wrong-rendering bug instead of an exception.

curses.color_pair(n) now raises OverflowError for a pair number too large to be packed, instead of silently masking it to a different pair (the cause of gh-119138). The check verifies that the value round-trips through COLOR_PAIR()/PAIR_NUMBER(), relying only on those macros being inverses, so it is portable and makes no assumption about how the pair is packed.

The attr argument of the character-cell and attribute methods (addch, addstr, addnstr, insch, insstr, insnstr, bkgd, bkgdset, echochar, hline, vline, box, border, attron, attroff and attrset) now goes through the checked attr converter, so an out-of-range or non-integer attribute raises OverflowError/TypeError instead of being silently truncated. This matches the existing pair_converter, component_converter, attr_converter and curses_setcchar, which already reject out-of-range values.

… color-pair packing path

curses.color_pair() now raises OverflowError for a pair number too large
to be packed, instead of silently masking it to a different pair.

The attr argument of the character-cell and attribute methods (addch,
addstr, attron, attrset and others) now goes through the checked attr
converter, so an out-of-range or non-integer attribute is rejected rather
than silently truncated.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

@vstinner vstinner 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. Replacing attr: long with attr: attr in Argument Clinic input is simple and reuse attr_converter(). The added tests check for the changed behavior and are enough.

# Conflicts:
#	Modules/clinic/_cursesmodule.c.h
@read-the-docs-community

read-the-docs-community Bot commented Jun 26, 2026

Copy link
Copy Markdown

For methods where attr is an optional argument (addch(), addstr(),
insch(), ...) the attr converter left the attr_t variable uninitialized
when the argument was omitted, so a garbage value could be applied as
character attributes.  Give the converter c_ignored_default, as the
builtin numeric converters have, so the option-group variable is
initialized to zero.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@serhiy-storchaka serhiy-storchaka force-pushed the curses-chtype-overflow branch from ccdf6a4 to e886f93 Compare June 26, 2026 18:11
@serhiy-storchaka serhiy-storchaka merged commit 7bf63fa into python:main Jun 27, 2026
54 checks passed
@serhiy-storchaka serhiy-storchaka deleted the curses-chtype-overflow branch June 27, 2026 12:42
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