From a5601ce7344cc2f8ed5b84948d49549f39681b49 Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Tue, 18 Nov 2025 19:52:05 +0000 Subject: [PATCH 1/5] C++: Lift 'getTypeOperand' to the superclass. --- cpp/ql/lib/semmle/code/cpp/exprs/Cast.qll | 30 ++++++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/cpp/ql/lib/semmle/code/cpp/exprs/Cast.qll b/cpp/ql/lib/semmle/code/cpp/exprs/Cast.qll index 83364e422ebe..e5df5cb160f3 100644 --- a/cpp/ql/lib/semmle/code/cpp/exprs/Cast.qll +++ b/cpp/ql/lib/semmle/code/cpp/exprs/Cast.qll @@ -750,6 +750,16 @@ class SizeofPackTypeOperator extends SizeofPackOperator { */ class SizeofOperator extends Expr, @runtime_sizeof { override int getPrecedence() { result = 16 } + + /** + * Gets the contained type of this `sizeof`. For example, + * the result is `int` in both cases below: + * ``` + * sizeof(int); + * sizeof(42); + * ``` + */ + Type getTypeOperand() { none() } // overridden in subclasses } /** @@ -766,6 +776,8 @@ class SizeofExprOperator extends SizeofOperator { /** Gets the contained expression. */ Expr getExprOperand() { result = this.getChild(0) } + override Type getTypeOperand() { result = this.getExprOperand().getType() } + override string toString() { result = "sizeof()" } override predicate mayBeImpure() { this.getExprOperand().mayBeImpure() } @@ -784,8 +796,7 @@ class SizeofTypeOperator extends SizeofOperator { override string getAPrimaryQlClass() { result = "SizeofTypeOperator" } - /** Gets the contained type. */ - Type getTypeOperand() { sizeof_bind(underlyingElement(this), unresolveElement(result)) } + override Type getTypeOperand() { sizeof_bind(underlyingElement(this), unresolveElement(result)) } override string toString() { result = "sizeof(" + this.getTypeOperand().getName() + ")" } @@ -842,6 +853,16 @@ class AlignofTypeOperator extends AlignofOperator { */ class DatasizeofOperator extends Expr, @datasizeof { override int getPrecedence() { result = 16 } + + /** + * Gets the contained type of this `__datasizeof`. For example, + * the result is `int` in both cases below: + * ``` + * __datasizeof(int); + * __datasizeof(42); + * ``` + */ + Type getTypeOperand() { none() } } /** @@ -855,6 +876,8 @@ class DatasizeofExprOperator extends DatasizeofOperator { /** Gets the contained expression. */ Expr getExprOperand() { result = this.getChild(0) } + override Type getTypeOperand() { result = this.getExprOperand().getType() } + override string toString() { result = "__datasizeof()" } override predicate mayBeImpure() { this.getExprOperand().mayBeImpure() } @@ -870,8 +893,7 @@ class DatasizeofTypeOperator extends DatasizeofOperator { override string getAPrimaryQlClass() { result = "DatasizeofTypeOperator" } - /** Gets the contained type. */ - Type getTypeOperand() { sizeof_bind(underlyingElement(this), unresolveElement(result)) } + override Type getTypeOperand() { sizeof_bind(underlyingElement(this), unresolveElement(result)) } override string toString() { result = "__datasizeof(" + this.getTypeOperand().getName() + ")" } From d4a8dbb5f3f5ac168be028d1b9b6aec9142993d1 Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Tue, 18 Nov 2025 19:52:30 +0000 Subject: [PATCH 2/5] C++: Slightly modify a test so that we can see the effect of this change. --- cpp/ql/test/library-tests/types/sizeof/sizeof.expected | 6 ++++++ cpp/ql/test/library-tests/types/sizeof/sizeof.ql | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/cpp/ql/test/library-tests/types/sizeof/sizeof.expected b/cpp/ql/test/library-tests/types/sizeof/sizeof.expected index c0d075126dfc..1e2963bc93bf 100644 --- a/cpp/ql/test/library-tests/types/sizeof/sizeof.expected +++ b/cpp/ql/test/library-tests/types/sizeof/sizeof.expected @@ -3,8 +3,14 @@ | sizeof.cpp:21:15:21:27 | sizeof(int *) | 8 | SizeofTypeOperator.getTypeOperand() | file://:0:0:0:0 | int * | | sizeof.cpp:22:15:22:29 | sizeof(MyClass) | 16 | SizeofTypeOperator.getTypeOperand() | sizeof.cpp:4:7:4:13 | MyClass | | sizeof.cpp:23:15:23:23 | sizeof() | 4 | SizeofExprOperator.getExprOperand() | sizeof.cpp:23:22:23:22 | i | +| sizeof.cpp:23:15:23:23 | sizeof() | 4 | SizeofTypeOperator.getTypeOperand() | file://:0:0:0:0 | int | | sizeof.cpp:24:15:24:23 | sizeof() | 1 | SizeofExprOperator.getExprOperand() | sizeof.cpp:24:22:24:22 | c | +| sizeof.cpp:24:15:24:23 | sizeof() | 1 | SizeofTypeOperator.getTypeOperand() | file://:0:0:0:0 | char | | sizeof.cpp:25:15:25:25 | sizeof() | 8 | SizeofExprOperator.getExprOperand() | sizeof.cpp:25:22:25:24 | ptr | +| sizeof.cpp:25:15:25:25 | sizeof() | 8 | SizeofTypeOperator.getTypeOperand() | file://:0:0:0:0 | int * | | sizeof.cpp:26:15:26:24 | sizeof() | 16 | SizeofExprOperator.getExprOperand() | sizeof.cpp:26:22:26:23 | mc | +| sizeof.cpp:26:15:26:24 | sizeof() | 16 | SizeofTypeOperator.getTypeOperand() | sizeof.cpp:4:7:4:13 | MyClass | | sizeof.cpp:27:15:27:25 | sizeof() | 40 | SizeofExprOperator.getExprOperand() | sizeof.cpp:27:22:27:24 | arr | +| sizeof.cpp:27:15:27:25 | sizeof() | 40 | SizeofTypeOperator.getTypeOperand() | file://:0:0:0:0 | int[10] | | sizeof.cpp:28:16:28:29 | sizeof() | 4 | SizeofExprOperator.getExprOperand() | sizeof.cpp:28:23:28:28 | access to array | +| sizeof.cpp:28:16:28:29 | sizeof() | 4 | SizeofTypeOperator.getTypeOperand() | file://:0:0:0:0 | int | diff --git a/cpp/ql/test/library-tests/types/sizeof/sizeof.ql b/cpp/ql/test/library-tests/types/sizeof/sizeof.ql index 531f55ee45c8..0ec6711adb89 100644 --- a/cpp/ql/test/library-tests/types/sizeof/sizeof.ql +++ b/cpp/ql/test/library-tests/types/sizeof/sizeof.ql @@ -3,7 +3,7 @@ import cpp from SizeofOperator sto, string elemDesc, Element e where elemDesc = "SizeofTypeOperator.getTypeOperand()" and - e = sto.(SizeofTypeOperator).getTypeOperand() + e = sto.(SizeofOperator).getTypeOperand() or elemDesc = "SizeofExprOperator.getExprOperand()" and e = sto.(SizeofExprOperator).getExprOperand() From 6b136e3a53229352370d314aa4207d6c89ccd298 Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Tue, 18 Nov 2025 20:00:32 +0000 Subject: [PATCH 3/5] Update cpp/ql/test/library-tests/types/sizeof/sizeof.ql Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- cpp/ql/test/library-tests/types/sizeof/sizeof.ql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/ql/test/library-tests/types/sizeof/sizeof.ql b/cpp/ql/test/library-tests/types/sizeof/sizeof.ql index 0ec6711adb89..46d78ebbe602 100644 --- a/cpp/ql/test/library-tests/types/sizeof/sizeof.ql +++ b/cpp/ql/test/library-tests/types/sizeof/sizeof.ql @@ -2,7 +2,7 @@ import cpp from SizeofOperator sto, string elemDesc, Element e where - elemDesc = "SizeofTypeOperator.getTypeOperand()" and + elemDesc = "SizeofOperator.getTypeOperand()" and e = sto.(SizeofOperator).getTypeOperand() or elemDesc = "SizeofExprOperator.getExprOperand()" and From a27ac9d59dc47d49ddfd5d64dba052a4e85e5d35 Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Tue, 18 Nov 2025 20:03:10 +0000 Subject: [PATCH 4/5] C++: Updated expected after Copilot change. --- .../types/sizeof/sizeof.expected | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/cpp/ql/test/library-tests/types/sizeof/sizeof.expected b/cpp/ql/test/library-tests/types/sizeof/sizeof.expected index 1e2963bc93bf..ac55caf5a021 100644 --- a/cpp/ql/test/library-tests/types/sizeof/sizeof.expected +++ b/cpp/ql/test/library-tests/types/sizeof/sizeof.expected @@ -1,16 +1,16 @@ -| sizeof.cpp:19:15:19:25 | sizeof(int) | 4 | SizeofTypeOperator.getTypeOperand() | file://:0:0:0:0 | int | -| sizeof.cpp:20:15:20:26 | sizeof(char) | 1 | SizeofTypeOperator.getTypeOperand() | file://:0:0:0:0 | char | -| sizeof.cpp:21:15:21:27 | sizeof(int *) | 8 | SizeofTypeOperator.getTypeOperand() | file://:0:0:0:0 | int * | -| sizeof.cpp:22:15:22:29 | sizeof(MyClass) | 16 | SizeofTypeOperator.getTypeOperand() | sizeof.cpp:4:7:4:13 | MyClass | +| sizeof.cpp:19:15:19:25 | sizeof(int) | 4 | SizeofOperator.getTypeOperand() | file://:0:0:0:0 | int | +| sizeof.cpp:20:15:20:26 | sizeof(char) | 1 | SizeofOperator.getTypeOperand() | file://:0:0:0:0 | char | +| sizeof.cpp:21:15:21:27 | sizeof(int *) | 8 | SizeofOperator.getTypeOperand() | file://:0:0:0:0 | int * | +| sizeof.cpp:22:15:22:29 | sizeof(MyClass) | 16 | SizeofOperator.getTypeOperand() | sizeof.cpp:4:7:4:13 | MyClass | | sizeof.cpp:23:15:23:23 | sizeof() | 4 | SizeofExprOperator.getExprOperand() | sizeof.cpp:23:22:23:22 | i | -| sizeof.cpp:23:15:23:23 | sizeof() | 4 | SizeofTypeOperator.getTypeOperand() | file://:0:0:0:0 | int | +| sizeof.cpp:23:15:23:23 | sizeof() | 4 | SizeofOperator.getTypeOperand() | file://:0:0:0:0 | int | | sizeof.cpp:24:15:24:23 | sizeof() | 1 | SizeofExprOperator.getExprOperand() | sizeof.cpp:24:22:24:22 | c | -| sizeof.cpp:24:15:24:23 | sizeof() | 1 | SizeofTypeOperator.getTypeOperand() | file://:0:0:0:0 | char | +| sizeof.cpp:24:15:24:23 | sizeof() | 1 | SizeofOperator.getTypeOperand() | file://:0:0:0:0 | char | | sizeof.cpp:25:15:25:25 | sizeof() | 8 | SizeofExprOperator.getExprOperand() | sizeof.cpp:25:22:25:24 | ptr | -| sizeof.cpp:25:15:25:25 | sizeof() | 8 | SizeofTypeOperator.getTypeOperand() | file://:0:0:0:0 | int * | +| sizeof.cpp:25:15:25:25 | sizeof() | 8 | SizeofOperator.getTypeOperand() | file://:0:0:0:0 | int * | | sizeof.cpp:26:15:26:24 | sizeof() | 16 | SizeofExprOperator.getExprOperand() | sizeof.cpp:26:22:26:23 | mc | -| sizeof.cpp:26:15:26:24 | sizeof() | 16 | SizeofTypeOperator.getTypeOperand() | sizeof.cpp:4:7:4:13 | MyClass | +| sizeof.cpp:26:15:26:24 | sizeof() | 16 | SizeofOperator.getTypeOperand() | sizeof.cpp:4:7:4:13 | MyClass | | sizeof.cpp:27:15:27:25 | sizeof() | 40 | SizeofExprOperator.getExprOperand() | sizeof.cpp:27:22:27:24 | arr | -| sizeof.cpp:27:15:27:25 | sizeof() | 40 | SizeofTypeOperator.getTypeOperand() | file://:0:0:0:0 | int[10] | +| sizeof.cpp:27:15:27:25 | sizeof() | 40 | SizeofOperator.getTypeOperand() | file://:0:0:0:0 | int[10] | | sizeof.cpp:28:16:28:29 | sizeof() | 4 | SizeofExprOperator.getExprOperand() | sizeof.cpp:28:23:28:28 | access to array | -| sizeof.cpp:28:16:28:29 | sizeof() | 4 | SizeofTypeOperator.getTypeOperand() | file://:0:0:0:0 | int | +| sizeof.cpp:28:16:28:29 | sizeof() | 4 | SizeofOperator.getTypeOperand() | file://:0:0:0:0 | int | From 4279a970fa8dd14b555775bbb0746feef8212d54 Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Tue, 18 Nov 2025 20:03:24 +0000 Subject: [PATCH 5/5] C++: Remove unnecessary cast. --- cpp/ql/test/library-tests/types/sizeof/sizeof.ql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/ql/test/library-tests/types/sizeof/sizeof.ql b/cpp/ql/test/library-tests/types/sizeof/sizeof.ql index 46d78ebbe602..31834e0180fe 100644 --- a/cpp/ql/test/library-tests/types/sizeof/sizeof.ql +++ b/cpp/ql/test/library-tests/types/sizeof/sizeof.ql @@ -3,7 +3,7 @@ import cpp from SizeofOperator sto, string elemDesc, Element e where elemDesc = "SizeofOperator.getTypeOperand()" and - e = sto.(SizeofOperator).getTypeOperand() + e = sto.getTypeOperand() or elemDesc = "SizeofExprOperator.getExprOperand()" and e = sto.(SizeofExprOperator).getExprOperand()