diff options
author | Taylor R Campbell <campbell+dnsmasq@mumble.net> | 2023-02-25 15:00:30 +0000 |
---|---|---|
committer | Simon Kelley <simon@thekelleys.org.uk> | 2023-02-27 14:56:25 +0000 |
commit | 137ae2e9cf0dc3596641e7c8b91d15307a35319e (patch) | |
tree | 7b305e92ae84a16ba435053c974de338a36e93cf /man/dnsmasq.8 | |
parent | 5dc14b6e05f39a5ab0dc02e376b1d7da2fda5bc1 (diff) | |
download | dnsmasq-137ae2e9cf0dc3596641e7c8b91d15307a35319e.tar.gz |
Avoid undefined behaviour with the ctype(3) functions.
As defined in the C standard:
In all cases the argument is an int, the value of which shall
be representable as an unsigned char or shall equal the value
of the macro EOF. If the argument has any other value, the
behavior is undefined.
This is because they're designed to work with the int values returned
by getc or fgetc; they need extra work to handle a char value.
If EOF is -1 (as it almost always is), with 8-bit bytes, the allowed
inputs to the ctype(3) functions are:
{-1, 0, 1, 2, 3, ..., 255}.
However, on platforms where char is signed, such as x86 with the
usual ABI, code like
char *arg = ...;
... isspace(*arg) ...
may pass in values in the range:
{-128, -127, -126, ..., -2, -1, 0, 1, ..., 127}.
This has two problems:
1. Inputs in the set {-128, -127, -126, ..., -2} are forbidden.
2. The non-EOF byte 0xff is conflated with the value EOF = -1, so
even though the input is not forbidden, it may give the wrong
answer.
Casting char to int first before passing the result to ctype(3)
doesn't help: inputs like -128 are unchanged by this cast. It is
necessary to cast char inputs to unsigned char first; you can then
cast to int if you like but there's no need because the functions
will always convert the argument to int by definition. So the above
fragment needs to be:
char *arg = ...;
... isspace((unsigned char)*arg) ...
This patch inserts unsigned char casts where necessary, and changes
int casts to unsigned char casts where the input is char.
I left alone int casts where the input is unsigned char already --
they're not immediately harmful, although they would have the effect
of suppressing some compiler warnings if the input is ever changed to
be char instead of unsigned char, so it might be better to remove
those casts too.
I also left alone calls where the input is int to begin with because
it came from getc; casting to unsigned char here would be wrong, of
course.
Diffstat (limited to 'man/dnsmasq.8')
0 files changed, 0 insertions, 0 deletions