13
13
import cpp
14
14
import codingstandards.c.misra
15
15
16
+ /** Pointer to Wide character type, i.e. `wchar_t*`. */
17
+ class WideCharPointerType extends PointerType {
18
+ WideCharPointerType ( ) { this .getBaseType ( ) instanceof Wchar_t }
19
+
20
+ override string getAPrimaryQlClass ( ) { result = "WideCharPointerType" }
21
+ }
22
+
23
+ class GenericCharPointerType extends PointerType {
24
+ GenericCharPointerType ( ) {
25
+ /* This type resolves to wchar_t* (which is in turn a typedef depending on its implementation) */
26
+ this .resolveTypedefs * ( ) instanceof WideCharPointerType
27
+ or
28
+ /* This type eventually resolves to char* */
29
+ this .resolveTypedefs * ( ) instanceof CharPointerType
30
+ }
31
+
32
+ predicate isWideCharPointerType ( ) { this .resolveTypedefs * ( ) instanceof WideCharPointerType }
33
+
34
+ override string toString ( ) {
35
+ if this .isWideCharPointerType ( ) then result = "wchar_t*" else result = "char*"
36
+ }
37
+ }
38
+
16
39
class NonConstCharStarType extends Type {
17
40
NonConstCharStarType ( ) {
18
- this instanceof CharPointerType and
41
+ this instanceof GenericCharPointerType and
19
42
not this .isDeeplyConstBelow ( )
20
43
}
21
44
}
@@ -24,40 +47,48 @@ class NonConstCharStarType extends Type {
24
47
predicate declaringNonConstCharVar ( Variable decl , string message ) {
25
48
not decl instanceof Parameter and // exclude parameters
26
49
/* It should be declaring a char* type variable */
27
- decl .getUnspecifiedType ( ) instanceof CharPointerType and
28
- not decl .getUnderlyingType ( ) .isDeeplyConstBelow ( ) and
29
- /* But it's declared to hold a string literal. */
50
+ decl .getType ( ) instanceof GenericCharPointerType and
51
+ not decl .getType ( ) .isDeeplyConstBelow ( ) and
52
+ /* But it's declared to hold a string literal. */
30
53
decl .getInitializer ( ) .getExpr ( ) instanceof StringLiteral and
31
- message = "char* variable " + decl + " is declared with a string literal."
54
+ message =
55
+ decl .getType ( ) .( GenericCharPointerType ) + " variable " + decl +
56
+ " is declared with a string literal."
32
57
}
33
58
34
59
/* String literal being assigned to a non-const-char* variable */
35
60
predicate assignmentToNonConstCharVar ( Assignment assign , string message ) {
36
61
/* The variable being assigned is char* */
37
- assign .getLValue ( ) .getUnderlyingType ( ) instanceof NonConstCharStarType and
62
+ assign .getLValue ( ) .getType ( ) instanceof NonConstCharStarType and
38
63
/* But the rvalue is a string literal */
39
- exists ( Expr rvalue | rvalue = assign .getRValue ( ) | rvalue instanceof StringLiteral ) and
40
- message = "char* variable " + assign .getLValue ( ) + " is assigned a string literal. "
64
+ assign .getRValue ( ) instanceof StringLiteral and
65
+ message =
66
+ assign .getLValue ( ) .getType ( ) .( GenericCharPointerType ) + " variable " + assign .getLValue ( ) +
67
+ " is assigned a string literal. "
41
68
}
42
69
43
70
/* String literal being passed to a non-const-char* parameter */
44
71
predicate assignmentToNonConstCharParam ( FunctionCall call , string message ) {
45
72
exists ( int index |
46
73
/* Param at index is a char* */
47
- call .getTarget ( ) .getParameter ( index ) .getUnderlyingType ( ) instanceof NonConstCharStarType and
74
+ call .getTarget ( ) .getParameter ( index ) .getType ( ) instanceof NonConstCharStarType and
48
75
/* But a string literal is passed */
49
- call .getArgument ( index ) instanceof StringLiteral
50
- ) and
51
- message = "char* parameter of " + call .getTarget ( ) + " is passed a string literal."
76
+ call .getArgument ( index ) instanceof StringLiteral and
77
+ message =
78
+ call .getTarget ( ) .getParameter ( index ) .getType ( ) .( GenericCharPointerType ) + " parameter of " +
79
+ call .getTarget ( ) + " is passed a string literal."
80
+ )
52
81
}
53
82
54
83
/* String literal being returned by a non-const-char* function */
55
84
predicate returningNonConstCharVar ( ReturnStmt return , string message ) {
56
85
/* The function is declared to return a char* */
57
- return .getEnclosingFunction ( ) .getType ( ) . resolveTypedefs ( ) instanceof NonConstCharStarType and
86
+ return .getEnclosingFunction ( ) .getType ( ) instanceof NonConstCharStarType and
58
87
/* But in reality it returns a string literal */
59
88
return .getExpr ( ) instanceof StringLiteral and
60
- message = "char* function " + return .getEnclosingFunction ( ) + " is returning a string literal."
89
+ message =
90
+ return .getEnclosingFunction ( ) .getType ( ) .( GenericCharPointerType ) + " function " +
91
+ return .getEnclosingFunction ( ) + " is returning a string literal."
61
92
}
62
93
63
94
from Element elem , string message
0 commit comments