Skip to content

Commit 111df47

Browse files
authored
Merge pull request #485 from geoffw0/limitedscopefunction
CPP: Fix Limitedscopefunction.ql
2 parents 6d17642 + a51b984 commit 111df47

File tree

6 files changed

+57
-2
lines changed

6 files changed

+57
-2
lines changed

cpp/ql/src/JPL_C/LOC-3/Rule 13/LimitedScopeFunction.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ import cpp
1010

1111
from GlobalVariable v, Function f
1212
where v.getAnAccess().getEnclosingFunction() = f and
13-
strictcount(v.getAnAccess().getEnclosingFunction()) = 1
13+
strictcount(v.getAnAccess().getEnclosingFunction()) = 1 and
14+
forall(VariableAccess a | a = v.getAnAccess() | exists(a.getEnclosingFunction())) and
15+
not v.getADeclarationEntry().getFile() instanceof HeaderFile // intended to be accessed elsewhere
1416
select v, "The variable " + v.getName() + " is only accessed in $@ and should be scoped accordingly.", f, f.getName()

cpp/ql/src/Power of 10/Rule 6/VariableScopeTooLarge.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ import cpp
1010

1111
from GlobalVariable v, Function f
1212
where v.getAnAccess().getEnclosingFunction() = f and
13-
strictcount(v.getAnAccess().getEnclosingFunction()) = 1
13+
strictcount(v.getAnAccess().getEnclosingFunction()) = 1 and
14+
forall(VariableAccess a | a = v.getAnAccess() | exists(a.getEnclosingFunction())) and
15+
not v.getADeclarationEntry().getFile() instanceof HeaderFile // intended to be accessed elsewhere
1416
select v, "The variable " + v.getName() + " is only accessed in $@ and should be scoped accordingly.", f, f.getName()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.c:8:5:8:14 | globalInt4 | The variable globalInt4 is only accessed in $@ and should be scoped accordingly. | test.c:19:6:19:10 | func1 | func1 |
2+
| test.c:9:5:9:14 | globalInt5 | The variable globalInt5 is only accessed in $@ and should be scoped accordingly. | test.c:19:6:19:10 | func1 | func1 |
3+
| test.c:10:5:10:14 | globalInt6 | The variable globalInt6 is only accessed in $@ and should be scoped accordingly. | test.c:19:6:19:10 | func1 | func1 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
JPL_C/LOC-3/Rule 13/LimitedScopeFunction.ql
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// test.c
2+
3+
#include "test.h"
4+
5+
int globalInt1; // GOOD [used in func1, func2]
6+
int globalInt2; // GOOD [used in func1, func2]
7+
int globalInt3; // GOOD [used in func1, func2]
8+
int globalInt4; // BAD [only used in func1]
9+
int globalInt5; // BAD [only used in func1]
10+
int globalInt6; // BAD [only used in func1]
11+
int globalInt7; // GOOD [not used, should be reported by another query]
12+
int globalInt8; // GOOD [used at file level]
13+
int *addrGlobalInt8 = &globalInt8; // GOOD [used in func1, func2]
14+
int globalInt9; // GOOD [used at file level and in func1]
15+
int *addrGlobalInt9 = &globalInt9; // GOOD [used in func1, func2]
16+
17+
int externInt; // GOOD [extern'd so could be part of an interface]
18+
19+
void func1()
20+
{
21+
int *ptr3 = &globalInt3;
22+
int *ptr6 = &globalInt6;
23+
int i8 = *addrGlobalInt8;
24+
25+
globalInt1 = globalInt2;
26+
globalInt4 = globalInt5;
27+
externInt = globalInt9;
28+
}
29+
30+
void func2()
31+
{
32+
int *ptr1 = &globalInt3;
33+
int i8 = *addrGlobalInt8;
34+
35+
globalInt1 = globalInt2;
36+
}
37+
38+
void func3()
39+
{
40+
static int staticInt; // GOOD [declared in local scope]
41+
int i;
42+
43+
i = staticInt;
44+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// test.h
2+
3+
extern int externInt;

0 commit comments

Comments
 (0)