@@ -17,7 +17,7 @@ import codingstandards.cpp.Alignment
1717import codingstandards.cpp.dataflow.DataFlow
1818import codingstandards.cpp.dataflow.DataFlow2
1919import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
20- import DataFlow :: PathGraph
20+ import ExprWithAlignmentToCStyleCastFlow :: PathGraph
2121
2222/**
2323 * An expression with a type that has defined alignment requirements
@@ -96,8 +96,7 @@ class UnconvertedCastFromNonVoidPointerExpr extends Expr {
9696 */
9797class DefaultAlignedPointerExpr extends UnconvertedCastFromNonVoidPointerExpr , ExprWithAlignment {
9898 DefaultAlignedPointerExpr ( ) {
99- not any ( AllocationOrAddressOfExprToUnconvertedCastFromNonVoidPointerExprConfig config )
100- .hasFlowTo ( DataFlow:: exprNode ( this ) )
99+ not AllocationOrAddressOfExprToUnconvertedCastFromNonVoidPointerExprFlow:: flowTo ( DataFlow:: exprNode ( this ) )
101100 }
102101
103102 override int getAlignment ( ) { result = this .getType ( ) .( PointerType ) .getBaseType ( ) .getAlignment ( ) }
@@ -118,43 +117,37 @@ class DefaultAlignedPointerExpr extends UnconvertedCastFromNonVoidPointerExpr, E
118117 * to exclude an `DefaultAlignedPointerAccessExpr` as a source if a preceding source
119118 * defined by this configuration provides more accurate alignment information.
120119 */
121- class AllocationOrAddressOfExprToUnconvertedCastFromNonVoidPointerExprConfig extends DataFlow2:: Configuration
120+ module AllocationOrAddressOfExprToUnconvertedCastFromNonVoidPointerExprConfig implements
121+ DataFlow:: ConfigSig
122122{
123- AllocationOrAddressOfExprToUnconvertedCastFromNonVoidPointerExprConfig ( ) {
124- this = "AllocationOrAddressOfExprToUnconvertedCastFromNonVoidPointerExprConfig"
125- }
126-
127- override predicate isSource ( DataFlow:: Node source ) {
123+ predicate isSource ( DataFlow:: Node source ) {
128124 source .asExpr ( ) instanceof AddressOfAlignedVariableExpr or
129125 source .asExpr ( ) instanceof DefinedAlignmentAllocationExpr
130126 }
131127
132- override predicate isSink ( DataFlow:: Node sink ) {
128+ predicate isSink ( DataFlow:: Node sink ) {
133129 sink .asExpr ( ) instanceof UnconvertedCastFromNonVoidPointerExpr
134130 }
135131}
136132
133+ module AllocationOrAddressOfExprToUnconvertedCastFromNonVoidPointerExprFlow =
134+ DataFlow:: Global< AllocationOrAddressOfExprToUnconvertedCastFromNonVoidPointerExprConfig > ;
135+
137136/**
138137 * A data-flow configuration for analysing the flow of `ExprWithAlignment` pointer expressions
139138 * to casts which perform pointer type conversions and potentially create pointer alignment issues.
140139 */
141- class ExprWithAlignmentToCStyleCastConfiguration extends DataFlow:: Configuration {
142- ExprWithAlignmentToCStyleCastConfiguration ( ) {
143- this = "ExprWithAlignmentToCStyleCastConfiguration"
144- }
140+ module ExprWithAlignmentToCStyleCastConfig implements DataFlow:: ConfigSig {
141+ predicate isSource ( DataFlow:: Node source ) { source .asExpr ( ) instanceof ExprWithAlignment }
145142
146- override predicate isSource ( DataFlow:: Node source ) {
147- source .asExpr ( ) instanceof ExprWithAlignment
148- }
149-
150- override predicate isSink ( DataFlow:: Node sink ) {
143+ predicate isSink ( DataFlow:: Node sink ) {
151144 exists ( CStyleCast cast |
152145 cast .getUnderlyingType ( ) instanceof PointerType and
153146 cast .getUnconverted ( ) = sink .asExpr ( )
154147 )
155148 }
156149
157- override predicate isBarrierOut ( DataFlow:: Node node ) {
150+ predicate isBarrierOut ( DataFlow:: Node node ) {
158151 // the default interprocedural data-flow model flows through any array assignment expressions
159152 // to the qualifier (array base or pointer dereferenced) instead of the individual element
160153 // that the assignment modifies. this default behaviour causes false positives for any future
@@ -169,12 +162,15 @@ class ExprWithAlignmentToCStyleCastConfiguration extends DataFlow::Configuration
169162 }
170163}
171164
165+ module ExprWithAlignmentToCStyleCastFlow = DataFlow:: Global< ExprWithAlignmentToCStyleCastConfig > ;
166+
172167from
173- DataFlow:: PathNode source , DataFlow:: PathNode sink , ExprWithAlignment expr , CStyleCast cast ,
168+ ExprWithAlignmentToCStyleCastFlow:: PathNode source ,
169+ ExprWithAlignmentToCStyleCastFlow:: PathNode sink , ExprWithAlignment expr , CStyleCast cast ,
174170 Type toBaseType , int alignmentFrom , int alignmentTo
175171where
176172 not isExcluded ( cast , Pointers3Package:: doNotCastPointerToMoreStrictlyAlignedPointerTypeQuery ( ) ) and
177- any ( ExprWithAlignmentToCStyleCastConfiguration config ) . hasFlowPath ( source , sink ) and
173+ ExprWithAlignmentToCStyleCastFlow :: flowPath ( source , sink ) and
178174 source .getNode ( ) .asExpr ( ) = expr and
179175 sink .getNode ( ) .asExpr ( ) = cast .getUnconverted ( ) and
180176 toBaseType = cast .getActualType ( ) .( PointerType ) .getBaseType ( ) and
0 commit comments