Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vstring: Avoid int -> char truncation warnings #3690

Merged
merged 5 commits into from
May 24, 2023

Commits on May 15, 2023

  1. vstring: Avoid int -> char truncation warnings

    Either avoid or silence expected casts from int to char.
    
    I'm not entirely sure whether vStringPut() shouldn't actually take a
    char argument instead of an int, as it will cast it in all cases. It is
    however slightly more convenient as the APIs to retrieve characters
    uses int only for edge cases (e.g. EOF), so the common case is a safe
    cast and it's more convenient not to explicit it everywhere.
    b4n committed May 15, 2023
    Configuration menu
    Copy the full SHA
    0ef46b3 View commit details
    Browse the repository at this point in the history
  2. vstring: Assert that the value passed to vStringPut() is valid

    We expect an `unsigned char` passed as an `int`.  Anything else is
    invalid and probably means there is an issue in the calling code.
    
    Co-Authored-By: Masatake YAMATO <yamato@redhat.com>
    b4n and masatake committed May 15, 2023
    Configuration menu
    Copy the full SHA
    a7802b5 View commit details
    Browse the repository at this point in the history

Commits on May 23, 2023

  1. 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>
    b4n committed May 23, 2023
    Configuration menu
    Copy the full SHA
    e6e018b View commit details
    Browse the repository at this point in the history
  2. Fix unnecessary or incorrect casts in vStringPut() calls

    `vStringPut()` has been made forgiving in the type the character is
    pass as in e6e018b.  It however
    requires the value to be valid in one of the formats it accepts.
    
    This commits should update all problematic cases, basically removing
    all explicit casts:
    
    * removing incorrect `int` to `char` casts;
    * removing incorrect `char` to `int` casts;
    * removing unnecessary `char` to `unsigned char` casts.
    * removing unnecessary `unsigned char` to `int` casts.
    
    Incorrect casts can lead to unexpected results with values > 0x7F.
    Unnecessary casts make the code harder to read than necessary, and
    risk being incorrectly copied to code where they would be incorrect.
    b4n committed May 23, 2023
    Configuration menu
    Copy the full SHA
    8fffa20 View commit details
    Browse the repository at this point in the history
  3. c-based: Fix adding non-ASCII when collecting a signature

    `cppGetc()` can return `CHAR_SYMBOL` and `STRING_SYMBOL` which need to
    be properly handled when trying to represent as character.
    b4n committed May 23, 2023
    Configuration menu
    Copy the full SHA
    1128712 View commit details
    Browse the repository at this point in the history