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

signed integers used as bit masks #142

Open
marcpawl opened this issue Jun 29, 2020 · 2 comments
Open

signed integers used as bit masks #142

marcpawl opened this issue Jun 29, 2020 · 2 comments

Comments

@marcpawl
Copy link

Found with clang-tidy hicpp-signed-bitwise with use of SNMP_MSG_GET
Explored with
https://godbolt.org/z/Ank87j
#define u_char unsigned char
#define ASN_CONTEXT ((u_char)0x80)
#define ASN_CONSTRUCTOR ((u_char)0x20)
The expression
ASN_CONTEXT | ASN_CONSTRUCTOR
becomes a signed integer so even if you change
(ASN_CONTEXT | ASN_CONSTRUCTOR | 0x3)
to
(ASN_CONTEXT | ASN_CONSTRUCTOR | 0x03U)
you sill still get the same warning.

@marcpawl
Copy link
Author

A work around for C++ client to not get clang-tidy warning is
// NOLINTNEXTLINE (hicpp-signed-bitwise)
constexpr auto MYSNMP_MSG_GET = SNMP_MSG_GET;

and use the alias in the remainder of your code.

bvanassche added a commit to bvanassche/net-snmp that referenced this issue Jun 29, 2020
The C language requires that the operands of a bitwise or expression
are promoted before the bitwise OR happens. From the C standard:
"The following may be used in an expression wherever an int or unsigned int
may be used:
- An object or expression with an integer type (other than int or unsigned
  int) whose integer conversion rank is less than or equal to the rank of
  int and unsigned int.
- A bit-field of type _Bool , int, signed int, or unsigned int.

If an int can represent all values of the original type (as restricted by
the width, for a bit-field), the value is converted to an int; otherwise,
it is converted to an unsigned int. These are called the integer promotions.
All other types are unchanged by the integer promotions."

In other words, the result of a bitwise OR of two unsigned char operands
has type 'int' instead of unsigned int. Hence change the type of constants
in header files from unsigned char into unsigned int.

See also net-snmp#142.
@bvanassche
Copy link
Contributor

Does commit 31a2c80 fix the hicpp-signed-bitwise warnings?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants