Skip to content

avoid UB when calling ctype functions#8465

Merged
rustyrussell merged 1 commit intoElementsProject:masterfrom
whitslack:ctype-no-ub
Aug 18, 2025
Merged

avoid UB when calling ctype functions#8465
rustyrussell merged 1 commit intoElementsProject:masterfrom
whitslack:ctype-no-ub

Conversation

@whitslack
Copy link
Collaborator

The character classification functions in <ctype.h> are designed to classify characters returned by <stdio.h> getchar() and friends, which return characters as signed integers in the range 0 to 255 or EOF. The behavior of the ctype functions is undefined if they are passed a value outside of that range, which may happen if they are passed a char-typed value and the system's char type is signed.

<ccan/str/str.h> defines some inline utility functions that perform the necessary cast to coerce a char-typed argument into the allowed value range. Call these wrappers instead of the bare ctype functions when classifying char-typed characters.

Changelog-None

Checklist

Before submitting the PR, ensure the following tasks are completed. If an item is not applicable to your PR, please mark it as checked:

  • The changelog has been updated in the relevant commit(s) according to the guidelines.
  • Tests have been added or modified to reflect the changes. Not applicable.
  • Documentation has been reviewed and updated as needed. Not applicable.
  • Related issues have been listed and linked, including any that this PR closes. None found.

The character classification functions in <ctype.h> are designed to
classify characters returned by <stdio.h> getchar() and friends, which
return characters as signed integers in the range 0 to 255 or EOF. The
behavior of the ctype functions is undefined if they are passed a value
outside of that range, which may happen if they are passed a char-typed
value and the system's char type is signed.

<ccan/str/str.h> defines some inline utility functions that perform the
necessary cast to coerce a char-typed argument into the allowed value
range. Call these wrappers instead of the bare ctype functions when
classifying char-typed characters.

Changelog-None
@rustyrussell rustyrussell merged commit 41d31dc into ElementsProject:master Aug 18, 2025
37 of 40 checks passed
@whitslack whitslack deleted the ctype-no-ub branch August 18, 2025 05:14
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