Skip to content

Commit 38b1005

Browse files
committed
C++: Only add conversion when necessary.
1 parent 53561b7 commit 38b1005

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedExpr.qll

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,8 +1906,10 @@ class TranslatedNonConstantAllocationSize extends TranslatedAllocationSize {
19061906
final override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
19071907
resultType = getTypeForPRValue(expr.getAllocator().getParameter(0).getType()) and
19081908
(
1909+
this.extentNeedsConversion() and
19091910
// Convert the extent to `size_t`, because the AST doesn't do this already.
1910-
tag = AllocationExtentConvertTag() and opcode instanceof Opcode::Convert
1911+
tag = AllocationExtentConvertTag() and
1912+
opcode instanceof Opcode::Convert
19111913
or
19121914
tag = AllocationElementSizeTag() and opcode instanceof Opcode::Constant
19131915
or
@@ -1918,6 +1920,7 @@ class TranslatedNonConstantAllocationSize extends TranslatedAllocationSize {
19181920
final override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) {
19191921
kind instanceof GotoEdge and
19201922
(
1923+
this.extentNeedsConversion() and
19211924
tag = AllocationExtentConvertTag() and
19221925
result = this.getInstruction(AllocationElementSizeTag())
19231926
or
@@ -1933,7 +1936,9 @@ class TranslatedNonConstantAllocationSize extends TranslatedAllocationSize {
19331936

19341937
final override Instruction getChildSuccessor(TranslatedElement child) {
19351938
child = this.getExtent() and
1936-
result = this.getInstruction(AllocationExtentConvertTag())
1939+
if this.extentNeedsConversion()
1940+
then result = this.getInstruction(AllocationExtentConvertTag())
1941+
else result = this.getInstruction(AllocationElementSizeTag())
19371942
}
19381943

19391944
final override string getInstructionConstantValue(InstructionTag tag) {
@@ -1945,18 +1950,32 @@ class TranslatedNonConstantAllocationSize extends TranslatedAllocationSize {
19451950
tag = AllocationSizeTag() and
19461951
(
19471952
operandTag instanceof LeftOperandTag and
1948-
result = this.getInstruction(AllocationExtentConvertTag())
1953+
(
1954+
if this.extentNeedsConversion()
1955+
then result = this.getInstruction(AllocationExtentConvertTag())
1956+
else result = this.getExtent().getResult()
1957+
)
19491958
or
19501959
operandTag instanceof RightOperandTag and
19511960
result = this.getInstruction(AllocationElementSizeTag())
19521961
)
19531962
or
1963+
this.extentNeedsConversion() and
19541964
tag = AllocationExtentConvertTag() and
19551965
operandTag instanceof UnaryOperandTag and
19561966
result = this.getExtent().getResult()
19571967
}
19581968

19591969
TranslatedExpr getExtent() { result = getTranslatedExpr(expr.getExtent().getFullyConverted()) }
1970+
1971+
/**
1972+
* Holds if the result of `expr.getExtent()` does not have the same type as
1973+
* the allocator's size parameter.
1974+
*/
1975+
private predicate extentNeedsConversion() {
1976+
expr.getExtent().getFullyConverted().getUnspecifiedType() !=
1977+
expr.getAllocator().getParameter(0).getUnspecifiedType()
1978+
}
19601979
}
19611980

19621981
/**

0 commit comments

Comments
 (0)