C++: Remove uses of getQualifiedName

This removes all uses of `Declaration.getQualifiedName` that I think can
be removed without changing any behaviour. The following uses in the
LGTM default suite remain:

* `cpp/ql/src/Security/CWE/CWE-121/UnterminatedVarargsCall.ql` (in `select`).
* `cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowDispatch.qll` (needs template args).
* `cpp/ql/src/semmle/code/cpp/security/FunctionWithWrappers.qll` (used for alert messages).
This commit is contained in:
Jonas Jensen
2019-05-03 08:44:41 +02:00
parent 0a2e28858a
commit 64a87a863c
16 changed files with 34 additions and 61 deletions

View File

@@ -20,7 +20,7 @@ predicate global(GlobalVariable v) {
}
predicate mainCalled(Function f) {
f.getQualifiedName() = "main"
f.hasGlobalName("main")
or
exists(Function caller | mainCalled(caller) and allCalls(caller, f))
}

View File

@@ -17,12 +17,12 @@ import cpp
class Allocation extends FunctionCall {
Allocation() {
exists(string name |
this.getTarget().hasQualifiedName(name) and
this.getTarget().hasGlobalName(name) and
(name = "malloc" or name = "calloc" or name = "realloc")
)
}
string getName() { result = this.getTarget().getQualifiedName() }
private string getName() { this.getTarget().hasGlobalName(result) }
int getSize() {
this.getName() = "malloc" and

View File

@@ -17,12 +17,12 @@ import cpp
class Allocation extends FunctionCall {
Allocation() {
exists(string name |
this.getTarget().hasQualifiedName(name) and
this.getTarget().hasGlobalName(name) and
(name = "malloc" or name = "calloc" or name = "realloc")
)
}
string getName() { result = this.getTarget().getQualifiedName() }
private string getName() { this.getTarget().hasGlobalName(result) }
int getSize() {
this.getName() = "malloc" and

View File

@@ -61,7 +61,7 @@ class Options extends string
*/
predicate exits(Function f) {
f.getAnAttribute().hasName("noreturn") or
exists(string name | f.getQualifiedName() = name |
exists(string name | f.hasGlobalName(name) |
name = "exit" or
name = "_exit" or
name = "abort" or

View File

@@ -58,11 +58,11 @@ predicate refToStdString(Expr e, ConstructorCall source) {
* will also become invalid.
*/
predicate flowFunction(Function fcn, int argIndex) {
(fcn.getQualifiedName() = "_JNIEnv::NewStringUTF" and argIndex = 0)
(fcn.hasQualifiedName("", "_JNIEnv", "NewStringUTF") and argIndex = 0)
or
(fcn.getQualifiedName() = "art::JNI::NewStringUTF" and argIndex = 1)
(fcn.hasQualifiedName("art", "JNI", "NewStringUTF") and argIndex = 1)
or
(fcn.getQualifiedName() = "art::CheckJNI::NewStringUTF" and argIndex = 1)
(fcn.hasQualifiedName("art", "CheckJNI", "NewStringUTF") and argIndex = 1)
// Add other functions that behave like NewStringUTF here.
}

View File

@@ -13,13 +13,10 @@ import cpp
Expr getTest() {
// cppunit tests; https://freedesktop.org/wiki/Software/cppunit/
exists(Function f | result.(FunctionCall).getTarget() = f
and f.getNamespace().getName() = "CppUnit"
and f.getName() = "addTest")
result.(FunctionCall).getTarget().hasQualifiedName("CppUnit", _, "addTest")
or
// boost tests; http://www.boost.org/
exists(Function f | result.(FunctionCall).getTarget() = f
and f.getQualifiedName() = "boost::unit_test::make_test_case")
result.(FunctionCall).getTarget().hasQualifiedName("boost::unit_test", "make_test_case")
}
from File f, int n

View File

@@ -23,7 +23,7 @@ import semmle.code.cpp.security.TaintTracking
*/
class FileFunction extends FunctionWithWrappers {
FileFunction() {
exists(string nme | this.getQualifiedName() = nme |
exists(string nme | this.hasGlobalName(nme) |
nme = "fopen" or
nme = "_fopen" or
nme = "_wfopen" or
@@ -32,10 +32,7 @@ class FileFunction extends FunctionWithWrappers {
nme = "_wopen" or
// create file function on windows
nme.matches("CreateFile%") or
// Objective C standard library
nme.matches("NSFileHandle%::+fileHandleFor%AtPath:")
nme.matches("CreateFile%")
)
or
(

View File

@@ -12,7 +12,7 @@
import cpp
predicate potentiallyDangerousFunction(Function f, string message) {
exists(string name | name = f.getQualifiedName() |
exists(string name | f.hasGlobalName(name) |
(
name = "gmtime" or
name = "localtime" or
@@ -21,7 +21,7 @@ predicate potentiallyDangerousFunction(Function f, string message) {
) and
message = "Call to " + name + " is potentially dangerous"
) or (
f.getQualifiedName() = "gets" and
f.hasGlobalName("gets") and
message = "gets does not guard against buffer overflow"
)
}

View File

@@ -21,7 +21,7 @@ predicate acquireExpr(Expr acquire, string kind) {
exists(FunctionCall fc, Function f, string name |
fc = acquire and
f = fc.getTarget() and
name = f.getQualifiedName() and
f.hasGlobalName(name) and
(
(
name = "fopen" and
@@ -47,7 +47,7 @@ predicate releaseExpr(Expr release, Expr resource, string kind) {
exists(FunctionCall fc, Function f, string name |
fc = release and
f = fc.getTarget() and
name = f.getQualifiedName() and
f.hasGlobalName(name) and
(
(
name = "fclose" and
@@ -252,7 +252,7 @@ pragma[noopt] predicate badRelease(Resource r, Expr acquire, Function functionCa
)
}
Class qtObject() { result.getABaseClass*().getQualifiedName() = "QObject" }
Class qtObject() { result.getABaseClass*().hasGlobalName("QObject") }
PointerType qtObjectReference() { result.getBaseType() = qtObject() }
Constructor qtParentConstructor() {
exists(Parameter p |

View File

@@ -74,20 +74,6 @@ class UserDefinedFormattingFunction extends FormattingFunction {
override int getFormatParameterIndex() { callsVariadicFormatter(this, result) }
}
/**
* The Objective C method `stringWithFormat:`.
*/
class NsstringStringWithFormat extends FormattingFunction {
NsstringStringWithFormat() {
getQualifiedName().matches("NSString%::+stringWithFormat:") or
getQualifiedName().matches("NSString%::+localizedStringWithFormat:")
}
override int getFormatParameterIndex() {
result = 0
}
}
/**
* A call to one of the formatting functions.
*/

View File

@@ -260,13 +260,13 @@ predicate callMayReturnNull(Call call)
*/
predicate mayReturnNull(Function f)
{
f.getQualifiedName() = "malloc"
f.hasGlobalName("malloc")
or
f.getQualifiedName() = "calloc"
f.hasGlobalName("calloc")
or
// f.getQualifiedName() = "strchr"
// f.hasGlobalName("strchr")
// or
// f.getQualifiedName() = "strstr"
// f.hasGlobalName("strstr")
// or
exists(ReturnStmt ret |
nullValue(ret.getExpr()) and

View File

@@ -69,8 +69,8 @@ class Sprintf extends FormattingFunction {
or
hasGlobalName("__builtin___sprintf_chk") and result = 3
or
getQualifiedName() != "g_strdup_printf" and
getQualifiedName() != "__builtin___sprintf_chk" and
getName() != "g_strdup_printf" and
getName() != "__builtin___sprintf_chk" and
result = 1
}
override int getOutputParameterIndex() {
@@ -127,7 +127,7 @@ class Snprintf extends FormattingFunction {
override int getFirstFormatArgumentIndex() {
exists(string name |
name = getQualifiedName()
hasGlobalName(name)
and (
name = "__builtin___snprintf_chk" and
result = 5

View File

@@ -532,7 +532,7 @@ private int path_max() {
class RealpathBW extends BufferWriteCall {
RealpathBW() {
exists(path_max()) and // Ignore realpath() calls if PATH_MAX cannot be determined
getTarget().getQualifiedName() = "realpath" // realpath(path, resolved_path);
getTarget().hasGlobalName("realpath") // realpath(path, resolved_path);
}
override Type getBufferType()

View File

@@ -85,8 +85,8 @@ class VarargsExecFunctionCall extends FunctionCall {
* all the other ones start with the command. */
private int getCommandIdx() {
if (
getTarget().getQualifiedName().matches("\\_spawn%")
or getTarget().getQualifiedName().matches("\\_wspawn%"))
getTarget().getName().matches("\\_spawn%")
or getTarget().getName().matches("\\_wspawn%"))
then result = 1
else result = 0
}
@@ -137,8 +137,8 @@ class ArrayExecFunctionCall extends FunctionCall {
* all the other ones start with the command. */
private int getCommandIdx() {
if (
getTarget().getQualifiedName().matches("\\_spawn%")
or getTarget().getQualifiedName().matches("\\_wspawn%"))
getTarget().getName().matches("\\_spawn%")
or getTarget().getName().matches("\\_wspawn%"))
then result = 1
else result = 0
}

View File

@@ -154,7 +154,7 @@ private predicate fileStreamChain(ChainedOutputCall out, Expr source, Expr dest)
*/
private predicate fileWrite(Call write, Expr source, Expr dest) {
exists(Function f, int s, int d | f = write.getTarget() and source = write.getArgument(s) and dest = write.getArgument(d) |
exists(string name | name = f.getQualifiedName() |
exists(string name | f.hasGlobalName(name) |
// named functions
name = "fwrite" and s = 0 and d = 3 or
(
@@ -165,14 +165,7 @@ private predicate fileWrite(Call write, Expr source, Expr dest) {
name = "putc" or
name = "putwc" or
name = "putw"
) and s = 0 and d = 1 or
name.matches("NSFileManager%::-createFileAtPath:contents:attributes:") and s = 1 and d = 0 or
(
// methods that write into the receiver
dest = write.getQualifier() and
source = write.getArgument(0) and
name.matches("NSFileHandle%::-writeData:")
)
) and s = 0 and d = 1
) or (
// fprintf
s >= f.(Fprintf).getFormatParameterIndex() and

View File

@@ -66,8 +66,8 @@ private predicate outputWrite(Expr write, Expr source) {
) or (
// puts, putchar
(
f.getQualifiedName() = "puts" or
f.getQualifiedName() = "putchar"
f.hasGlobalName("puts") or
f.hasGlobalName("putchar")
) and arg = 0
) or exists(Call wrappedCall, Expr wrappedSource |
// wrapped output call (recursive case)