Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 057e810

Browse files
committed
C++: Fix flow through arrays.
1 parent 9d64c0a commit 057e810

4 files changed

Lines changed: 18 additions & 30 deletions

File tree

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,6 @@ class OperandNode extends Node, Node0 {
441441
Type stripPointer(Type t) {
442442
result = any(Ssa::Indirection ind | ind.getType() = t).getBaseType()
443443
or
444-
// These types have a sensible base type, but don't receive additional
445-
// dataflow nodes representing their indirections. So for now we special case them.
446-
result = t.(ArrayType).getBaseType()
447-
or
448444
result = t.(PointerToMemberType).getBaseType()
449445
or
450446
result = t.(FunctionPointerIshType).getBaseType()

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternals.qll

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,9 @@ private newtype TDefOrUseImpl =
176176
cppType.hasUnspecifiedType(p.getUnspecifiedType(), _) and
177177
isModifiableAt(cppType, indirectionIndex + 1)
178178
) and
179-
(
180-
exists(Indirection indirection |
181-
indirection.getType() = p.getUnspecifiedType() and
182-
indirectionIndex = [1 .. indirection.getNumberOfIndirections()]
183-
)
184-
or
185-
// Array types don't have indirections. So we need to special case them here.
186-
exists(Cpp::ArrayType arrayType, CppType cppType |
187-
arrayType = p.getUnspecifiedType() and
188-
cppType.hasUnspecifiedType(arrayType, _) and
189-
indirectionIndex = [1 .. countIndirectionsForCppType(cppType)]
190-
)
179+
exists(Indirection indirection |
180+
indirection.getType() = p.getUnspecifiedType() and
181+
indirectionIndex = [1 .. indirection.getNumberOfIndirections()]
191182
)
192183
}
193184

@@ -287,7 +278,7 @@ abstract class DefImpl extends DefOrUseImpl {
287278

288279
override int getIndirectionIndex() { result = ind }
289280

290-
override string toString() { result = "DefImpl" }
281+
override string toString() { result = "Def of " + this.getSourceVariable() }
291282

292283
override Cpp::Location getLocation() { result = this.getAddressOperand().getUse().getLocation() }
293284

@@ -331,7 +322,7 @@ abstract class UseImpl extends DefOrUseImpl {
331322
/** Gets the node associated with this use. */
332323
abstract Node getNode();
333324

334-
override string toString() { result = "UseImpl" }
325+
override string toString() { result = "Use of " + this.getSourceVariable() }
335326

336327
/** Gets the indirection index of this use. */
337328
final override int getIndirectionIndex() { result = ind }

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternalsCommon.qll

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ int getMaxIndirectionsForType(Type type) {
8686
result = countIndirectionsForCppType(getTypeForGLValue(type))
8787
}
8888

89-
private class PointerOrReferenceType extends Cpp::DerivedType {
90-
PointerOrReferenceType() {
89+
private class PointerOrArrayOrReferenceType extends Cpp::DerivedType {
90+
PointerOrArrayOrReferenceType() {
9191
this instanceof Cpp::PointerType
9292
or
93+
this instanceof Cpp::ArrayType
94+
or
9395
this instanceof Cpp::ReferenceType
9496
}
9597
}
@@ -180,8 +182,10 @@ abstract class Indirection extends Type {
180182
predicate isAdditionalConversionFlow(Operand opFrom, Instruction instrTo) { none() }
181183
}
182184

183-
private class PointerOrReferenceTypeIndirection extends Indirection instanceof PointerOrReferenceType {
184-
PointerOrReferenceTypeIndirection() { baseType = PointerOrReferenceType.super.getBaseType() }
185+
private class PointerOrArrayOrReferenceTypeIndirection extends Indirection instanceof PointerOrArrayOrReferenceType {
186+
PointerOrArrayOrReferenceTypeIndirection() {
187+
baseType = PointerOrArrayOrReferenceType.super.getBaseType()
188+
}
185189

186190
override int getNumberOfIndirections() {
187191
result = 1 + countIndirections(this.getBaseType().getUnspecifiedType())
@@ -211,7 +215,8 @@ private module IteratorIndirections {
211215

212216
class IteratorIndirection extends Indirection instanceof Interfaces::Iterator {
213217
IteratorIndirection() {
214-
not this instanceof PointerOrReferenceTypeIndirection and baseType = super.getValueType()
218+
not this instanceof PointerOrArrayOrReferenceTypeIndirection and
219+
baseType = super.getValueType()
215220
}
216221

217222
override int getNumberOfIndirections() {
@@ -399,7 +404,7 @@ predicate isModifiableByCall(ArgumentOperand operand, int indirectionIndex) {
399404
// by `call` should not be of the form `const T*` (for some deeply const type `T`).
400405
if call.getStaticCallTarget() instanceof Cpp::ConstMemberFunction
401406
then
402-
exists(PointerOrReferenceType resultType |
407+
exists(PointerOrArrayOrReferenceType resultType |
403408
resultType = call.getResultType() and
404409
not resultType.isDeeplyConstBelow()
405410
)
@@ -420,10 +425,7 @@ private predicate isModifiableAtImpl(CppType cppType, int indirectionIndex) {
420425
(
421426
exists(Type pointerType, Type base, Type t |
422427
pointerType = t.getUnderlyingType() and
423-
(
424-
pointerType = any(Indirection ind).getUnderlyingType() or
425-
pointerType instanceof Cpp::ArrayType
426-
) and
428+
pointerType = any(Indirection ind).getUnderlyingType() and
427429
cppType.hasType(t, _) and
428430
base = getTypeImpl(pointerType, indirectionIndex)
429431
|

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/ssa0/SsaInternals.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ private newtype TDefOrUseImpl =
4141
isIteratorUse(container, iteratorAddress, _, _)
4242
} or
4343
TFinalParameterUse(Parameter p) {
44-
any(Indirection indirection).getType() = p.getUnspecifiedType() or
45-
p.getUnspecifiedType() instanceof Cpp::ArrayType
44+
any(Indirection indirection).getType() = p.getUnspecifiedType()
4645
}
4746

4847
abstract private class DefOrUseImpl extends TDefOrUseImpl {

0 commit comments

Comments
 (0)