Skip to content

Commit b9d1c38

Browse files
authored
Merge pull request #2371 from max-schaefer/rc/1.23
Merge rc/1.23 into master
2 parents ed4657c + 34f4b11 commit b9d1c38

File tree

16 files changed

+78
-49
lines changed

16 files changed

+78
-49
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This open source repository contains the standard CodeQL libraries and queries t
55
## How do I learn CodeQL and run queries?
66

77
There is [extensive documentation](https://help.semmle.com/QL/learn-ql/) on getting started with writing CodeQL.
8-
You can use the [interactive query console](https://lgtm.com/help/lgtm/using-query-console) on LGTM.com or the [QL for Eclipse](https://lgtm.com/help/lgtm/running-queries-ide) plugin to try out your queries on any open source project that's currently being analyzed.
8+
You can use the [interactive query console](https://lgtm.com/help/lgtm/using-query-console) on LGTM.com or the [CodeQL for Visual Studio Code](https://help.semmle.com/codeql/codeql-for-vscode.html) extension to try out your queries on any open source project that's currently being analyzed.
99

1010
## Contributing
1111

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
bool not_in_range(T *ptr, T *ptr_end, size_t a) {
2-
return ptr + a >= ptr_end || ptr + a < ptr; // BAD
1+
bool not_in_range(T *ptr, T *ptr_end, size_t i) {
2+
return ptr + i >= ptr_end || ptr + i < ptr; // BAD
33
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
bool not_in_range(T *ptr, T *ptr_end, size_t a) {
2-
return a >= ptr_end - ptr; // GOOD
1+
bool not_in_range(T *ptr, T *ptr_end, size_t i) {
2+
return i >= ptr_end - ptr; // GOOD
33
}

cpp/ql/src/Likely Bugs/Memory Management/PointerOverflow.qhelp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,27 @@
44
<qhelp>
55
<overview>
66
<p>
7-
The expression <code>ptr + a &lt; ptr</code> is equivalent to <code>a &lt;
8-
0</code>, and an optimizing compiler is likely to make that replacement,
9-
thereby removing a range check that might have been necessary for security.
10-
If <code>a</code> is known to be non-negative, the compiler can even replace <code>ptr +
11-
a &lt; ptr</code> with <code>false</code>.
7+
When checking for integer overflow, you may often write tests like
8+
<code>p + i &lt; p</code>. This works fine if <code>p</code> and
9+
<code>i</code> are unsigned integers, since any overflow in the addition
10+
will cause the value to simply "wrap around." However, using this pattern when
11+
<code>p</code> is a pointer is problematic because pointer overflow has
12+
undefined behavior according to the C and C++ standards. If the addition
13+
overflows and has an undefined result, the comparison will likewise be
14+
undefined; it may produce an unintended result, or may be deleted entirely by an
15+
optimizing compiler.
1216
</p>
1317

14-
<p>
15-
The reason is that pointer arithmetic overflow in C/C++ is undefined
16-
behavior. The optimizing compiler can assume that the program has no
17-
undefined behavior, which means that adding a positive number to <code>ptr</code> cannot
18-
produce a pointer less than <code>ptr</code>.
19-
</p>
2018
</overview>
2119
<recommendation>
2220
<p>
23-
To check whether an index <code>a</code> is less than the length of an array,
24-
simply compare these two numbers as unsigned integers: <code>a &lt; ARRAY_LENGTH</code>.
21+
To check whether an index <code>i</code> is less than the length of an array,
22+
simply compare these two numbers as unsigned integers: <code>i &lt; ARRAY_LENGTH</code>.
2523
If the length of the array is defined as the difference between two pointers
26-
<code>ptr</code> and <code>p_end</code>, write <code>a &lt; p_end - ptr</code>.
27-
If a is <code>signed</code>, cast it to <code>unsigned</code>
28-
in order to guard against negative <code>a</code>. For example, write
29-
<code>(size_t)a &lt; p_end - ptr</code>.
24+
<code>ptr</code> and <code>p_end</code>, write <code>i &lt; p_end - ptr</code>.
25+
If <code>i</code> is signed, cast it to unsigned
26+
in order to guard against negative <code>i</code>. For example, write
27+
<code>(size_t)i &lt; p_end - ptr</code>.
3028
</p>
3129
</recommendation>
3230
<example>
@@ -43,14 +41,14 @@ overflows and wraps around.
4341
<p>
4442
In both of these checks, the operations are performed in the wrong order.
4543
First, an expression that may cause undefined behavior is evaluated
46-
(<code>ptr + a</code>), and then the result is checked for being in range.
44+
(<code>ptr + i</code>), and then the result is checked for being in range.
4745
But once undefined behavior has happened in the pointer addition, it cannot
4846
be recovered from: it's too late to perform the range check after a possible
4947
pointer overflow.
5048
</p>
5149

5250
<p>
53-
While it's not the subject of this query, the expression <code>ptr + a &lt;
51+
While it's not the subject of this query, the expression <code>ptr + i &lt;
5452
ptr_end</code> is also an invalid range check. It's undefined behavor in
5553
C/C++ to create a pointer that points more than one past the end of an
5654
allocation.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
interface IPerson
4+
{
5+
string Name { get; }
6+
7+
string Greeting
8+
{
9+
get => "Hello";
10+
set { }
11+
}
12+
13+
string Greet(string name) => Greeting + " " + name;
14+
15+
string GreetingString => Greet(Name);
16+
17+
void Greet();
18+
}
19+
20+
class Person : IPerson
21+
{
22+
public string Name => "Petra";
23+
24+
string IPerson.Greeting { get => "Howdy"; set { } }
25+
26+
public void Greet() { }
27+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| DefaultInterfaceMethods.cs:9:9:9:11 | get_Greeting |
2+
| DefaultInterfaceMethods.cs:10:9:10:11 | set_Greeting |
3+
| DefaultInterfaceMethods.cs:13:12:13:16 | Greet |
4+
| DefaultInterfaceMethods.cs:15:30:15:40 | get_GreetingString |

csharp/ql/test/library-tests/csharp8/DefaultInterfaceMethods.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import csharp
22

3-
class DefaultInterfaceMethod extends Method {
3+
class DefaultInterfaceMethod extends Callable {
44
DefaultInterfaceMethod() {
55
this.hasBody() and
66
this.getDeclaringType() instanceof Interface

docs/language/learn-ql/introduction-to-ql.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Introduction to QL
33

44
QL is the powerful query language that underlies CodeQL, which is used to analyze code.
55
Queries written with CodeQL can find errors and uncover variants of important security vulnerabilities.
6-
Visit Semmle's `security research page <https://lgtm.com/security>`__ to read about examples of vulnerabilities that we have recently found in open source projects.
6+
Visit `GitHub Security Lab <https://securitylab.github.com/>`__ to read about examples of vulnerabilities that we have recently found in open source projects.
77

88
Before diving into code analysis with CodeQL, it can be helpful to learn about the underlying language more generally.
99

docs/language/learn-ql/javascript/introduce-libraries-ts.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,13 @@ Ambient nodes are mostly ignored by control flow and data flow analysis. The out
175175
Static type information
176176
-----------------------
177177

178-
.. TODO: Remove link to QL command-line tools below?
178+
Static type information and global name binding is available for projects with "full" TypeScript extraction enabled. This option is enabled by default for projects on LGTM.com and when you create databases with the `CodeQL CLI <https://help.semmle.com/codeql/codeql-cli.html>`__.
179179

180-
Static type information and global name binding is available for projects with "full" TypeScript extraction enabled. This option is enabled by default for projects on LGTM.com. If you are using the `QL command-line tools <https://help.semmle.com/wiki/display/SD/QL+command-line+tools>`__, you must enable it by passing ``--typescript-full`` to the JavaScript extractor. For further information on customizing calls to the extractor, see `Customizing JavaScript extraction <https://help.semmle.com/wiki/display/SD/Customizing+JavaScript+extraction>`__.
180+
.. pull-quote:: Note
181181

182-
**Note:** Without full extraction, the classes and predicates described in this section are empty.
182+
If you are using the `legacy QL command-line tools <https://help.semmle.com/wiki/display/SD/QL+command-line+tools>`__, you must enable full TypeScript extraction by passing ``--typescript-full`` to the JavaScript extractor. For further information on customizing calls to the extractor, see `Customizing JavaScript extraction <https://help.semmle.com/wiki/display/SD/Customizing+JavaScript+extraction>`__.
183+
184+
Without full extraction, the classes and predicates described in this section are empty.
183185

184186
Basic usage
185187
~~~~~~~~~~~

docs/language/learn-ql/writing-queries/introduction-to-queries.rst

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@ Queries are programs written with CodeQL. They are designed to highlight issues
1010
- **Path queries**: queries that describe the flow of information between a source and a sink in your code.
1111
- **Metric queries**: queries that compute statistics for your code.
1212

13-
You can add custom queries to `custom query packs <https://lgtm.com/help/lgtm/about-queries#what-are-query-packs>`__ to analyze your projects in `LGTM <https://lgtm.com>`__, use them to analyze a project using the `command-line tools <https://help.semmle.com/wiki/display/SD/QL+command-line+tools>`__, or you can contribute to the standard CodeQL queries in our `open source repository on GitHub <https://github.com/semmle/ql>`__.
14-
15-
.. TODO: Change "command-line tools" to a link to the CodeQL CLI? Similarly, change "QL for Eclipse".
13+
You can add custom queries to `custom query packs <https://lgtm.com/help/lgtm/about-queries#what-are-query-packs>`__ to analyze your projects in `LGTM <https://lgtm.com>`__, use them to analyze a database with the `CodeQL CLI <https://help.semmle.com/codeql/codeql-cli.html>`__, or you can contribute to the standard CodeQL queries in our `open source repository on GitHub <https://github.com/semmle/ql>`__.
1614

1715
.. pull-quote::
1816

1917
Note
2018

2119
Only the results generated by alert and path queries are displayed on LGTM.
22-
You can display the results generated by metric queries by running them against your project in the `query console on LGTM <https://lgtm.com/query>`__ or in `QL for Eclipse <https://help.semmle.com/ql-for-eclipse/Content/WebHelp/home-page.html>`__.
23-
You can explore the paths generated by path queries `directly in LGTM <https://lgtm.com/help/lgtm/exploring-data-flow-paths>`__ and the `path explorer view <https://help.semmle.com/ql-for-eclipse/Content/WebHelp/path-explorer-view.html>`__ in QL for Eclipse.
20+
You can display the results generated by metric queries by running them against your project in the `query console on LGTM <https://lgtm.com/query>`__ or with the CodeQL `extension for VS Code <https://help.semmle.com/codeql/codeql-for-vscode.html>`__.
21+
You can explore the paths generated by path queries `directly in LGTM <https://lgtm.com/help/lgtm/exploring-data-flow-paths>`__ and in the `Results view <https://help.semmle.com/codeql/codeql-for-vscode/procedures/exploring-paths.html>`__ in VS Code.
2422

2523

2624
This topic is a basic introduction to structuring query files. You can find further information on writing queries for specific programming languages `here <https://help.semmle.com/QL/learn-ql/>`__, and detailed technical information about QL in the `QL language handbook <https://help.semmle.com/QL/ql-handbook/index.html>`__ and the `QL language specification <https://help.semmle.com/QL/ql-spec/language.html>`__.
@@ -54,15 +52,15 @@ Query metadata
5452
Query metadata is used to identify your custom queries when they are added to the GitHub repository or used in your analysis. Metadata provides information about the query's purpose, and also specifies how to interpret and display the query results. For a full list of metadata properties, see the :doc:`query metadata reference <query-metadata>`. The exact metadata requirement depends on how you are going to run your query:
5553

5654
- If you are contributing a query to the GitHub repository, please read the `query metadata style guide <https://github.com/Semmle/ql/blob/master/docs/query-metadata-style-guide.md#metadata-area>`__.
57-
- If you are adding a custom query to a query pack for analysis using LGTM , see `Writing custom queries to include in LGTM analysis <https://lgtm.com/help/lgtm/writing-custom-queries>`__.
58-
- If you are analyzing a project using the `QL command-line tools <https://help.semmle.com/wiki/display/SD/QL+command-line+tools>`__, see `Preparing custom queries <https://help.semmle.com/wiki/display/SD/Preparing+custom+queries>`__.
59-
- If you are running a query in the query console on LGTM or in the Quick query window in QL for Eclipse, metadata is not mandatory. However, if you want your results to be displayed as either an 'alert' or a 'path', you must specify the correct `@kind` property, as explained below. See `Using the query console <https://lgtm.com/help/lgtm/using-query-console>`__ and `Running a quick query <https://help.semmle.com/ql-for-eclipse/Content/WebHelp/run-quick-query.html>`__ for further information.
55+
- If you are adding a custom query to a query pack for analysis using LGTM , see `Writing custom queries to include in LGTM analysis <https://lgtm.com/help/lgtm/writing-custom-queries>`__.
56+
- If you are analyzing a database using the `CodeQL CLI <https://help.semmle.com/codeql/codeql-cli.html>`__, your query metadata must contain ``@kind``.
57+
- If you are running a query in the query console on LGTM or with the CodeQL extension for VS Code, metadata is not mandatory. However, if you want your results to be displayed as either an 'alert' or a 'path', you must specify the correct ``@kind`` property, as explained below. See `Using the query console <https://lgtm.com/help/lgtm/using-query-console>`__ and `Using the extension <https://help.semmle.com/codeql/codeql-for-vscode/procedures/using-extension.html>`__ for further information.
6058

6159
.. pull-quote::
6260

6361
Note
6462

65-
Queries that are contributed to the open source repository, added to a query pack in LGTM, or used to analyze a project with the QL command-line tools must have a query type (``@kind``) specified. The ``@kind`` property indicates how to interpret and display the results of the query analysis:
63+
Queries that are contributed to the open source repository, added to a query pack in LGTM, or used to analyze a database with the `CodeQL CLI <https://help.semmle.com/codeql/codeql-cli.html>`__ must have a query type (``@kind``) specified. The ``@kind`` property indicates how to interpret and display the results of the query analysis:
6664

6765
- Alert query metadata must contain ``@kind problem``.
6866
- Path query metadata must contain ``@kind path-problem``.
@@ -87,7 +85,7 @@ When writing your own alert queries, you would typically import the standard lib
8785

8886
There are also libraries containing commonly used predicates, types, and other modules associated with different analyses, including data flow, control flow, and taint-tracking. In order to calculate path graphs, path queries require you to import a data flow library into the query file. See :doc:`Constructing path queries <path-queries>` for further information.
8987

90-
You can explore the contents of all the standard libraries in the `CodeQL library reference documentation <https://help.semmle.com/QL/ql-libraries.html>`__, using `QL for Eclipse <https://help.semmle.com/ql-for-eclipse/Content/WebHelp/z-queries.html>`__, or in the `GitHub repository <https://github.com/semmle/ql>`__.
88+
You can explore the contents of all the standard libraries in the `CodeQL library reference documentation <https://help.semmle.com/QL/ql-libraries.html>`__ or in the `GitHub repository <https://github.com/semmle/ql>`__.
9189

9290

9391
Optional CodeQL classes and predicates

0 commit comments

Comments
 (0)