Skip to content

Commit 710eb0c

Browse files
committed
C++: Replace "min = max" with "unique"
With the new `unique` aggregate added to QL, we can express directly what the "min = max" pattern emulates. Replacing "min and max" with `unique` might in general lead to fewer results, but that happens only in cases where the aggregate expression has multiple values. For the three predicates changed in this commit, that should only happen on malformed databases.
1 parent 875a70c commit 710eb0c

File tree

5 files changed

+5
-20
lines changed

5 files changed

+5
-20
lines changed

cpp/ql/src/semmle/code/cpp/commons/Buffer.qll

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,7 @@ int getBufferSize(Expr bufferExpr, Element why) {
9292
// dataflow (all sources must be the same size)
9393
bufferExprNode = DataFlow::exprNode(bufferExpr) and
9494
result =
95-
min(Expr def |
96-
DataFlow::localFlowStep(DataFlow::exprNode(def), bufferExprNode)
97-
|
98-
getBufferSize(def, _)
99-
) and
100-
result =
101-
max(Expr def |
95+
unique(Expr def |
10296
DataFlow::localFlowStep(DataFlow::exprNode(def), bufferExprNode)
10397
|
10498
getBufferSize(def, _)

cpp/ql/src/semmle/code/cpp/controlflow/internal/ConstantExprs.qll

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -532,13 +532,7 @@ library class ExprEvaluator extends int {
532532
interestingVariableAccess(e, va, v, true) and
533533
// All assignments must have the same int value
534534
result =
535-
min(Expr value |
536-
value = v.getAnAssignedValue() and not ignoreVariableAssignment(e, v, value)
537-
|
538-
getValueInternalNonSubExpr(value)
539-
) and
540-
result =
541-
max(Expr value |
535+
unique(Expr value |
542536
value = v.getAnAssignedValue() and not ignoreVariableAssignment(e, v, value)
543537
|
544538
getValueInternalNonSubExpr(value)

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/constant/ConstantAnalysis.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ int getConstantValue(Instruction instr) {
1414
or
1515
exists(PhiInstruction phi |
1616
phi = instr and
17-
result = max(Operand op | op = phi.getAnInputOperand() | getConstantValue(op.getDef())) and
18-
result = min(Operand op | op = phi.getAnInputOperand() | getConstantValue(op.getDef()))
17+
result = unique(Operand op | op = phi.getAnInputOperand() | getConstantValue(op.getDef()))
1918
)
2019
}
2120

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/constant/ConstantAnalysis.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ int getConstantValue(Instruction instr) {
1414
or
1515
exists(PhiInstruction phi |
1616
phi = instr and
17-
result = max(Operand op | op = phi.getAnInputOperand() | getConstantValue(op.getDef())) and
18-
result = min(Operand op | op = phi.getAnInputOperand() | getConstantValue(op.getDef()))
17+
result = unique(Operand op | op = phi.getAnInputOperand() | getConstantValue(op.getDef()))
1918
)
2019
}
2120

cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/constant/ConstantAnalysis.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ int getConstantValue(Instruction instr) {
1414
or
1515
exists(PhiInstruction phi |
1616
phi = instr and
17-
result = max(Operand op | op = phi.getAnInputOperand() | getConstantValue(op.getDef())) and
18-
result = min(Operand op | op = phi.getAnInputOperand() | getConstantValue(op.getDef()))
17+
result = unique(Operand op | op = phi.getAnInputOperand() | getConstantValue(op.getDef()))
1918
)
2019
}
2120

0 commit comments

Comments
 (0)