mirror of
https://github.com/github/codeql.git
synced 2026-05-02 12:15:17 +02:00
[CPP-434] Initial version of query + test cases.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
// Signed-comparison tests
|
||||
|
||||
/* 1. Signed-signed comparison. The semantics are undefined. */
|
||||
bool cannotHoldAnother8(int n1) {
|
||||
// clang 8.0.0 -O2: deleted (silently)
|
||||
// gcc 9.2 -O2: deleted (silently)
|
||||
// msvc 19.22 /O2: not deleted
|
||||
return n1 + 8 < n1; // BAD
|
||||
}
|
||||
bool canHoldPreceding16(int n1) {
|
||||
return n1 - 16 < n1;
|
||||
}
|
||||
|
||||
/* 2. Signed comparison with a narrower unsigned type. The narrower
|
||||
type gets promoted to the (signed) larger type, and so the
|
||||
semantics are undefined. */
|
||||
bool cannotHoldAnotherUShort(int n1, unsigned short delta) {
|
||||
// clang 8.0.0 -O2: deleted (silently)
|
||||
// gcc 9.2 -O2: deleted (silently)
|
||||
// msvc 19.22 /O2: not deleted
|
||||
return n1 + delta < n1; // BAD
|
||||
}
|
||||
bool canHoldPrecedingUShort(int n1, unsigned short delta) {
|
||||
return n1 - delta < n1;
|
||||
}
|
||||
|
||||
/* 3. Signed comparison with a non-narrower unsigned type. The
|
||||
signed type gets promoted to (a possibly wider) unsigned type,
|
||||
and the resulting comparison is unsigned. */
|
||||
bool cannotHoldAnotherUInt(int n1, unsigned int delta) {
|
||||
// clang 8.0.0 -O2: not deleted
|
||||
// gcc 9.2 -O2: not deleted
|
||||
// msvc 19.22 /O2: not deleted
|
||||
return n1 + delta < n1; // GOOD
|
||||
}
|
||||
bool canHoldPrecedingUInt(int n1, unsigned int delta) {
|
||||
return n1 - delta < n1;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Arithmetic/SignedComparisons.ql
|
||||
Reference in New Issue
Block a user