[clang] [clang] Fix static_cast bypassing access control (PR #132285)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Mar 22 12:57:27 PDT 2025
================
@@ -23,3 +22,67 @@ void test(A &a, B &b) {
const A &&ar10 = static_cast<const A&&>(xvalue<A>());
const A &&ar11 = static_cast<const A&&>(xvalue<B>());
}
+
+struct C : private A { // expected-note 4 {{declared private here}}
+ C&& that();
+
+ void f() {
+ (void)static_cast<A&&>(*this);
+ (void)static_cast<const A&&>(*this);
+
+ (void)static_cast<A&&>(that());
+ (void)static_cast<const A&&>(that());
+ }
+};
+C c;
+const C cc;
+
+void f() {
+ static_cast<A&&>(c); // expected-error {{cannot cast 'C' to its private base class 'A'}}
+ static_cast<A&&>(c.that()); // expected-error {{cannot cast 'C' to its private base class 'A'}}
+
+ static_cast<const A&&>(c); // expected-error {{cannot cast 'C' to its private base class 'const A'}}
+ static_cast<const A&&>(c.that()); // expected-error {{cannot cast 'C' to its private base class 'const A'}}
+}
+
+constexpr auto v = (
+ (A&&)c,
+ (A&&)(C&&)c,
+ (A&&)cc,
+ (A&&)(const C&&)c,
+ (const A&&)c,
+ (const A&&)(C&&)c,
+ (const A&&)cc,
+ (const A&&)(const C&&)c
+);
----------------
Sirraide wrote:
```suggestion
constexpr bool g() {
(A&&)c;
(A&&)(C&&)c;
(A&&)cc;
(A&&)(const C&&)c;
(const A&&)c;
(const A&&)(C&&)c;
(const A&&)cc;
(const A&&)(const C&&)c;
return true;
}
static_assert(g());
```
This here is a bit gross; I’d probably move all of thse into a function instead (you may have to add more `(void)` cases or alternatively just put `-Wno-unused-value` in the RUN line honestly).
https://github.com/llvm/llvm-project/pull/132285
More information about the cfe-commits
mailing list