Skip to content

Commit 41d31dc

Browse files
whitslackrustyrussell
authored andcommitted
avoid UB when calling ctype functions
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
1 parent 9680404 commit 41d31dc

File tree

4 files changed

+4
-4
lines changed

4 files changed

+4
-4
lines changed

common/json_parse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ bool json_to_millionths(const char *buffer, const jsmntok_t *tok,
3030

3131
*millionths = 0;
3232
for (int i = tok->start; i < tok->end; i++) {
33-
if (isdigit(buffer[i])) {
33+
if (cisdigit(buffer[i])) {
3434
has_digits = true;
3535
/* Ignore too much precision */
3636
if (decimal_places >= 0 && ++decimal_places > 6)

common/splice_script.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ char *fmt_splice_script_compiler_error(const tal_t *ctx,
503503

504504
static bool is_whitespace(char c)
505505
{
506-
return isspace(c);
506+
return cisspace(c);
507507
}
508508

509509
static struct splice_script_error *clean_whitespace(const tal_t *ctx,

db/db_sqlite3.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ static const char *find_column_name(const tal_t *ctx,
418418
{
419419
size_t start = 0;
420420

421-
while (isspace(sqlpart[start]))
421+
while (cisspace(sqlpart[start]))
422422
start++;
423423
*after = strspn(sqlpart + start, "abcdefghijklmnopqrstuvwxyz_0123456789") + start;
424424
if (*after == start || !cisspace(sqlpart[*after]))

devtools/onion.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static void do_decode(int argc, char **argv, const u8 *assocdata)
145145
size_t hexlen = strlen(hextemp);
146146

147147
// trim trailing whitespace
148-
while (isspace(hextemp[hexlen-1]))
148+
while (cisspace(hextemp[hexlen-1]))
149149
hexlen--;
150150

151151
serialized = tal_hexdata(hextemp, hextemp, hexlen);

0 commit comments

Comments
 (0)