Skip to content

Commit 67c9c56

Browse files
volkmhenryiii
andauthored
fix: fully qualify usages of concat to protect against ADL (#4955)
* Call concat with proper namespace in cast.h * Apply suggestions from code review * tests: add test for ADL on concat Signed-off-by: Henry Schreiner <[email protected]> * fix: fully qualify all usages of concat Signed-off-by: Henry Schreiner <[email protected]> --------- Signed-off-by: Henry Schreiner <[email protected]> Co-authored-by: Henry Schreiner <[email protected]>
1 parent 0efff79 commit 67c9c56

File tree

7 files changed

+31
-13
lines changed

7 files changed

+31
-13
lines changed

include/pybind11/cast.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,9 @@ class tuple_caster {
672672
return cast(*src, policy, parent);
673673
}
674674

675-
static constexpr auto name
676-
= const_name("tuple[") + concat(make_caster<Ts>::name...) + const_name("]");
675+
static constexpr auto name = const_name("tuple[")
676+
+ ::pybind11::detail::concat(make_caster<Ts>::name...)
677+
+ const_name("]");
677678

678679
template <typename T>
679680
using cast_op_type = type;
@@ -1569,7 +1570,8 @@ class argument_loader {
15691570
static_assert(args_pos == -1 || args_pos == constexpr_first<argument_is_args, Args...>(),
15701571
"py::args cannot be specified more than once");
15711572

1572-
static constexpr auto arg_names = concat(type_descr(make_caster<Args>::name)...);
1573+
static constexpr auto arg_names
1574+
= ::pybind11::detail::concat(type_descr(make_caster<Args>::name)...);
15731575

15741576
bool load_args(function_call &call) { return load_impl_sequence(call, indices{}); }
15751577

include/pybind11/eigen/tensor.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ struct eigen_tensor_helper<Eigen::Tensor<Scalar_, NumIndices_, Options_, IndexTy
7070

7171
template <size_t... Is>
7272
struct helper<index_sequence<Is...>> {
73-
static constexpr auto value = concat(const_name(((void) Is, "?"))...);
73+
static constexpr auto value = ::pybind11::detail::concat(const_name(((void) Is, "?"))...);
7474
};
7575

7676
static constexpr auto dimensions_descriptor
@@ -104,7 +104,8 @@ struct eigen_tensor_helper<
104104
return get_shape() == shape;
105105
}
106106

107-
static constexpr auto dimensions_descriptor = concat(const_name<Indices>()...);
107+
static constexpr auto dimensions_descriptor
108+
= ::pybind11::detail::concat(const_name<Indices>()...);
108109

109110
template <typename... Args>
110111
static Type *alloc(Args &&...args) {

include/pybind11/functional.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ struct type_caster<std::function<Return(Args...)>> {
128128
}
129129

130130
PYBIND11_TYPE_CASTER(type,
131-
const_name("Callable[[") + concat(make_caster<Args>::name...)
131+
const_name("Callable[[")
132+
+ ::pybind11::detail::concat(make_caster<Args>::name...)
132133
+ const_name("], ") + make_caster<retval_type>::name
133134
+ const_name("]"));
134135
};

include/pybind11/numpy.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ struct array_info<std::array<T, N>> {
446446
}
447447

448448
static constexpr auto extents = const_name<array_info<T>::is_array>(
449-
concat(const_name<N>(), array_info<T>::extents), const_name<N>());
449+
::pybind11::detail::concat(const_name<N>(), array_info<T>::extents), const_name<N>());
450450
};
451451
// For numpy we have special handling for arrays of characters, so we don't include
452452
// the size in the array extents.

include/pybind11/stl.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ struct variant_caster<V<Ts...>> {
421421

422422
using Type = V<Ts...>;
423423
PYBIND11_TYPE_CASTER(Type,
424-
const_name("Union[") + detail::concat(make_caster<Ts>::name...)
424+
const_name("Union[")
425+
+ ::pybind11::detail::concat(make_caster<Ts>::name...)
425426
+ const_name("]"));
426427
};
427428

include/pybind11/typing.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ PYBIND11_NAMESPACE_BEGIN(detail)
6969

7070
template <typename... Types>
7171
struct handle_type_name<typing::Tuple<Types...>> {
72-
static constexpr auto name
73-
= const_name("tuple[") + concat(make_caster<Types>::name...) + const_name("]");
72+
static constexpr auto name = const_name("tuple[")
73+
+ ::pybind11::detail::concat(make_caster<Types>::name...)
74+
+ const_name("]");
7475
};
7576

7677
template <>
@@ -115,9 +116,9 @@ struct handle_type_name<typing::Iterator<T>> {
115116
template <typename Return, typename... Args>
116117
struct handle_type_name<typing::Callable<Return(Args...)>> {
117118
using retval_type = conditional_t<std::is_same<Return, void>::value, void_type, Return>;
118-
static constexpr auto name = const_name("Callable[[") + concat(make_caster<Args>::name...)
119-
+ const_name("], ") + make_caster<retval_type>::name
120-
+ const_name("]");
119+
static constexpr auto name
120+
= const_name("Callable[[") + ::pybind11::detail::concat(make_caster<Args>::name...)
121+
+ const_name("], ") + make_caster<retval_type>::name + const_name("]");
121122
};
122123

123124
PYBIND11_NAMESPACE_END(detail)

tests/test_custom_type_casters.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ struct type_caster<other_lib::MyType> : public other_lib::my_caster {};
134134
} // namespace detail
135135
} // namespace PYBIND11_NAMESPACE
136136

137+
// This simply is required to compile
138+
namespace ADL_issue {
139+
template <typename OutStringType = std::string, typename... Args>
140+
OutStringType concat(Args &&...) {
141+
return OutStringType();
142+
}
143+
144+
struct test {};
145+
} // namespace ADL_issue
146+
137147
TEST_SUBMODULE(custom_type_casters, m) {
138148
// test_custom_type_casters
139149

@@ -206,4 +216,6 @@ TEST_SUBMODULE(custom_type_casters, m) {
206216
py::return_value_policy::reference);
207217

208218
m.def("other_lib_type", [](other_lib::MyType x) { return x; });
219+
220+
m.def("_adl_issue", [](const ADL_issue::test &) {});
209221
}

0 commit comments

Comments
 (0)