Skip to content

Commit 2b37c6c

Browse files
authored
Merge pull request github#17548 from hvitved/shared/inline-test-post-process
Shared: Post-processing query for inline test expectations
2 parents 977eb05 + 495c92d commit 2b37c6c

File tree

72 files changed

+1651
-717
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1651
-717
lines changed

cpp/ql/test/TestUtilities/InlineExpectationsTest.qll

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,5 @@
55

66
import cpp as C
77
private import codeql.util.test.InlineExpectationsTest
8-
9-
private module Impl implements InlineExpectationsTestSig {
10-
private newtype TExpectationComment = MkExpectationComment(C::CppStyleComment c)
11-
12-
/**
13-
* A class representing a line comment in the CPP style.
14-
* Unlike the `CppStyleComment` class, however, the string returned by `getContents` does _not_
15-
* include the preceding comment marker (`//`).
16-
*/
17-
class ExpectationComment extends TExpectationComment {
18-
C::CppStyleComment comment;
19-
20-
ExpectationComment() { this = MkExpectationComment(comment) }
21-
22-
/** Returns the contents of the given comment, _without_ the preceding comment marker (`//`). */
23-
string getContents() { result = comment.getContents().suffix(2) }
24-
25-
/** Gets a textual representation of this element. */
26-
string toString() { result = comment.toString() }
27-
28-
/** Gets the location of this comment. */
29-
Location getLocation() { result = comment.getLocation() }
30-
}
31-
32-
class Location = C::Location;
33-
}
34-
8+
private import internal.InlineExpectationsTestImpl
359
import Make<Impl>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @kind test-postprocess
3+
*/
4+
5+
private import cpp
6+
private import codeql.util.test.InlineExpectationsTest as T
7+
private import internal.InlineExpectationsTestImpl
8+
import T::TestPostProcessing
9+
import T::TestPostProcessing::Make<Impl, Input>
10+
11+
private module Input implements T::TestPostProcessing::InputSig<Impl> {
12+
string getRelativeUrl(Location location) {
13+
exists(File f, int startline, int startcolumn, int endline, int endcolumn |
14+
location.hasLocationInfo(_, startline, startcolumn, endline, endcolumn) and
15+
f = location.getFile()
16+
|
17+
result =
18+
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
19+
)
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import cpp as C
2+
private import codeql.util.test.InlineExpectationsTest
3+
4+
module Impl implements InlineExpectationsTestSig {
5+
private newtype TExpectationComment = MkExpectationComment(C::CppStyleComment c)
6+
7+
/**
8+
* A class representing a line comment in the CPP style.
9+
* Unlike the `CppStyleComment` class, however, the string returned by `getContents` does _not_
10+
* include the preceding comment marker (`//`).
11+
*/
12+
class ExpectationComment extends TExpectationComment {
13+
C::CppStyleComment comment;
14+
15+
ExpectationComment() { this = MkExpectationComment(comment) }
16+
17+
/** Returns the contents of the given comment, _without_ the preceding comment marker (`//`). */
18+
string getContents() { result = comment.getContents().suffix(2) }
19+
20+
/** Gets a textual representation of this element. */
21+
string toString() { result = comment.toString() }
22+
23+
/** Gets the location of this comment. */
24+
Location getLocation() { result = comment.getLocation() }
25+
}
26+
27+
class Location = C::Location;
28+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Critical/SizeCheck.ql
1+
query: Critical/SizeCheck.ql
2+
postprocess: TestUtilities/InlineExpectationsTestQuery.ql

cpp/ql/test/query-tests/Critical/SizeCheck/test.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ void free(void *ptr);
1313

1414
void bad0(void) {
1515

16-
float *fptr = malloc(3); // BAD -- Too small
17-
double *dptr = malloc(5); // BAD -- Too small
16+
float *fptr = malloc(3); // $ Alert -- Too small
17+
double *dptr = malloc(5); // $ Alert -- Too small
1818
free(fptr);
1919
free(dptr);
2020
}
@@ -29,8 +29,8 @@ void good0(void) {
2929

3030
void bad1(void) {
3131

32-
float *fptr = malloc(sizeof(short)); // BAD -- Too small
33-
double *dptr = malloc(sizeof(float)); // BAD -- Too small
32+
float *fptr = malloc(sizeof(short)); // $ Alert -- Too small
33+
double *dptr = malloc(sizeof(float)); // $ Alert -- Too small
3434
free(fptr);
3535
free(dptr);
3636
}
@@ -56,7 +56,7 @@ typedef union _myUnion
5656

5757
void test_union() {
5858
MyUnion *a = malloc(sizeof(MyUnion)); // GOOD
59-
MyUnion *b = malloc(sizeof(MyStruct)); // BAD (too small)
59+
MyUnion *b = malloc(sizeof(MyStruct)); // $ Alert (too small)
6060
}
6161

6262
// --- custom allocators ---
@@ -66,6 +66,6 @@ void *MyMalloc2(size_t size);
6666

6767
void customAllocatorTests()
6868
{
69-
float *fptr1 = MyMalloc1(3); // BAD (too small) [NOT DETECTED]
70-
float *fptr2 = MyMalloc2(3); // BAD (too small) [NOT DETECTED]
69+
float *fptr1 = MyMalloc1(3); // $ MISSING: BAD (too small)
70+
float *fptr2 = MyMalloc2(3); // $ MISSING: BAD (too small)
7171
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Security/CWE/CWE-022/TaintedPath.ql
1+
query: Security/CWE/CWE-022/TaintedPath.ql
2+
postprocess: TestUtilities/InlineExpectationsTestQuery.ql

cpp/ql/test/query-tests/Security/CWE/CWE-022/semmle/tests/test.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#define PATH_MAX 4096
66
///// Test code /////
77

8-
int main(int argc, char** argv) {
8+
int main(int argc, char** argv) { // $ Source=argv
99
char *userAndFile = argv[2];
1010

1111
{
@@ -14,7 +14,7 @@ int main(int argc, char** argv) {
1414
size_t len = strlen(fileName);
1515
strncat(fileName+len, userAndFile, FILENAME_MAX-len-1);
1616
// BAD: a string from the user is used in a filename
17-
fopen(fileName, "wb+");
17+
fopen(fileName, "wb+"); // $ Alert=argv
1818
}
1919

2020
{
@@ -29,30 +29,30 @@ int main(int argc, char** argv) {
2929

3030
{
3131
char *fileName = argv[1];
32-
fopen(fileName, "wb+"); // BAD
32+
fopen(fileName, "wb+"); // $ Alert=argv
3333
}
3434

3535
{
3636
char fileName[20];
37-
scanf("%s", fileName);
38-
fopen(fileName, "wb+"); // BAD
37+
scanf("%s", fileName); // $ Source=scanf_output1
38+
fopen(fileName, "wb+"); // $ Alert=scanf_output1
3939
}
4040

4141
{
4242
char *fileName = (char*)malloc(20 * sizeof(char));
43-
scanf("%s", fileName);
44-
fopen(fileName, "wb+"); // BAD
43+
scanf("%s", fileName); // $ Source=scanf_output2
44+
fopen(fileName, "wb+"); // $ Alert=scanf_output2
4545
}
4646

4747
{
48-
char *tainted = getenv("A_STRING");
49-
fopen(tainted, "wb+"); // BAD
48+
char *tainted = getenv("A_STRING"); // $ Source=getenv1
49+
fopen(tainted, "wb+"); // $ Alert=getenv1
5050
}
5151

5252
{
5353
char buffer[1024];
54-
strncpy(buffer, getenv("A_STRING"), 1024);
55-
fopen(buffer, "wb+"); // BAD
54+
strncpy(buffer, getenv("A_STRING"), 1024); // $ Source=getenv2
55+
fopen(buffer, "wb+"); // $ Alert=getenv2
5656
fopen(buffer, "wb+"); // (we don't want a duplicate result here)
5757
}
5858

@@ -66,22 +66,22 @@ int main(int argc, char** argv) {
6666

6767
{
6868
void readFile(const char *fileName);
69-
readFile(argv[1]); // BAD
69+
readFile(argv[1]); // $ Alert=argv
7070
}
7171

7272
{
7373
char buffer[1024];
74-
read(0, buffer, 1024);
75-
read(0, buffer, 1024);
76-
fopen(buffer, "wb+"); // BAD [duplicated with both sources]
74+
read(0, buffer, 1024); // $ Source=read_output1
75+
read(0, buffer, 1024); // $ Source=read_output2
76+
fopen(buffer, "wb+"); // $ SPURIOUS: Alert=read_output1 $ Alert=read_output2 [duplicated with both sources]
7777
}
7878

7979
{
8080
char *userAndFile = argv[2];
8181
char fileBuffer[PATH_MAX];
8282
snprintf(fileBuffer, sizeof(fileBuffer), "/home/%s", userAndFile);
8383
// BAD: a string from the user is used in a filename
84-
fopen(fileBuffer, "wb+");
84+
fopen(fileBuffer, "wb+"); // $ Alert=argv
8585
}
8686

8787
{
@@ -95,7 +95,7 @@ int main(int argc, char** argv) {
9595
char fileBuffer[PATH_MAX];
9696
snprintf(fileBuffer, sizeof(fileBuffer), "/home/user/files/%s", fileName);
9797
// GOOD: We know that the filename is safe and stays within the public folder. But we currently get an FP here.
98-
FILE *file = fopen(fileBuffer, "wb+");
98+
FILE *file = fopen(fileBuffer, "wb+"); // $ SPURIOUS: Alert=argv
9999
}
100100

101101
{

csharp/ql/test/TestUtilities/InlineExpectationsTest.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Inline expectation tests for CSharp.
2+
* Inline expectation tests for C#.
33
* See `shared/util/codeql/util/test/InlineExpectationsTest.qll`
44
*/
55

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @kind test-postprocess
3+
*/
4+
5+
private import csharp
6+
private import codeql.util.test.InlineExpectationsTest as T
7+
private import internal.InlineExpectationsTestImpl
8+
import T::TestPostProcessing
9+
import T::TestPostProcessing::Make<Impl, Input>
10+
11+
private module Input implements T::TestPostProcessing::InputSig<Impl> {
12+
string getRelativeUrl(Location location) {
13+
exists(File f, int startline, int startcolumn, int endline, int endcolumn |
14+
location.hasLocationInfo(_, startline, startcolumn, endline, endcolumn) and
15+
f = location.getFile()
16+
|
17+
result =
18+
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
19+
)
20+
}
21+
}

csharp/ql/test/TestUtilities/PrettyPrintModels.ql

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,3 @@
55
import semmle.code.csharp.dataflow.internal.ExternalFlow
66
import codeql.dataflow.test.ProvenancePathGraph
77
import codeql.dataflow.test.ProvenancePathGraph::TestPostProcessing::TranslateProvenanceResults<interpretModelForTest/2>
8-
9-
from string relation, int row, int column, string data
10-
where results(relation, row, column, data)
11-
select relation, row, column, data

0 commit comments

Comments
 (0)