@@ -15,9 +15,8 @@ import cpp
1515import codingstandards.c.cert
1616import codingstandards.cpp.Alignment
1717import codingstandards.cpp.dataflow.DataFlow
18- import codingstandards.cpp.dataflow.DataFlow2
1918import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
20- import DataFlow :: PathGraph
19+ import ExprWithAlignmentToCStyleCastFlow :: PathGraph
2120
2221/**
2322 * An expression with a type that has defined alignment requirements
@@ -96,8 +95,7 @@ class UnconvertedCastFromNonVoidPointerExpr extends Expr {
9695 */
9796class DefaultAlignedPointerExpr extends UnconvertedCastFromNonVoidPointerExpr , ExprWithAlignment {
9897 DefaultAlignedPointerExpr ( ) {
99- not any ( AllocationOrAddressOfExprToUnconvertedCastFromNonVoidPointerExprConfig config )
100- .hasFlowTo ( DataFlow:: exprNode ( this ) )
98+ not AllocationOrAddressOfExprToUnconvertedCastFromNonVoidPointerExprFlow:: flowTo ( DataFlow:: exprNode ( this ) )
10199 }
102100
103101 override int getAlignment ( ) { result = this .getType ( ) .( PointerType ) .getBaseType ( ) .getAlignment ( ) }
@@ -118,43 +116,37 @@ class DefaultAlignedPointerExpr extends UnconvertedCastFromNonVoidPointerExpr, E
118116 * to exclude an `DefaultAlignedPointerAccessExpr` as a source if a preceding source
119117 * defined by this configuration provides more accurate alignment information.
120118 */
121- class AllocationOrAddressOfExprToUnconvertedCastFromNonVoidPointerExprConfig extends DataFlow2:: Configuration
119+ module AllocationOrAddressOfExprToUnconvertedCastFromNonVoidPointerExprConfig implements
120+ DataFlow:: ConfigSig
122121{
123- AllocationOrAddressOfExprToUnconvertedCastFromNonVoidPointerExprConfig ( ) {
124- this = "AllocationOrAddressOfExprToUnconvertedCastFromNonVoidPointerExprConfig"
125- }
126-
127- override predicate isSource ( DataFlow:: Node source ) {
122+ predicate isSource ( DataFlow:: Node source ) {
128123 source .asExpr ( ) instanceof AddressOfAlignedVariableExpr or
129124 source .asExpr ( ) instanceof DefinedAlignmentAllocationExpr
130125 }
131126
132- override predicate isSink ( DataFlow:: Node sink ) {
127+ predicate isSink ( DataFlow:: Node sink ) {
133128 sink .asExpr ( ) instanceof UnconvertedCastFromNonVoidPointerExpr
134129 }
135130}
136131
132+ module AllocationOrAddressOfExprToUnconvertedCastFromNonVoidPointerExprFlow =
133+ DataFlow:: Global< AllocationOrAddressOfExprToUnconvertedCastFromNonVoidPointerExprConfig > ;
134+
137135/**
138136 * A data-flow configuration for analysing the flow of `ExprWithAlignment` pointer expressions
139137 * to casts which perform pointer type conversions and potentially create pointer alignment issues.
140138 */
141- class ExprWithAlignmentToCStyleCastConfiguration extends DataFlow:: Configuration {
142- ExprWithAlignmentToCStyleCastConfiguration ( ) {
143- this = "ExprWithAlignmentToCStyleCastConfiguration"
144- }
139+ module ExprWithAlignmentToCStyleCastConfig implements DataFlow:: ConfigSig {
140+ predicate isSource ( DataFlow:: Node source ) { source .asExpr ( ) instanceof ExprWithAlignment }
145141
146- override predicate isSource ( DataFlow:: Node source ) {
147- source .asExpr ( ) instanceof ExprWithAlignment
148- }
149-
150- override predicate isSink ( DataFlow:: Node sink ) {
142+ predicate isSink ( DataFlow:: Node sink ) {
151143 exists ( CStyleCast cast |
152144 cast .getUnderlyingType ( ) instanceof PointerType and
153145 cast .getUnconverted ( ) = sink .asExpr ( )
154146 )
155147 }
156148
157- override predicate isBarrierOut ( DataFlow:: Node node ) {
149+ predicate isBarrierOut ( DataFlow:: Node node ) {
158150 // the default interprocedural data-flow model flows through any array assignment expressions
159151 // to the qualifier (array base or pointer dereferenced) instead of the individual element
160152 // that the assignment modifies. this default behaviour causes false positives for any future
@@ -169,12 +161,15 @@ class ExprWithAlignmentToCStyleCastConfiguration extends DataFlow::Configuration
169161 }
170162}
171163
164+ module ExprWithAlignmentToCStyleCastFlow = DataFlow:: Global< ExprWithAlignmentToCStyleCastConfig > ;
165+
172166from
173- DataFlow:: PathNode source , DataFlow:: PathNode sink , ExprWithAlignment expr , CStyleCast cast ,
167+ ExprWithAlignmentToCStyleCastFlow:: PathNode source ,
168+ ExprWithAlignmentToCStyleCastFlow:: PathNode sink , ExprWithAlignment expr , CStyleCast cast ,
174169 Type toBaseType , int alignmentFrom , int alignmentTo
175170where
176171 not isExcluded ( cast , Pointers3Package:: doNotCastPointerToMoreStrictlyAlignedPointerTypeQuery ( ) ) and
177- any ( ExprWithAlignmentToCStyleCastConfiguration config ) . hasFlowPath ( source , sink ) and
172+ ExprWithAlignmentToCStyleCastFlow :: flowPath ( source , sink ) and
178173 source .getNode ( ) .asExpr ( ) = expr and
179174 sink .getNode ( ) .asExpr ( ) = cast .getUnconverted ( ) and
180175 toBaseType = cast .getActualType ( ) .( PointerType ) .getBaseType ( ) and
0 commit comments