Skip to content

Commit 77bb230

Browse files
committed
C++: Small refactor.
1 parent 3777020 commit 77bb230

3 files changed

Lines changed: 19 additions & 11 deletions

File tree

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private module Cached {
144144
TNonUnionContent(CanonicalField f, int indirectionIndex) {
145145
// the indirection index for field content starts at 1 (because `TNonUnionContent` is thought of as
146146
// the address of the field, `FieldAddress` in the IR).
147-
indirectionIndex = [1 .. max(SsaImpl::getMaxIndirectionsForType(f.getAnUnspecifiedType()))] and
147+
indirectionIndex = [1 .. max(SsaImpl::getMaxIndirectionsForGLType(f.getAnUnspecifiedType()))] and
148148
// Reads and writes of union fields are tracked using `UnionContent`.
149149
not f.getDeclaringType() instanceof Union
150150
} or
@@ -156,7 +156,7 @@ private module Cached {
156156
// field can be read by any read of the union's fields. Again, the indirection index
157157
// is 1-based (because 0 is considered the address).
158158
indirectionIndex =
159-
[1 .. max(SsaImpl::getMaxIndirectionsForType(getAFieldWithSize(u, bytes)
159+
[1 .. max(SsaImpl::getMaxIndirectionsForGLType(getAFieldWithSize(u, bytes)
160160
.getAnUnspecifiedType())
161161
)]
162162
)
@@ -184,7 +184,7 @@ private module Cached {
184184
TNode0(Node0Impl node) { DataFlowImplCommon::forceCachingInSameStage() } or
185185
TGlobalLikeVariableNode(GlobalLikeVariable var, int indirectionIndex) {
186186
indirectionIndex =
187-
[getMinIndirectionsForType(var.getUnspecifiedType()) .. SsaImpl::getMaxIndirectionsForType(var.getUnspecifiedType())]
187+
[getMinIndirectionsForType(var.getUnspecifiedType()) .. SsaImpl::getMaxIndirectionsForGLType(var.getUnspecifiedType())]
188188
} or
189189
TPostUpdateNodeImpl(Operand operand, int indirectionIndex) {
190190
isPostUpdateNodeImpl(operand, indirectionIndex)
@@ -209,10 +209,7 @@ private module Cached {
209209
TBodyLessParameterNodeImpl(Parameter p, int indirectionIndex) {
210210
// Rule out parameters of catch blocks.
211211
not exists(p.getCatchBlock()) and
212-
// We subtract one because `getMaxIndirectionsForType` returns the maximum
213-
// indirection for a glvalue of a given type, and this doesn't apply to
214-
// parameters.
215-
indirectionIndex = [0 .. SsaImpl::getMaxIndirectionsForType(p.getUnspecifiedType()) - 1] and
212+
indirectionIndex = [0 .. SsaImpl::getMaxIndirectionsForPRType(p.getUnspecifiedType())] and
216213
not any(InitializeParameterInstruction init).getParameter() = p
217214
} or
218215
TFlowSummaryNode(FlowSummaryImpl::Private::SummaryNode sn)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ newtype TPosition =
737737
// the function.
738738
exists(Cpp::Function f, Cpp::Parameter p |
739739
p = f.getParameter(argumentIndex) and
740-
indirectionIndex = [1 .. Ssa::getMaxIndirectionsForType(p.getUnspecifiedType()) - 1]
740+
indirectionIndex = [1 .. Ssa::getMaxIndirectionsForPRType(p.getUnspecifiedType())]
741741
)
742742
} or
743743
TFlowSummaryPosition(ReturnKind rk) { FlowSummaryImpl::Private::relevantFlowSummaryPosition(rk) }
@@ -755,7 +755,7 @@ private newtype TReturnKind =
755755
[0 .. max(Cpp::Function f |
756756
not exists(f.getBlock())
757757
|
758-
Ssa::getMaxIndirectionsForType(f.getUnspecifiedType()) - 1 // -1 because a returned value is a prvalue not a glvalue
758+
Ssa::getMaxIndirectionsForPRType(f.getUnspecifiedType())
759759
)]
760760
} or
761761
TIndirectReturnKind(int argumentIndex, int indirectionIndex) {
@@ -770,7 +770,7 @@ private newtype TReturnKind =
770770
[0 .. max(Cpp::Function f |
771771
not exists(f.getBlock())
772772
|
773-
Ssa::getMaxIndirectionsForType(f.getParameter(argumentIndex).getUnspecifiedType()) - 1 // -1 because an argument is a prvalue not a glvalue
773+
Ssa::getMaxIndirectionsForPRType(f.getParameter(argumentIndex).getUnspecifiedType())
774774
)]
775775
}
776776

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,21 @@ CppType getLanguageType(Operand operand) { result = getResultLanguageType(operan
4040
* - If `type = MyStruct`, the result is 1
4141
* - If `type = char*`, the result is 2
4242
*/
43-
int getMaxIndirectionsForType(Type type) {
43+
int getMaxIndirectionsForGLType(Type type) {
4444
result = countIndirectionsForCppType(getTypeForGLValue(type))
4545
}
4646

47+
/**
48+
* Gets the maximum number of indirections a prvalue of type `type` can have.
49+
* For example:
50+
* - If `type = int`, the result is 0
51+
* - If `type = MyStruct`, the result is 0
52+
* - If `type = char*`, the result is 1
53+
*/
54+
int getMaxIndirectionsForPRType(Type type) {
55+
result = countIndirectionsForCppType(getTypeForPRValue(type))
56+
}
57+
4758
private class PointerOrArrayOrReferenceType extends Cpp::DerivedType {
4859
PointerOrArrayOrReferenceType() {
4960
this instanceof Cpp::PointerType

0 commit comments

Comments
 (0)