Skip to content

Commit 809d1d5

Browse files
committed
remove cases involving sizeof
1 parent 114b468 commit 809d1d5

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

cpp/ql/src/Critical/GlobalUseBeforeInit.ql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,19 @@ predicate dominatingInitInFunc(GlobalVariable v, Function f, ControlFlowNode nod
3131
)
3232
}
3333

34+
predicate safeAccess(VariableAccess access) {
35+
// it is safe if the variable access is part of a `sizeof` expression
36+
exists(SizeofExprOperator e |
37+
e.getAChild*() = access
38+
)
39+
}
40+
3441
predicate useFunc(GlobalVariable v, Function f) {
3542
exists(VariableAccess access |
3643
v.getAnAccess() = access and
3744
access.isRValue() and
3845
access.getEnclosingFunction() = f and
46+
not safeAccess(access) and
3947
not dominatingInitInFunc(v, f, access)
4048
)
4149
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| test.cpp:27:5:27:6 | f1 | The variable $@ is used in this function but may not be initialized when it is called. | test.cpp:14:5:14:5 | b | b |
2-
| test.cpp:38:5:38:8 | main | The variable $@ is used in this function but may not be initialized when it is called. | test.cpp:14:5:14:5 | b | b |
1+
| test.cpp:28:5:28:6 | f1 | The variable $@ is used in this function but may not be initialized when it is called. | test.cpp:14:5:14:5 | b | b |
2+
| test.cpp:39:5:39:8 | main | The variable $@ is used in this function but may not be initialized when it is called. | test.cpp:14:5:14:5 | b | b |

cpp/ql/test/query-tests/Critical/GlobalUseBeforeInit/test.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ int vfprintf (FILE *, const char *, va_list);
1212

1313
int a = 1;
1414
int b;
15+
int *c;
1516

1617
int my_printf(const char * fmt, ...)
1718
{
@@ -37,8 +38,9 @@ void f2() {
3738

3839
int main()
3940
{
41+
unsigned size = sizeof(*c); // GOOD
4042
my_printf("%d\n", b); // BAD
4143
b = f1();
4244
f2();
4345
return 0;
44-
}
46+
}

0 commit comments

Comments
 (0)