pkgconf/tests
Taylor R Campbell 212c85863a Avoid undefined behaviour with the ctype(3) functions.
fix https://github.com/pkgconf/pkgconf/issues/291

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 *ptr = ...;
        ... isspace(*ptr) ...

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 unsigned int first before passing the result to
ctype(3) doesn't help: inputs like -128 are unchanged by this cast,
because (on a two's-complement machine with 32-bit int and unsigned
int), converting the signed char with integer value -128 to unsigned
int gives integer value 2^32 - 128 = 0xffffff80, which is out of
range, and which is converted in int back to -128, which is also out
of range.

It is necessary to cast char inputs to unsigned char first; you can
then cast to unsigned 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 *ptr = ...;
        ... isspace((unsigned char)*ptr) ...

This patch changes unsigned int casts to unsigned char casts, and
adds unsigned char casts where they are missing.
2023-05-02 11:43:56 -07:00
..
lib-relocatable/lib/pkgconfig add support for making paths relocatable (closes #103) 2017-01-20 00:58:48 -06:00
lib1 Avoid undefined behaviour with the ctype(3) functions. 2023-05-02 11:43:56 -07:00
lib2 testsuite: use /test as an explicit prefix on all tests 2015-07-20 03:10:27 -05:00
lib3 testsuite: use /test as an explicit prefix on all tests 2015-07-20 03:10:27 -05:00
Kyuafile.in Add Kyua tests 2016-09-18 23:29:30 +02:00
basic.sh tests: fixup! 2023-01-21 21:43:23 +00:00
builtins.sh main: remove whitespace added for compatibility with older pkg-config (closes #113) 2017-02-07 10:11:12 -06:00
conflicts.sh tests: fixup trailing whitespace in expected results 2023-01-21 20:22:03 +00:00
framework.sh tests: fixup trailing whitespace in expected results 2023-01-21 20:22:03 +00:00
meson.build meson: remove useless command that isn't needed 2023-01-20 22:13:21 +00:00
parser.sh tests: fixup trailing whitespace in expected results 2023-01-21 20:22:03 +00:00
provides.sh tests: fixup trailing whitespace in expected results 2023-01-21 20:22:03 +00:00
regress.sh tests: fixup! 2023-01-21 21:43:23 +00:00
requires.sh tests: fixup! 2023-01-21 21:43:23 +00:00
sysroot.sh tests: fixup trailing whitespace in expected results 2023-01-21 20:22:03 +00:00
test_env.sh.in Don't prepend sysroot_dir if pkg-config file lies outside of sysroot_dir 2021-07-24 20:43:58 -05:00
version.sh Add Kyua tests 2016-09-18 23:29:30 +02:00