Comments (3)
The exists
quantifier
So far, we have declared variables in the from
section of a query clause. Sometimes we need temporary variables in other parts of the query, and don't want to expose them in the query clause. The exists
keyword helps us do this. It is a quantifier: it introduces temporary variables and checks if they satisfy a particular condition.
To understand how exists
works, visit the documentation.
Then look at this example from the “Find the thief” CodeQL tutorial:
from Person t
where exists(string c | t.getHairColor() = c)
select t
This query selects all persons with a hair color that is a string
. So we'll get all persons that are not bald, since we are able to find a c
that defines their hair color. We don't really need c
in the query except to know that it exists.
from codeql-uboot.
⌨️ Activity: Write your own NetworkByteSwap
class
-
We recommend that you first read the documentation on CodeQL classes.
-
Edit the file
9_class_network_byteswap.ql
with the template below:import cpp class NetworkByteSwap extends Expr { NetworkByteSwap () { // TODO: replace <class> and <var> exists(<class> <var> | // TODO: <condition> ) } } from NetworkByteSwap n select n, "Network byte swap"
-
This class
extends Expr
, which means it is a subclass ofExpr
, and it begins by taking all values fromExpr
. Now you need to restrict it to only the expressions we are interested in, which satisfy the condition of step 8.- You can do this by editing the characteristic predicate
NetworkByteSwap() { ... }
. The template includes theexists
quantifier, which will help. - Declare a temporary variable in the
exists
that refers to a macro invocation. - Constrain this macro invocation in the condition section of the
exists
. Use the same logic from thewhere
section of your query in step 8. - How is the macro invocation related to the expression? Use the same logic from the
select
section of your query in step 8. You can refer to the macro invocation using the name of the variable you created, and you can refer to the expression using thethis
variable.
- You can do this by editing the characteristic predicate
-
Once you're happy with the results, submit your solution.
from codeql-uboot.
Congratulations, looks like the query you introduced in 41cf6c0 finds the correct results!
If you created a pull request, merge it.
Let's continue to the next step.
from codeql-uboot.
Related Issues (10)
- Step 1 - Welcome to the course! HOT 1
- Step 2 - Set up your IDE HOT 2
- Step 3 - Your first query HOT 3
- Step 4 - Anatomy of a query HOT 2
- Step 5 - Using different classes and their predicates HOT 2
- Step 6 - Relating two variables HOT 2
- Step 7 - Relating two variables, continued HOT 2
- Step 8 - Changing the selected output HOT 2
- Step 10 - Data flow and taint tracking analysis HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from codeql-uboot.