avoid UB when calling ctype functions#8465
Merged
rustyrussell merged 1 commit intoElementsProject:masterfrom Aug 18, 2025
Merged
avoid UB when calling ctype functions#8465rustyrussell merged 1 commit intoElementsProject:masterfrom
rustyrussell merged 1 commit intoElementsProject:masterfrom
Conversation
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
approved these changes
Aug 18, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 orEOF. 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 achar-typed value and the system'schartype is signed.<ccan/str/str.h>defines some inline utility functions that perform the necessary cast to coerce achar-typed argument into the allowed value range. Call these wrappers instead of the bare ctype functions when classifyingchar-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: