Add explicit casts at narrowing integer conversions - #127
Open
escherstair wants to merge 1 commit into
Open
Conversation
In C, the operands of `<<`, `|`, `*`, `+` and `/` are promoted to `int`, so every one of these expressions has type `int` even when both operands are `uint8_t`/`uint16_t`. Assigning the result back to a narrow type is an implicit narrowing conversion, which compilers report under `-Wconversion` (GCC/Clang), `/W4` (MSVC, C4244) and at the default warning level of the NI LabWindows/CVI 2015 compiler. Every affected value is provably in range, so these casts document the intent rather than change behaviour: * `get_2()`, `get_regs()`, `put_regs()`, `swap_regs()` - byte swaps of a `uint16_t`; the result cannot exceed `0xFFFF`. * `nmbs_write_multiple_coils()` - `quantity <= 0x07B0`, so `(quantity + 7) / 8 <= 246`. * `nmbs_write_multiple_registers()` - `quantity <= 0x007B`, so `quantity * 2 <= 246`. * `nmbs_write_file_record()` - `count <= 122`, so `7 + count * 2 <= 251`. * `nmbs_read_write_registers()` - `write_quantity <= 0x0079`, so `write_quantity * 2 <= 242`. Each bound is already enforced by the argument validation at the top of the corresponding function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In C, the operands of
<<,|,*,+and/are promoted toint, so every one of these expressions has typeinteven when both operands areuint8_t/uint16_t. Assigning the result back to a narrow type is an implicit narrowing conversion, which compilers report under-Wconversion(GCC/Clang),/W4(MSVC, C4244).Every affected value is provably in range, so these casts document the intent rather than change behaviour:
get_2(),get_regs(),put_regs(),swap_regs()- byte swaps of auint16_t; the result cannot exceed0xFFFF.nmbs_write_multiple_coils()-quantity <= 0x07B0, so(quantity + 7) / 8 <= 246.nmbs_write_multiple_registers()-quantity <= 0x007B, soquantity * 2 <= 246.nmbs_write_file_record()-count <= 122, so7 + count * 2 <= 251.nmbs_read_write_registers()-write_quantity <= 0x0079, sowrite_quantity * 2 <= 242.Each bound is already enforced by the argument validation at the top of the corresponding function.