Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cpp/ql/src/Critical/GlobalUseBeforeInit.ql
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ predicate dominatingInitInFunc(GlobalVariable v, Function f, ControlFlowNode nod
)
}

predicate safeAccess(VariableAccess access) {
// it is safe if the variable access is part of a `sizeof` expression
exists(SizeofExprOperator e |
e.getAChild*() = access
)
Comment thread
mrigankpawagi marked this conversation as resolved.
Outdated
}

predicate useFunc(GlobalVariable v, Function f) {
exists(VariableAccess access |
v.getAnAccess() = access and
access.isRValue() and
access.getEnclosingFunction() = f and
not safeAccess(access) and
not dominatingInitInFunc(v, f, access)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
| 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 |
| 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 |
| 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 |
| 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 |
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ int vfprintf (FILE *, const char *, va_list);

int a = 1;
int b;
int *c;

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

int main()
{
unsigned size = sizeof(*c); // GOOD
Comment thread
mrigankpawagi marked this conversation as resolved.
my_printf("%d\n", b); // BAD
b = f1();
f2();
return 0;
}
}
Loading