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

Skip to content

Commit a4d434e

Browse files
committed
C++: Fix 'getType' for indirect dataflow nodes in IR dataflow.
1 parent 500004d commit a4d434e

1 file changed

Lines changed: 18 additions & 44 deletions

File tree

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

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -361,22 +361,20 @@ class OperandNode extends Node, TOperandNode {
361361
}
362362

363363
/**
364-
* Returns `t`, but stripped of the `n` outermost pointers, references, etc.
364+
* Returns `t`, but stripped of the outermost pointer, reference, etc.
365365
*
366-
* For example, `stripPointers(int*&, 2)` is `int` and `stripPointers(int*, 0)` is `int*`.
366+
* For example, `stripPointers(int*&)` is `int*` and `stripPointers(int*)` is `int`.
367367
*/
368-
private Type stripPointers(Type t, int n) {
369-
result = t and n = 0
368+
private Type stripPointer(Type t) {
369+
result = t.(PointerType).getBaseType()
370370
or
371-
result = stripPointers(t.(PointerType).getBaseType(), n - 1)
371+
result = t.(ArrayType).getBaseType()
372372
or
373-
result = stripPointers(t.(ArrayType).getBaseType(), n - 1)
373+
result = t.(ReferenceType).getBaseType()
374374
or
375-
result = stripPointers(t.(ReferenceType).getBaseType(), n - 1)
375+
result = t.(PointerToMemberType).getBaseType()
376376
or
377-
result = stripPointers(t.(PointerToMemberType).getBaseType(), n - 1)
378-
or
379-
result = stripPointers(t.(FunctionPointerIshType).getBaseType(), n - 1)
377+
result = t.(FunctionPointerIshType).getBaseType()
380378
}
381379

382380
/**
@@ -606,36 +604,12 @@ class IndirectReturnOutNode extends Node {
606604
int getIndirectionIndex() { result = indirectionIndex }
607605
}
608606

609-
private PointerType getGLValueType(Type t, int indirectionIndex) {
610-
result.getBaseType() = stripPointers(t, indirectionIndex - 1)
611-
}
612-
613-
bindingset[isGLValue]
614-
private DataFlowType getTypeImpl(Type t, int indirectionIndex, boolean isGLValue) {
615-
if isGLValue = true
616-
then
617-
result = getGLValueType(t, indirectionIndex)
618-
or
619-
// Ideally, the above case would cover all glvalue cases. However, consider the case where
620-
// the database consists only of:
621-
// ```
622-
// void test() {
623-
// int* x;
624-
// x = nullptr;
625-
// }
626-
// ```
627-
// and we want to compute the type of `*x` in the assignment `x = nullptr`. Here, `x` is an lvalue
628-
// of type int* (which morally is an int**). So when we call `getTypeImpl` it will be with the
629-
// parameters:
630-
// - t = int*
631-
// - indirectionIndex = 1 (when we want to model the dataflow node corresponding to *x)
632-
// - isGLValue = true
633-
// In this case, `getTypeImpl(t, indirectionIndex, isGLValue)` should give back `int**`. In this
634-
// case, however, `int**` does not exist in the database. So instead we return int* (which is
635-
// wrong, but at least we have a type).
636-
not exists(getGLValueType(t, indirectionIndex)) and
637-
result = stripPointers(t, indirectionIndex - 1)
638-
else result = stripPointers(t, indirectionIndex)
607+
private Type getTypeImpl(Type t, int indirectionIndex) {
608+
indirectionIndex = 0 and
609+
result = t
610+
or
611+
indirectionIndex > 0 and
612+
result = getTypeImpl(stripPointer(t), indirectionIndex - 1)
639613
}
640614

641615
/**
@@ -660,8 +634,8 @@ class IndirectOperand extends Node, TIndirectOperand {
660634
override Declaration getEnclosingCallable() { result = this.getFunction() }
661635

662636
override DataFlowType getType() {
663-
exists(boolean isGLValue | if operand.isGLValue() then isGLValue = true else isGLValue = false |
664-
result = getTypeImpl(operand.getType().getUnspecifiedType(), indirectionIndex, isGLValue)
637+
exists(int sub | if operand.isGLValue() then sub = 1 else sub = 0 |
638+
result = getTypeImpl(operand.getType().getUnspecifiedType(), indirectionIndex - sub)
665639
)
666640
}
667641

@@ -713,8 +687,8 @@ class IndirectInstruction extends Node, TIndirectInstruction {
713687
override Declaration getEnclosingCallable() { result = this.getFunction() }
714688

715689
override DataFlowType getType() {
716-
exists(boolean isGLValue | if instr.isGLValue() then isGLValue = true else isGLValue = false |
717-
result = getTypeImpl(instr.getResultType().getUnspecifiedType(), indirectionIndex, isGLValue)
690+
exists(int sub | if instr.isGLValue() then sub = 1 else sub = 0 |
691+
result = getTypeImpl(instr.getResultType().getUnspecifiedType(), indirectionIndex - sub)
718692
)
719693
}
720694

0 commit comments

Comments
 (0)