Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vstring: Make vStringPut*() usage more forgiving
vStringPut() and friends take an `unsigned char` as an `int`, similar to ctype functions, in order to be easy to use with `fgetc()`-style functions. However, this API is also used on regular C strings, which require a cast if the value is larger than 0x7F (127) on systems where `char` is a signed type. In order to make the API easier to use, as it's easy to forget the cast when working with `char`, introduce wrapper macros that add the cast when called with `char`. The cast is conditional so the underlying implementation can still verify the value is in the valid range when called with an `int` (to catch erroneous calls with EOF or other values that do not fit a `char`). Note that this will still not work properly if the caller has an incorrect cast on its side, like e.g. `vStringPut(s, (int) c)` where `c` is a `char`, as there's only so many magic tricks up our sleeves. These calls should be updated to either be `vStringPut(s, c)` with the added macros, or `vStringPut(s, (unsigned char) c)` if one wants to be explicit -- yet no need to go all the trouble to make them `vStringPut(s, (int) (unsigned char) c)`, as the `unsigned char` to `int` conversion is implicit and safe. Based off a suggestion from Masatake YAMATO <yamato@redhat.com>
- Loading branch information