From b14ece72becf72326a2d31c07458d8df432ad706 Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Wed, 11 Feb 2026 16:45:57 +0000 Subject: [PATCH] C++: Add range analysis test demonstrating missing measuring bounds. --- .../SimpleRangeAnalysis/lowerBound.expected | 15 ++++ .../SimpleRangeAnalysis/missing_bounds.cpp | 73 +++++++++++++++++++ .../SimpleRangeAnalysis/nrOfBounds.expected | 30 ++++++++ .../SimpleRangeAnalysis/nrOfBounds.ql | 3 + .../SimpleRangeAnalysis/upperBound.expected | 15 ++++ 5 files changed, 136 insertions(+) create mode 100644 cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/missing_bounds.cpp diff --git a/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/lowerBound.expected b/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/lowerBound.expected index 0c520190c8b..8853777d339 100644 --- a/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/lowerBound.expected +++ b/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/lowerBound.expected @@ -17,6 +17,21 @@ | minmax.c:26:37:26:37 | x | 1 | | minmax.c:26:40:26:40 | y | 2 | | minmax.c:26:43:26:43 | z | 0 | +| missing_bounds.cpp:40:5:40:20 | x | 0 | +| missing_bounds.cpp:40:5:40:20 | x | 0 | +| missing_bounds.cpp:41:5:41:20 | x | 0 | +| missing_bounds.cpp:41:5:41:20 | x | 0 | +| missing_bounds.cpp:42:5:42:20 | x | 0 | +| missing_bounds.cpp:42:5:42:20 | x | 0 | +| missing_bounds.cpp:43:5:43:20 | x | 0 | +| missing_bounds.cpp:43:5:43:20 | x | 0 | +| missing_bounds.cpp:44:5:44:20 | x | 0 | +| missing_bounds.cpp:44:5:44:20 | x | 0 | +| missing_bounds.cpp:45:5:45:20 | x | 0 | +| missing_bounds.cpp:45:5:45:20 | x | 0 | +| missing_bounds.cpp:46:5:46:20 | x | 0 | +| missing_bounds.cpp:46:5:46:20 | x | 0 | +| missing_bounds.cpp:72:12:72:12 | x | 0 | | test.c:8:5:8:9 | count | -2147483648 | | test.c:8:13:8:17 | count | -2147483648 | | test.c:10:10:10:14 | count | -2147483648 | diff --git a/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/missing_bounds.cpp b/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/missing_bounds.cpp new file mode 100644 index 00000000000..cdb332e9387 --- /dev/null +++ b/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/missing_bounds.cpp @@ -0,0 +1,73 @@ +enum MY_ENUM { + A = 0x1, // $ nonFunctionalNrOfBounds + B = 0x2, // $ nonFunctionalNrOfBounds + C = 0x4, // $ nonFunctionalNrOfBounds + D = 0x8, // $ nonFunctionalNrOfBounds + E = 0x10, // $ nonFunctionalNrOfBounds + F = 0x20, // $ nonFunctionalNrOfBounds + G = 0x40, // $ nonFunctionalNrOfBounds + H = 0x80, // $ nonFunctionalNrOfBounds + I = 0x100, // $ nonFunctionalNrOfBounds + J = 0x200, // $ nonFunctionalNrOfBounds + L = 0x400, // $ nonFunctionalNrOfBounds + M = 0x800, // $ nonFunctionalNrOfBounds + N = 0x1000, // $ nonFunctionalNrOfBounds + O = 0x2000, // $ nonFunctionalNrOfBounds + P = 0x4000, // $ nonFunctionalNrOfBounds + Q = 0x8000, // $ nonFunctionalNrOfBounds + R = 0x10000, // $ nonFunctionalNrOfBounds + S = 0x20000, // $ nonFunctionalNrOfBounds + T = 0x40000, // $ nonFunctionalNrOfBounds + U = 0x80000, // $ nonFunctionalNrOfBounds + V = 0x100000, // $ nonFunctionalNrOfBounds + W = 0x200000, // $ nonFunctionalNrOfBounds + X = 0x400000, // $ nonFunctionalNrOfBounds + Y = 0x800000, // $ nonFunctionalNrOfBounds + Z = 0x1000000, // $ nonFunctionalNrOfBounds + AA = 0x2000000, // $ nonFunctionalNrOfBounds + AB = 0x4000000, // $ nonFunctionalNrOfBounds + AC = 0x8000000, // $ nonFunctionalNrOfBounds + AD = 0x10000000, // $ nonFunctionalNrOfBounds + AE = 0x20000000 // $ nonFunctionalNrOfBounds +}; + +typedef unsigned int MY_ENUM_FLAGS; + +MY_ENUM_FLAGS check_and_subs(MY_ENUM_FLAGS x) +{ + + #define CHECK_AND_SUB(flag) if ((x & flag) == flag) { x -= flag; } + CHECK_AND_SUB(A); // $ nonFunctionalNrOfBounds + CHECK_AND_SUB(B); // $ nonFunctionalNrOfBounds + CHECK_AND_SUB(C); // $ nonFunctionalNrOfBounds + CHECK_AND_SUB(D); // $ nonFunctionalNrOfBounds + CHECK_AND_SUB(E); // $ nonFunctionalNrOfBounds + CHECK_AND_SUB(F); // $ nonFunctionalNrOfBounds + CHECK_AND_SUB(G); // $ nonFunctionalNrOfBounds + // CHECK_AND_SUB(H); + // CHECK_AND_SUB(I); + // CHECK_AND_SUB(J); + // CHECK_AND_SUB(L); + // CHECK_AND_SUB(M); + // CHECK_AND_SUB(N); + // CHECK_AND_SUB(O); + // CHECK_AND_SUB(P); + // CHECK_AND_SUB(Q); + // CHECK_AND_SUB(R); + // CHECK_AND_SUB(S); + // CHECK_AND_SUB(T); + // CHECK_AND_SUB(U); + // CHECK_AND_SUB(V); + // CHECK_AND_SUB(W); + // CHECK_AND_SUB(X); + // CHECK_AND_SUB(Y); + // CHECK_AND_SUB(Z); + // CHECK_AND_SUB(AA); + // CHECK_AND_SUB(AB); + // CHECK_AND_SUB(AC); + // CHECK_AND_SUB(AD); + // CHECK_AND_SUB(AE); + #undef CHECK_AND_SUB + + return x; // $ nonFunctionalNrOfBounds +} \ No newline at end of file diff --git a/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/nrOfBounds.expected b/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/nrOfBounds.expected index 7d583e47f5b..98189dd2d07 100644 --- a/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/nrOfBounds.expected +++ b/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/nrOfBounds.expected @@ -37,6 +37,36 @@ estimateNrOfBounds | minmax.c:26:37:26:37 | x | 1.0 | | minmax.c:26:40:26:40 | y | 1.0 | | minmax.c:26:43:26:43 | z | 2.0 | +| missing_bounds.cpp:40:5:40:19 | ... == ... | 1.0 | +| missing_bounds.cpp:40:5:40:20 | x | 1.0 | +| missing_bounds.cpp:40:5:40:20 | x | 1.0 | +| missing_bounds.cpp:40:19:40:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:40:19:40:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:40:19:40:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:41:5:41:19 | ... == ... | 1.0 | +| missing_bounds.cpp:41:19:41:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:41:19:41:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:41:19:41:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:42:5:42:19 | ... == ... | 1.0 | +| missing_bounds.cpp:42:19:42:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:42:19:42:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:42:19:42:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:43:5:43:19 | ... == ... | 1.0 | +| missing_bounds.cpp:43:19:43:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:43:19:43:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:43:19:43:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:44:5:44:19 | ... == ... | 1.0 | +| missing_bounds.cpp:44:19:44:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:44:19:44:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:44:19:44:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:45:5:45:19 | ... == ... | 1.0 | +| missing_bounds.cpp:45:19:45:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:45:19:45:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:45:19:45:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:46:5:46:19 | ... == ... | 1.0 | +| missing_bounds.cpp:46:19:46:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:46:19:46:19 | (unsigned int)... | 1.0 | +| missing_bounds.cpp:46:19:46:19 | (unsigned int)... | 1.0 | | test.c:6:14:6:15 | 0 | 1.0 | | test.c:8:5:8:9 | count | 1.0 | | test.c:8:5:8:19 | ... = ... | 13.0 | diff --git a/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/nrOfBounds.ql b/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/nrOfBounds.ql index efafcf6e684..c39ab92ae71 100644 --- a/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/nrOfBounds.ql +++ b/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/nrOfBounds.ql @@ -12,6 +12,9 @@ query predicate estimateNrOfBounds(Expr e, float nrOfBounds) { */ private predicate nonFunctionalNrOfBounds(Expr e) { strictcount(SimpleRangeAnalysisInternal::estimateNrOfBounds(e)) > 1 + or + e.getFile().getBaseName() = "missing_bounds.cpp" and + count(SimpleRangeAnalysisInternal::estimateNrOfBounds(e)) != 1 } module FunctionalityTest implements TestSig { diff --git a/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/upperBound.expected b/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/upperBound.expected index dd7fc398f0c..7c68f417793 100644 --- a/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/upperBound.expected +++ b/cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/upperBound.expected @@ -17,6 +17,21 @@ | minmax.c:26:37:26:37 | x | 1 | | minmax.c:26:40:26:40 | y | 2 | | minmax.c:26:43:26:43 | z | 1 | +| missing_bounds.cpp:40:5:40:20 | x | 4294967295 | +| missing_bounds.cpp:40:5:40:20 | x | 4294967295 | +| missing_bounds.cpp:41:5:41:20 | x | 4294967295 | +| missing_bounds.cpp:41:5:41:20 | x | 4294967295 | +| missing_bounds.cpp:42:5:42:20 | x | 4294967295 | +| missing_bounds.cpp:42:5:42:20 | x | 4294967295 | +| missing_bounds.cpp:43:5:43:20 | x | 4294967295 | +| missing_bounds.cpp:43:5:43:20 | x | 4294967295 | +| missing_bounds.cpp:44:5:44:20 | x | 4294967295 | +| missing_bounds.cpp:44:5:44:20 | x | 4294967295 | +| missing_bounds.cpp:45:5:45:20 | x | 4294967295 | +| missing_bounds.cpp:45:5:45:20 | x | 4294967295 | +| missing_bounds.cpp:46:5:46:20 | x | 4294967295 | +| missing_bounds.cpp:46:5:46:20 | x | 4294967295 | +| missing_bounds.cpp:72:12:72:12 | x | 4294967295 | | test.c:8:5:8:9 | count | 2147483647 | | test.c:8:13:8:17 | count | 2147483647 | | test.c:10:10:10:14 | count | 2147483647 |