@@ -21,80 +21,55 @@ class NonConstCharStarType extends Type {
2121}
2222
2323/* A non-const-char* variable declared with a string literal */
24- predicate declaringNonConstCharVar ( Variable decl ) {
24+ predicate declaringNonConstCharVar ( Variable decl , string message ) {
2525 not decl instanceof Parameter and // exclude parameters
2626 /* It should be declaring a char* type variable */
2727 decl .getUnspecifiedType ( ) instanceof CharPointerType and
2828 not decl .getUnderlyingType ( ) .isDeeplyConstBelow ( ) and
2929 /* 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."
3132}
3233
3334/* String literal being assigned to a non-const-char* variable */
34- predicate assignmentToNonConstCharVar ( Assignment assign ) {
35+ predicate assignmentToNonConstCharVar ( Assignment assign , string message ) {
3536 /* The variable being assigned is char* */
3637 assign .getLValue ( ) .getUnderlyingType ( ) instanceof NonConstCharStarType and
3738 /* 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. "
3941}
4042
4143/* String literal being passed to a non-const-char* parameter */
42- predicate assignmentToNonConstCharParam ( FunctionCall call ) {
44+ predicate assignmentToNonConstCharParam ( FunctionCall call , string message ) {
4345 exists ( int index |
4446 /* Param at index is a char* */
4547 call .getTarget ( ) .getParameter ( index ) .getUnderlyingType ( ) instanceof NonConstCharStarType and
4648 /* But a string literal is passed */
4749 call .getArgument ( index ) instanceof StringLiteral
48- )
50+ ) and
51+ message = "char* parameter of " + call .getTarget ( ) + " is passed a string literal."
4952}
5053
5154/* String literal being returned by a non-const-char* function */
52- predicate returningNonConstCharVar ( ReturnStmt return ) {
55+ predicate returningNonConstCharVar ( ReturnStmt return , string message ) {
5356 /* The function is declared to return a char* */
5457 return .getEnclosingFunction ( ) .getType ( ) .resolveTypedefs ( ) instanceof NonConstCharStarType and
5558 /* 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."
5761}
5862
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
8264where
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
8766 (
88- declaringNonConstCharVar ( decl ) and
89- message = "char* variable " + decl + " is declared with a string literal."
67+ declaringNonConstCharVar ( elem , message )
9068 or
91- assignmentToNonConstCharVar ( assign ) and
92- message = "char* variable " + assign .getLValue ( ) + " is assigned a string literal. "
69+ assignmentToNonConstCharVar ( elem , message )
9370 or
94- assignmentToNonConstCharParam ( call ) and
95- message = "char* parameter of " + call .getTarget ( ) + " is passed a string literal."
71+ assignmentToNonConstCharParam ( elem , message )
9672 or
97- returningNonConstCharVar ( return ) and
98- message = "char* function " + return .getEnclosingFunction ( ) + " is returning a string literal."
73+ returningNonConstCharVar ( elem , message )
9974 )
10075select message
0 commit comments