You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/language/learn-ql/cpp/guards.rst
+31-4Lines changed: 31 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,10 +3,10 @@ Using the guards library in C and C++
3
3
4
4
Overview
5
5
--------
6
-
The guards library (defined in ``semmle.code.cpp.controlflow.Guards``) provides a class ``GuardCondition`` representing Boolean values that are used to make control flow decisions.
6
+
The guards library (defined in ``semmle.code.cpp.controlflow.Guards``) provides a class `GuardCondition<https://help.semmle.com/qldoc/cpp/semmle/code/cpp/controlflow/Guards.qll/type.Guards$GuardCondition.html>`__ representing Boolean values that are used to make control flow decisions.
7
7
A ``GuardCondition`` is considered to guard a basic block if the block can only be reached if the ``GuardCondition`` is evaluated a certain way. For instance, in the following code, ``x < 10`` is a ``GuardCondition``, and it guards all the code before the return statement.
8
8
9
-
.. code:: cpp
9
+
.. code-block:: cpp
10
10
11
11
if(x < 10) {
12
12
f(x);
@@ -24,7 +24,7 @@ The ``controls`` predicate helps determine which blocks are only run when the ``
24
24
25
25
In the following code sample, the call to ``isValid`` controls the calls to ``performAction`` and ``logFailure`` but not the return statement.
26
26
27
-
.. code:: cpp
27
+
.. code-block:: cpp
28
28
29
29
if(isValid(accessToken)) {
30
30
performAction();
@@ -35,22 +35,49 @@ In the following code sample, the call to ``isValid`` controls the calls to ``pe
35
35
}
36
36
return succeeded;
37
37
38
+
In the following code sample, the call to `isValid` controls the body of the if and also the code after the if.
39
+
40
+
.. code-block:: cpp
41
+
42
+
if(!isValid(accessToken)) {
43
+
logFailure();
44
+
return 0;
45
+
}
46
+
performAction();
47
+
return succeeded;
48
+
38
49
The ``ensuresEq`` and ``ensuresLt`` predicates
39
50
----------------------------------------------
40
51
The ``ensuresEq`` and ``ensuresLt`` predicates are the main way of determining what, if any, guarantees the ``GuardCondition`` provides for a given basic block.
41
52
53
+
The ``ensuresEq`` predicate
54
+
***************************
42
55
When ``ensuresEq(left, right, k, block, true)`` holds, then ``block`` is only executed if ``left`` was equal to ``right + k`` at their last evaluation. When ``ensuresEq(left, right, k, block, false)`` holds, then ``block`` is only executed if ``left`` was not equal to ``right + k`` at their last evaluation.
43
56
57
+
The ``ensuresLt`` predicate
58
+
***************************
44
59
When ``ensuresLt(left, right, k, block, true)`` holds, then ``block`` is only executed if ``left`` was strictly less than ``right + k`` at their last evaluation. When ``ensuresLt(left, right, k, block, false)`` holds, then ``block`` is only executed if ``left`` was greater than or equal to ``right + k`` at their last evaluation.
45
60
46
-
.. TODO: examples for these predicates (none for others?)
61
+
In the following code sample, the comparison on the first line ensures that ``index`` is less than ``size`` in the then block, and that ``index`` is greater than or equal to ``size`` in the else block.
62
+
63
+
.. code-block:: cpp
47
64
65
+
if(index < size) {
66
+
ret = array[index];
67
+
} else {
68
+
ret = nullptr
69
+
}
70
+
return ret;
48
71
49
72
The ``comparesEq`` and ``comparesLt`` predicates
50
73
------------------------------------------------
51
74
The ``comparesEq`` and ``comparesLt`` predicates help determine if the ``GuardCondition`` evaluates to true.
52
75
76
+
The ``comparesEq`` predicate
77
+
****************************
53
78
``comparesEq(left, right, k, true, testIsTrue)`` holds if ``left`` equals ``right + k`` when the expression evaluates to ``testIsTrue``.
54
79
80
+
The ``comparesLt`` predicate
81
+
****************************
55
82
``comparesLt(left, right, k, isLessThan, testIsTrue)`` holds if ``left < right + k`` evaluates to ``isLessThan`` when the expression evaluates to ``testIsTrue``.
0 commit comments