From 3f595fdd0370ea55c6d2f04b4c9310cf2a66a2f4 Mon Sep 17 00:00:00 2001 From: idrissrio Date: Tue, 17 Jun 2025 18:47:42 +0200 Subject: [PATCH 1/6] C++: add tests for `ArrayType` typedef resolution --- cpp/ql/test/library-tests/typedefs/ArrayTypedefs.cpp | 7 +++++++ cpp/ql/test/library-tests/typedefs/ArrayTypedefs.expected | 5 +++++ cpp/ql/test/library-tests/typedefs/ArrayTypedefs.ql | 4 ++++ 3 files changed, 16 insertions(+) create mode 100644 cpp/ql/test/library-tests/typedefs/ArrayTypedefs.cpp create mode 100644 cpp/ql/test/library-tests/typedefs/ArrayTypedefs.expected create mode 100644 cpp/ql/test/library-tests/typedefs/ArrayTypedefs.ql diff --git a/cpp/ql/test/library-tests/typedefs/ArrayTypedefs.cpp b/cpp/ql/test/library-tests/typedefs/ArrayTypedefs.cpp new file mode 100644 index 000000000000..393ab4c7d9ea --- /dev/null +++ b/cpp/ql/test/library-tests/typedefs/ArrayTypedefs.cpp @@ -0,0 +1,7 @@ +typedef int int_t; +int_t g1[10]; +int_t g2[2][4]; + +typedef float float_t; +float_t arr1[5]; +float_t (*a_pointer)[10]; diff --git a/cpp/ql/test/library-tests/typedefs/ArrayTypedefs.expected b/cpp/ql/test/library-tests/typedefs/ArrayTypedefs.expected new file mode 100644 index 000000000000..9b91579e8dcb --- /dev/null +++ b/cpp/ql/test/library-tests/typedefs/ArrayTypedefs.expected @@ -0,0 +1,5 @@ +| file://:0:0:0:0 | float_t[5] | file://:0:0:0:0 | float_t[5] | ArrayTypedefs.cpp:6:9:6:12 | definition of arr1 | +| file://:0:0:0:0 | float_t[10] | file://:0:0:0:0 | float_t[10] | ArrayTypedefs.cpp:7:11:7:19 | definition of a_pointer | +| file://:0:0:0:0 | int_t[2][4] | file://:0:0:0:0 | int_t[2][4] | ArrayTypedefs.cpp:3:7:3:8 | definition of g2 | +| file://:0:0:0:0 | int_t[4] | file://:0:0:0:0 | int_t[4] | ArrayTypedefs.cpp:3:7:3:8 | definition of g2 | +| file://:0:0:0:0 | int_t[10] | file://:0:0:0:0 | int_t[10] | ArrayTypedefs.cpp:2:7:2:8 | definition of g1 | diff --git a/cpp/ql/test/library-tests/typedefs/ArrayTypedefs.ql b/cpp/ql/test/library-tests/typedefs/ArrayTypedefs.ql new file mode 100644 index 000000000000..73ebe35d8ff7 --- /dev/null +++ b/cpp/ql/test/library-tests/typedefs/ArrayTypedefs.ql @@ -0,0 +1,4 @@ +import cpp + +from ArrayType type +select type, type.resolveTypedefs(), type.getATypeNameUse() From a6d7fb6aad5fe6ef7e06b18b74ca0e63e87a47ee Mon Sep 17 00:00:00 2001 From: idrissrio Date: Tue, 17 Jun 2025 18:49:39 +0200 Subject: [PATCH 2/6] C++: fix typedef resolution in `ArrayType` --- cpp/ql/lib/semmle/code/cpp/Type.qll | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cpp/ql/lib/semmle/code/cpp/Type.qll b/cpp/ql/lib/semmle/code/cpp/Type.qll index fef978b198d6..3db3cd49e54b 100644 --- a/cpp/ql/lib/semmle/code/cpp/Type.qll +++ b/cpp/ql/lib/semmle/code/cpp/Type.qll @@ -1589,6 +1589,11 @@ class ArrayType extends DerivedType { * Holds if this array is a variable-length array (VLA). */ predicate isVla() { type_is_vla(underlyingElement(this)) } + + override Type resolveTypedefs() { + result.(ArrayType).getBaseType() = this.getBaseType().resolveTypedefs() and + result.(ArrayType).getArraySize() = this.getArraySize() + } } /** From 6ffba11f134cffcd110166a3de0c21731abc5626 Mon Sep 17 00:00:00 2001 From: idrissrio Date: Tue, 17 Jun 2025 18:51:43 +0200 Subject: [PATCH 3/6] C++: accept new test results after changes --- .../test/library-tests/typedefs/ArrayTypedefs.expected | 10 +++++----- cpp/ql/test/library-tests/typedefs/Typedefs1.expected | 6 ++++++ cpp/ql/test/library-tests/typedefs/Typedefs3.expected | 2 ++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cpp/ql/test/library-tests/typedefs/ArrayTypedefs.expected b/cpp/ql/test/library-tests/typedefs/ArrayTypedefs.expected index 9b91579e8dcb..83e003b5bb15 100644 --- a/cpp/ql/test/library-tests/typedefs/ArrayTypedefs.expected +++ b/cpp/ql/test/library-tests/typedefs/ArrayTypedefs.expected @@ -1,5 +1,5 @@ -| file://:0:0:0:0 | float_t[5] | file://:0:0:0:0 | float_t[5] | ArrayTypedefs.cpp:6:9:6:12 | definition of arr1 | -| file://:0:0:0:0 | float_t[10] | file://:0:0:0:0 | float_t[10] | ArrayTypedefs.cpp:7:11:7:19 | definition of a_pointer | -| file://:0:0:0:0 | int_t[2][4] | file://:0:0:0:0 | int_t[2][4] | ArrayTypedefs.cpp:3:7:3:8 | definition of g2 | -| file://:0:0:0:0 | int_t[4] | file://:0:0:0:0 | int_t[4] | ArrayTypedefs.cpp:3:7:3:8 | definition of g2 | -| file://:0:0:0:0 | int_t[10] | file://:0:0:0:0 | int_t[10] | ArrayTypedefs.cpp:2:7:2:8 | definition of g1 | +| file://:0:0:0:0 | float_t[5] | file://:0:0:0:0 | float[5] | ArrayTypedefs.cpp:6:9:6:12 | definition of arr1 | +| file://:0:0:0:0 | float_t[10] | file://:0:0:0:0 | float[10] | ArrayTypedefs.cpp:7:11:7:19 | definition of a_pointer | +| file://:0:0:0:0 | int_t[2][4] | file://:0:0:0:0 | int[2][4] | ArrayTypedefs.cpp:3:7:3:8 | definition of g2 | +| file://:0:0:0:0 | int_t[4] | file://:0:0:0:0 | int[4] | ArrayTypedefs.cpp:3:7:3:8 | definition of g2 | +| file://:0:0:0:0 | int_t[10] | file://:0:0:0:0 | int[10] | ArrayTypedefs.cpp:2:7:2:8 | definition of g1 | diff --git a/cpp/ql/test/library-tests/typedefs/Typedefs1.expected b/cpp/ql/test/library-tests/typedefs/Typedefs1.expected index 21b506c015a4..f64afc49ccd1 100644 --- a/cpp/ql/test/library-tests/typedefs/Typedefs1.expected +++ b/cpp/ql/test/library-tests/typedefs/Typedefs1.expected @@ -1,3 +1,9 @@ +| file://:0:0:0:0 | (global namespace) | ArrayTypedefs.cpp:1:13:1:17 | int_t | +| file://:0:0:0:0 | (global namespace) | ArrayTypedefs.cpp:2:7:2:8 | g1 | +| file://:0:0:0:0 | (global namespace) | ArrayTypedefs.cpp:3:7:3:8 | g2 | +| file://:0:0:0:0 | (global namespace) | ArrayTypedefs.cpp:5:15:5:21 | float_t | +| file://:0:0:0:0 | (global namespace) | ArrayTypedefs.cpp:6:9:6:12 | arr1 | +| file://:0:0:0:0 | (global namespace) | ArrayTypedefs.cpp:7:11:7:19 | a_pointer | | file://:0:0:0:0 | (global namespace) | ODASA-6095-A.hpp:2:8:2:17 | MyTemplate | | file://:0:0:0:0 | (global namespace) | ODASA-6095-A.hpp:2:8:2:17 | MyTemplate | | file://:0:0:0:0 | (global namespace) | ODASA-6095-B.hpp:4:8:4:18 | MyTemplate2 | diff --git a/cpp/ql/test/library-tests/typedefs/Typedefs3.expected b/cpp/ql/test/library-tests/typedefs/Typedefs3.expected index c0f584a04e64..1cd9bc34f15e 100644 --- a/cpp/ql/test/library-tests/typedefs/Typedefs3.expected +++ b/cpp/ql/test/library-tests/typedefs/Typedefs3.expected @@ -1,3 +1,5 @@ +| ArrayTypedefs.cpp:1:13:1:17 | int_t | int_t | getBaseType() = int | +| ArrayTypedefs.cpp:5:15:5:21 | float_t | float_t | getBaseType() = float | | ODASA-6095-A.hpp:5:25:5:30 | mytype | MyTemplate::mytype | NestedTypedefType, getBaseType() = MyTemplate, member of MyTemplate | | ODASA-6095-B.hpp:6:43:6:49 | mytype2 | MyTemplate2::mytype2 | NestedTypedefType, getBaseType() = decltype(...), member of MyTemplate2 | | file://:0:0:0:0 | mytype | MyTemplate::mytype | NestedTypedefType, getBaseType() = MyTemplate, member of MyTemplate | From b145523a717bdfcf7089af6f8fc9e8ccaadaf2c8 Mon Sep 17 00:00:00 2001 From: idrissrio Date: Tue, 17 Jun 2025 19:00:37 +0200 Subject: [PATCH 4/6] C++: add change note --- cpp/ql/lib/change-notes/2025-06-17-arraytype-typedefs.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 cpp/ql/lib/change-notes/2025-06-17-arraytype-typedefs.md diff --git a/cpp/ql/lib/change-notes/2025-06-17-arraytype-typedefs.md b/cpp/ql/lib/change-notes/2025-06-17-arraytype-typedefs.md new file mode 100644 index 000000000000..e4e6e732cc7f --- /dev/null +++ b/cpp/ql/lib/change-notes/2025-06-17-arraytype-typedefs.md @@ -0,0 +1,4 @@ +--- +category: fix +--- +* Fixed how typedefs are resolved for ArrayType by overriding the `resolveTypedefs` predicate. From 4ec64021e10970c1a1a985a7b1f0b172a29d525d Mon Sep 17 00:00:00 2001 From: idrissrio Date: Wed, 18 Jun 2025 09:49:12 +0200 Subject: [PATCH 5/6] C++: update change note after review comments --- cpp/ql/lib/change-notes/2025-06-17-arraytype-typedefs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/ql/lib/change-notes/2025-06-17-arraytype-typedefs.md b/cpp/ql/lib/change-notes/2025-06-17-arraytype-typedefs.md index e4e6e732cc7f..0bc3130e6a3d 100644 --- a/cpp/ql/lib/change-notes/2025-06-17-arraytype-typedefs.md +++ b/cpp/ql/lib/change-notes/2025-06-17-arraytype-typedefs.md @@ -1,4 +1,4 @@ --- category: fix --- -* Fixed how typedefs are resolved for ArrayType by overriding the `resolveTypedefs` predicate. +* `resolveTypedefs` now properly resolves typedefs for `ArrayType`s. From 72559d5198a2bdb4d0d5ac89e84eebf66d66c315 Mon Sep 17 00:00:00 2001 From: idrissrio Date: Wed, 18 Jun 2025 09:50:54 +0200 Subject: [PATCH 6/6] C++: move `resolveTypedefs` tests to dedicated directory --- .../{typedefs => resolve_typedefs}/ArrayTypedefs.cpp | 0 .../{typedefs => resolve_typedefs}/ArrayTypedefs.expected | 0 .../{typedefs => resolve_typedefs}/ArrayTypedefs.ql | 0 cpp/ql/test/library-tests/typedefs/Typedefs1.expected | 6 ------ cpp/ql/test/library-tests/typedefs/Typedefs3.expected | 2 -- 5 files changed, 8 deletions(-) rename cpp/ql/test/library-tests/{typedefs => resolve_typedefs}/ArrayTypedefs.cpp (100%) rename cpp/ql/test/library-tests/{typedefs => resolve_typedefs}/ArrayTypedefs.expected (100%) rename cpp/ql/test/library-tests/{typedefs => resolve_typedefs}/ArrayTypedefs.ql (100%) diff --git a/cpp/ql/test/library-tests/typedefs/ArrayTypedefs.cpp b/cpp/ql/test/library-tests/resolve_typedefs/ArrayTypedefs.cpp similarity index 100% rename from cpp/ql/test/library-tests/typedefs/ArrayTypedefs.cpp rename to cpp/ql/test/library-tests/resolve_typedefs/ArrayTypedefs.cpp diff --git a/cpp/ql/test/library-tests/typedefs/ArrayTypedefs.expected b/cpp/ql/test/library-tests/resolve_typedefs/ArrayTypedefs.expected similarity index 100% rename from cpp/ql/test/library-tests/typedefs/ArrayTypedefs.expected rename to cpp/ql/test/library-tests/resolve_typedefs/ArrayTypedefs.expected diff --git a/cpp/ql/test/library-tests/typedefs/ArrayTypedefs.ql b/cpp/ql/test/library-tests/resolve_typedefs/ArrayTypedefs.ql similarity index 100% rename from cpp/ql/test/library-tests/typedefs/ArrayTypedefs.ql rename to cpp/ql/test/library-tests/resolve_typedefs/ArrayTypedefs.ql diff --git a/cpp/ql/test/library-tests/typedefs/Typedefs1.expected b/cpp/ql/test/library-tests/typedefs/Typedefs1.expected index f64afc49ccd1..21b506c015a4 100644 --- a/cpp/ql/test/library-tests/typedefs/Typedefs1.expected +++ b/cpp/ql/test/library-tests/typedefs/Typedefs1.expected @@ -1,9 +1,3 @@ -| file://:0:0:0:0 | (global namespace) | ArrayTypedefs.cpp:1:13:1:17 | int_t | -| file://:0:0:0:0 | (global namespace) | ArrayTypedefs.cpp:2:7:2:8 | g1 | -| file://:0:0:0:0 | (global namespace) | ArrayTypedefs.cpp:3:7:3:8 | g2 | -| file://:0:0:0:0 | (global namespace) | ArrayTypedefs.cpp:5:15:5:21 | float_t | -| file://:0:0:0:0 | (global namespace) | ArrayTypedefs.cpp:6:9:6:12 | arr1 | -| file://:0:0:0:0 | (global namespace) | ArrayTypedefs.cpp:7:11:7:19 | a_pointer | | file://:0:0:0:0 | (global namespace) | ODASA-6095-A.hpp:2:8:2:17 | MyTemplate | | file://:0:0:0:0 | (global namespace) | ODASA-6095-A.hpp:2:8:2:17 | MyTemplate | | file://:0:0:0:0 | (global namespace) | ODASA-6095-B.hpp:4:8:4:18 | MyTemplate2 | diff --git a/cpp/ql/test/library-tests/typedefs/Typedefs3.expected b/cpp/ql/test/library-tests/typedefs/Typedefs3.expected index 1cd9bc34f15e..c0f584a04e64 100644 --- a/cpp/ql/test/library-tests/typedefs/Typedefs3.expected +++ b/cpp/ql/test/library-tests/typedefs/Typedefs3.expected @@ -1,5 +1,3 @@ -| ArrayTypedefs.cpp:1:13:1:17 | int_t | int_t | getBaseType() = int | -| ArrayTypedefs.cpp:5:15:5:21 | float_t | float_t | getBaseType() = float | | ODASA-6095-A.hpp:5:25:5:30 | mytype | MyTemplate::mytype | NestedTypedefType, getBaseType() = MyTemplate, member of MyTemplate | | ODASA-6095-B.hpp:6:43:6:49 | mytype2 | MyTemplate2::mytype2 | NestedTypedefType, getBaseType() = decltype(...), member of MyTemplate2 | | file://:0:0:0:0 | mytype | MyTemplate::mytype | NestedTypedefType, getBaseType() = MyTemplate, member of MyTemplate |