@@ -21,80 +21,55 @@ class NonConstCharStarType extends Type {
21
21
}
22
22
23
23
/* A non-const-char* variable declared with a string literal */
24
- predicate declaringNonConstCharVar ( Variable decl ) {
24
+ predicate declaringNonConstCharVar ( Variable decl , string message ) {
25
25
not decl instanceof Parameter and // exclude parameters
26
26
/* It should be declaring a char* type variable */
27
27
decl .getUnspecifiedType ( ) instanceof CharPointerType and
28
28
not decl .getUnderlyingType ( ) .isDeeplyConstBelow ( ) and
29
29
/* But it's declared to hold a string literal. */
30
- decl .getInitializer ( ) .getExpr ( ) instanceof StringLiteral
30
+ decl .getInitializer ( ) .getExpr ( ) instanceof StringLiteral and
31
+ message = "char* variable " + decl + " is declared with a string literal."
31
32
}
32
33
33
34
/* String literal being assigned to a non-const-char* variable */
34
- predicate assignmentToNonConstCharVar ( Assignment assign ) {
35
+ predicate assignmentToNonConstCharVar ( Assignment assign , string message ) {
35
36
/* The variable being assigned is char* */
36
37
assign .getLValue ( ) .getUnderlyingType ( ) instanceof NonConstCharStarType and
37
38
/* But the rvalue is a string literal */
38
- exists ( Expr rvalue | rvalue = assign .getRValue ( ) | rvalue instanceof StringLiteral )
39
+ exists ( Expr rvalue | rvalue = assign .getRValue ( ) | rvalue instanceof StringLiteral ) and
40
+ message = "char* variable " + assign .getLValue ( ) + " is assigned a string literal. "
39
41
}
40
42
41
43
/* String literal being passed to a non-const-char* parameter */
42
- predicate assignmentToNonConstCharParam ( FunctionCall call ) {
44
+ predicate assignmentToNonConstCharParam ( FunctionCall call , string message ) {
43
45
exists ( int index |
44
46
/* Param at index is a char* */
45
47
call .getTarget ( ) .getParameter ( index ) .getUnderlyingType ( ) instanceof NonConstCharStarType and
46
48
/* But a string literal is passed */
47
49
call .getArgument ( index ) instanceof StringLiteral
48
- )
50
+ ) and
51
+ message = "char* parameter of " + call .getTarget ( ) + " is passed a string literal."
49
52
}
50
53
51
54
/* String literal being returned by a non-const-char* function */
52
- predicate returningNonConstCharVar ( ReturnStmt return ) {
55
+ predicate returningNonConstCharVar ( ReturnStmt return , string message ) {
53
56
/* The function is declared to return a char* */
54
57
return .getEnclosingFunction ( ) .getType ( ) .resolveTypedefs ( ) instanceof NonConstCharStarType and
55
58
/* But in reality it returns a string literal */
56
- return .getExpr ( ) instanceof StringLiteral
59
+ return .getExpr ( ) instanceof StringLiteral and
60
+ message = "char* function " + return .getEnclosingFunction ( ) + " is returning a string literal."
57
61
}
58
62
59
- // newtype TProblematicElem =
60
- // TVar(Variable decl) or
61
- // TAssign(Assignment assign) or
62
- // TFunCall(FunctionCall call) or
63
- // TReturnStmt(ReturnStmt return)
64
- // class ProblematicElem extends TProblematicElem {
65
- // Variable getVariable() { this = TVar(result) }
66
- // Assignment getAssign() { this = TAssign(result) }
67
- // FunctionCall getFunCall() { this = TFunCall(result) }
68
- // ReturnStmt getReturnStmt() { this = TReturnStmt(result) }
69
- // override string toString() {
70
- // this instanceof TVar and result = this.getVariable().toString()
71
- // or
72
- // this instanceof TAssign and result = this.getAssign().toString()
73
- // or
74
- // this instanceof TFunCall and result = this.getFunCall().toString()
75
- // or
76
- // this instanceof TReturnStmt and result = this.getReturnStmt().toString()
77
- // }
78
- // }
79
- // class ProblematicElem = Variable or Assignment or FunctionCall or ReturnStmt;
80
- // ^ Nope!
81
- from Variable decl , Assignment assign , FunctionCall call , ReturnStmt return , string message
63
+ from Element elem , string message
82
64
where
83
- not isExcluded ( decl , TypesPackage:: stringLiteralAssignedToNonConstCharQuery ( ) ) and
84
- not isExcluded ( assign , TypesPackage:: stringLiteralAssignedToNonConstCharQuery ( ) ) and
85
- not isExcluded ( call , TypesPackage:: stringLiteralAssignedToNonConstCharQuery ( ) ) and
86
- not isExcluded ( return , TypesPackage:: stringLiteralAssignedToNonConstCharQuery ( ) ) and
65
+ not isExcluded ( elem , TypesPackage:: stringLiteralAssignedToNonConstCharQuery ( ) ) and
87
66
(
88
- declaringNonConstCharVar ( decl ) and
89
- message = "char* variable " + decl + " is declared with a string literal."
67
+ declaringNonConstCharVar ( elem , message )
90
68
or
91
- assignmentToNonConstCharVar ( assign ) and
92
- message = "char* variable " + assign .getLValue ( ) + " is assigned a string literal. "
69
+ assignmentToNonConstCharVar ( elem , message )
93
70
or
94
- assignmentToNonConstCharParam ( call ) and
95
- message = "char* parameter of " + call .getTarget ( ) + " is passed a string literal."
71
+ assignmentToNonConstCharParam ( elem , message )
96
72
or
97
- returningNonConstCharVar ( return ) and
98
- message = "char* function " + return .getEnclosingFunction ( ) + " is returning a string literal."
73
+ returningNonConstCharVar ( elem , message )
99
74
)
100
75
select message
0 commit comments