-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathStringLiteralAssignedToNonConstChar.ql
More file actions
103 lines (93 loc) · 3.76 KB
/
StringLiteralAssignedToNonConstChar.ql
File metadata and controls
103 lines (93 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* @id c/misra/string-literal-assigned-to-non-const-char
* @name RULE-7-4: A string literal shall only be assigned to a pointer to const char
* @description Assigning string literal to a variable with type other than a pointer to const char
* and modifying it causes undefined behavior .
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-7-4
* external/misra/obligation/required
*/
import cpp
import codingstandards.c.misra
/** Pointer to Wide character type, i.e. `wchar_t*`. */
class WideCharPointerType extends PointerType {
WideCharPointerType() { this.getBaseType() instanceof Wchar_t }
override string getAPrimaryQlClass() { result = "WideCharPointerType" }
}
class GenericCharPointerType extends Type {
GenericCharPointerType() {
// A wide char pointer type
this instanceof WideCharPointerType
or
// A char pointer type
this.getUnspecifiedType() instanceof CharPointerType
or
// A typedef to any such type.
// Note: wchar_t is usually a typedef, so we cannot just use getUnspecifiedType() here.
this.(TypedefType).getBaseType() instanceof GenericCharPointerType
}
}
class NonConstCharStarType extends Type {
NonConstCharStarType() {
this instanceof GenericCharPointerType and
not this.isDeeplyConstBelow()
}
}
/* A non-const-char* variable declared with a string literal */
predicate declaringNonConstCharVar(Variable decl, string message) {
not decl instanceof Parameter and // exclude parameters
/* It should be declaring a char* type variable */
decl.getType() instanceof NonConstCharStarType and
/* But it's declared to hold a string literal. */
decl.getInitializer().getExpr() instanceof StringLiteral and
message =
decl.getType().(GenericCharPointerType) + " variable " + decl +
" is declared with a string literal."
}
/* String literal being assigned to a non-const-char* variable */
predicate assignmentToNonConstCharVar(Assignment assign, string message) {
/* The variable being assigned is char* */
assign.getLValue().getType() instanceof NonConstCharStarType and
/* But the rvalue is a string literal */
assign.getRValue() instanceof StringLiteral and
message =
assign.getLValue().getType().(GenericCharPointerType) + " variable " + assign.getLValue() +
" is assigned a string literal. "
}
/* String literal being passed to a non-const-char* parameter */
predicate assignmentToNonConstCharParam(FunctionCall call, string message) {
exists(int index |
/* Param at index is a char* */
call.getTarget().getParameter(index).getType() instanceof NonConstCharStarType and
/* But a string literal is passed */
call.getArgument(index) instanceof StringLiteral and
message =
call.getTarget().getParameter(index).getType().(GenericCharPointerType) + " parameter of " +
call.getTarget() + " is passed a string literal."
)
}
/* String literal being returned by a non-const-char* function */
predicate returningNonConstCharVar(ReturnStmt return, string message) {
/* The function is declared to return a char* */
return.getEnclosingFunction().getType() instanceof NonConstCharStarType and
/* But in reality it returns a string literal */
return.getExpr() instanceof StringLiteral and
message =
return.getEnclosingFunction().getType().(GenericCharPointerType) + " function " +
return.getEnclosingFunction() + " is returning a string literal."
}
from Element elem, string message
where
not isExcluded(elem, Types1Package::stringLiteralAssignedToNonConstCharQuery()) and
(
declaringNonConstCharVar(elem, message)
or
assignmentToNonConstCharVar(elem, message)
or
assignmentToNonConstCharParam(elem, message)
or
returningNonConstCharVar(elem, message)
)
select elem, message