enum values in C are not enum types, they are just numbers. By default
numbers are mapped to signed type, like 'int'.
enum types are represented with usigned types, like 'unsigned int', unless
values of enum contains negative numbers.
If bitwise NOT performed on pure enum value, which is automatically
mapped to 'int', then result is negative number. Result of casting of
negative number to unsigned type depends on architecture and may be
unexpected.
By explicitly casting enum values to enum types, values are converted
to correct representation type, like 'unsgined int'. Result of
bitwise NOT for unsigned type is predictable and does not need to
be casted to the final type.
Yes, casting of enum values to the same enum type looks strange, but this
is the only way to properly handle bitwise NOT for enums in C.
The function was rewritten to use arrays with indexes instead of pointers.
Address Sanitizer produced error because pointers may reach value beyond
allocated range. Despite the fact that such pointers are never dereferenced
comparison of them produced false-positive errors.
As a side effect, readability was improved a bit.
Hi,
gcc10 complains with two warnings when compiling libmicrohttpd using
#define NDEBUG 1
#define DAUTH_SUPPORT 1
so, "release build" with enabled "digest authentication":
../digestauth.c: In function 'MHD_digest_auth_check_digest2':
../digestauth.c:1287:9: warning: 'da.digest_size' may be used uninitialized in this function [-Wmaybe-uninitialized]
1287 | if (da.digest_size != digest_size)
| ~~^~~~~~~~~~~~
../digestauth.c: In function 'MHD_queue_auth_fail_response2':
../digestauth.c:1361:55: warning: 'da.digest_size' may be used uninitialized in this function [-Wmaybe-uninitialized]
1361 | char nonce[NONCE_STD_LEN (VLA_ARRAY_LEN_DIGEST (da.digest_size)) + 1];
|
This is a minor issue, without any practical effect, unless when calling the MHD API with an invalid value for the MHD_DigestAuthAlgorithm enum.
However, gcc is still right that there is a potential code path with undefined behaviour: the default-case in the switch statement in SETUP_DA does not set "da.digest_size".
Two functions later still always read this value. And the "mhd_assert" has no effect, since it's disabled when NDEBUG is set.
Trivial patch attached to silence the compiler warnings by also initializing "da.digest_size" in the default case of the switch statement: