cpp: Incorrect not opeartor usage

Marked as Low precision as Linux kernel code mix the usage of logical operators and bit-wise opeartors.
warning C6317: incorrect operator: logical-not (!) is not interchangeable with ones-complement (~)
This commit is contained in:
Raul Garcia
2018-11-06 12:49:33 -08:00
parent 54493eb990
commit 5a35edfbe2
8 changed files with 279 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
#define FLAGS 0x4004
#define cap_valid(x) ((x) >= 0 && (x) <= 4)
void C6317_positive(int i)
{
if (i & !FLAGS) // BUG
{
}
}
void C6317_negative(int i)
{
if (i & ~FLAGS)
{
}
}
void bitwiseAndUsage(unsigned int l, unsigned int r)
{
unsigned int x;
unsigned z = 0;
x = l & !r; //BUG
x = !FLAGS & r; //BUG
x = !FLAGS & !!r; //BUG
x = !!l & r; // Not a bug - double negation
x = !!!l & r; // Not a bug - double negation
x = !!FLAGS & r; // Not a bug - double negation
x = !FLAGS && r; // Not a bug - logical and
x = !FLAGS && !!r; // Not a bug - logical and
}
void bitwiseOrUsage(unsigned int l, unsigned int r)
{
unsigned int x;
x = l | !r; //BUG
x = !FLAGS | r; //BUG
x = !FLAGS | !!r; //BUG
x = !!l | r; // Not a bug - double negation
x = !!!l | r; // Not a bug - double negation
x = !!FLAGS | r; // Not a bug - double negation
x = !FLAGS || r; // Not a bug - logical or
x = !FLAGS || !!r; // Not a bug - logical or
}
void bitwiseXorUsage(unsigned int l, unsigned int r)
{
unsigned int x;
x = l ^ !r; //BUG
x = !FLAGS ^ r; //BUG
x = !FLAGS ^ !!r; //BUG
x = !!l ^ r; // Not a bug - double negation
x = !!!l ^ r; // Not a bug - double negation
x = !!FLAGS ^ r; // Not a bug - double negation
}
void shoftUsage(unsigned int val)
{
unsigned int x;
x = !val << 1; //BUG
x = !val >> 1; //BUG
x = !!val << 2; // Not a bug - double negation
x = !!val >> 2; // Not a bug - double negation
}
unsigned int bitWiseShiftUsage(unsigned int val)
{
return ((unsigned int)(!!val) << 4) + ((unsigned int)(!!val) >> 1); // Not a bug (double negation)
}
void macroUsage(unsigned int arg1, unsigned int arg2)
{
if (((!cap_valid(arg1)) | arg2)) { // BUG
}
}

View File

@@ -0,0 +1,85 @@
#define FLAGS 0x4004
#define cap_valid(x) ((x) >= 0 && (x) <= 4)
void C6317_positive(int i)
{
if (i & !FLAGS) // BUG
{
}
}
void C6317_negative(int i)
{
if (i & ~FLAGS)
{
}
}
void bitwiseAndUsage(unsigned int l, unsigned int r)
{
unsigned int x;
unsigned z = 0;
x = l & !r; //BUG
x = !FLAGS & r; //BUG
x = !FLAGS & !!r; //BUG
x = !!l & r; // Not a bug - double negation
x = !!!l & r; // Not a bug - double negation
x = !!FLAGS & r; // Not a bug - double negation
x = !FLAGS && r; // Not a bug - logical and
x = !FLAGS && !!r; // Not a bug - logical and
}
void bitwiseOrUsage(unsigned int l, unsigned int r)
{
unsigned int x;
x = l | !r; //BUG
x = !FLAGS | r; //BUG
x = !FLAGS | !!r; //BUG
x = !!l | r; // Not a bug - double negation
x = !!!l | r; // Not a bug - double negation
x = !!FLAGS | r; // Not a bug - double negation
x = !FLAGS || r; // Not a bug - logical or
x = !FLAGS || !!r; // Not a bug - logical or
}
void bitwiseXorUsage(unsigned int l, unsigned int r)
{
unsigned int x;
x = l ^ !r; //BUG
x = !FLAGS ^ r; //BUG
x = !FLAGS ^ !!r; //BUG
x = !!l ^ r; // Not a bug - double negation
x = !!!l ^ r; // Not a bug - double negation
x = !!FLAGS ^ r; // Not a bug - double negation
}
void shoftUsage(unsigned int val)
{
unsigned int x;
x = !val << 1; //BUG
x = !val >> 1; //BUG
x = !!val << 2; // Not a bug - double negation
x = !!val >> 2; // Not a bug - double negation
}
unsigned int bitWiseShiftUsage(unsigned int val)
{
return ((unsigned int)(!!val) << 4) + ((unsigned int)(!!val) >> 1); // Not a bug (double negation)
}
void macroUsage(unsigned int arg1, unsigned int arg2)
{
if (((!cap_valid(arg1)) | arg2)) { // BUG
}
}

View File

@@ -0,0 +1,26 @@
| IncorrectNotOperatorUsage.c:6:9:6:18 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.c:23:9:23:14 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.c:24:9:24:18 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.c:25:9:25:20 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.c:39:9:39:14 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.c:40:9:40:18 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.c:41:9:41:20 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.c:55:9:55:14 | ... ^ ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.c:56:9:56:18 | ... ^ ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.c:57:9:57:20 | ... ^ ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.c:68:9:68:17 | ... << ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.c:69:9:69:17 | ... >> ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.c:82:10:82:34 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.cpp:6:9:6:18 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.cpp:23:9:23:14 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.cpp:24:9:24:18 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.cpp:25:9:25:20 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.cpp:39:9:39:14 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.cpp:40:9:40:18 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.cpp:41:9:41:20 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.cpp:55:9:55:14 | ... ^ ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.cpp:56:9:56:18 | ... ^ ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.cpp:57:9:57:20 | ... ^ ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.cpp:68:9:68:17 | ... << ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.cpp:69:9:69:17 | ... >> ... | Usage of a logical not (!) expression as a bitwise operator. |
| IncorrectNotOperatorUsage.cpp:82:10:82:34 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |

View File

@@ -0,0 +1 @@
Likely Bugs/Likely Typos/IncorrectNotOperatorUsage.ql