[libcxx-commits] [libcxx] [libc++] Prevent calling the projection more than three times (PR #66315)
Danila Malyutin via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Sep 14 11:10:47 PDT 2023
https://github.com/danilaml updated https://github.com/llvm/llvm-project/pull/66315:
>From ead65bfcb70be46788bc9e88c891e7ae7f91b8d7 Mon Sep 17 00:00:00 2001
From: PandaNinjas <admin at malwarefight.wip.la>
Date: Wed, 13 Sep 2023 17:38:17 -0700
Subject: [PATCH 1/6] [libc++] Prevent calling the projection more than three
times
---
libcxx/include/__algorithm/ranges_clamp.h | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/libcxx/include/__algorithm/ranges_clamp.h b/libcxx/include/__algorithm/ranges_clamp.h
index 9613f7f37720a6c..ca46675eb4b3041 100644
--- a/libcxx/include/__algorithm/ranges_clamp.h
+++ b/libcxx/include/__algorithm/ranges_clamp.h
@@ -37,9 +37,10 @@ struct __fn {
_LIBCPP_ASSERT_UNCATEGORIZED(!bool(std::invoke(__comp, std::invoke(__proj, __high), std::invoke(__proj, __low))),
"Bad bounds passed to std::ranges::clamp");
- if (std::invoke(__comp, std::invoke(__proj, __value), std::invoke(__proj, __low)))
+ auto &projection = std::invoke(__proj, __value);
+ if (std::invoke(__comp, projection, std::invoke(__proj, __low)))
return __low;
- else if (std::invoke(__comp, std::invoke(__proj, __high), std::invoke(__proj, __value)))
+ else if (std::invoke(__comp, std::invoke(__proj, __high), projection))
return __high;
else
return __value;
>From c18d60870ac342a95a5528396a8e0c7b91717cbb Mon Sep 17 00:00:00 2001
From: PandaNinjas <admin at malwarefight.wip.la>
Date: Wed, 13 Sep 2023 18:56:44 -0700
Subject: [PATCH 2/6] [libc++] Run clang-format on file
---
libcxx/include/__algorithm/ranges_clamp.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/__algorithm/ranges_clamp.h b/libcxx/include/__algorithm/ranges_clamp.h
index ca46675eb4b3041..3469a6419b2270f 100644
--- a/libcxx/include/__algorithm/ranges_clamp.h
+++ b/libcxx/include/__algorithm/ranges_clamp.h
@@ -37,7 +37,7 @@ struct __fn {
_LIBCPP_ASSERT_UNCATEGORIZED(!bool(std::invoke(__comp, std::invoke(__proj, __high), std::invoke(__proj, __low))),
"Bad bounds passed to std::ranges::clamp");
- auto &projection = std::invoke(__proj, __value);
+ auto& projection = std::invoke(__proj, __value);
if (std::invoke(__comp, projection, std::invoke(__proj, __low)))
return __low;
else if (std::invoke(__comp, std::invoke(__proj, __high), projection))
>From b40e791f0e9fedbb19936851e1e71decf00331fa Mon Sep 17 00:00:00 2001
From: Jocelyn Castellano <admin at malwarefight.wip.la>
Date: Wed, 13 Sep 2023 19:11:20 -0700
Subject: [PATCH 3/6] [libcxx] CamelCase projection and make variable name more
descriptive
---
libcxx/include/__algorithm/ranges_clamp.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/__algorithm/ranges_clamp.h b/libcxx/include/__algorithm/ranges_clamp.h
index 3469a6419b2270f..3adb5fa828e1ee5 100644
--- a/libcxx/include/__algorithm/ranges_clamp.h
+++ b/libcxx/include/__algorithm/ranges_clamp.h
@@ -37,7 +37,7 @@ struct __fn {
_LIBCPP_ASSERT_UNCATEGORIZED(!bool(std::invoke(__comp, std::invoke(__proj, __high), std::invoke(__proj, __low))),
"Bad bounds passed to std::ranges::clamp");
- auto& projection = std::invoke(__proj, __value);
+ auto& ValueProjection = std::invoke(__proj, __value);
if (std::invoke(__comp, projection, std::invoke(__proj, __low)))
return __low;
else if (std::invoke(__comp, std::invoke(__proj, __high), projection))
>From a8907624defa4cc4f47520a2d93a8bd042816aa2 Mon Sep 17 00:00:00 2001
From: Jocelyn Castellano <admin at malwarefight.wip.la>
Date: Wed, 13 Sep 2023 19:11:47 -0700
Subject: [PATCH 4/6] [libcxx] properly change variable name
---
libcxx/include/__algorithm/ranges_clamp.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libcxx/include/__algorithm/ranges_clamp.h b/libcxx/include/__algorithm/ranges_clamp.h
index 3adb5fa828e1ee5..3d7a224b3649a3f 100644
--- a/libcxx/include/__algorithm/ranges_clamp.h
+++ b/libcxx/include/__algorithm/ranges_clamp.h
@@ -38,9 +38,9 @@ struct __fn {
"Bad bounds passed to std::ranges::clamp");
auto& ValueProjection = std::invoke(__proj, __value);
- if (std::invoke(__comp, projection, std::invoke(__proj, __low)))
+ if (std::invoke(__comp, ValueProjection, std::invoke(__proj, __low)))
return __low;
- else if (std::invoke(__comp, std::invoke(__proj, __high), projection))
+ else if (std::invoke(__comp, std::invoke(__proj, __high), ValueProjection))
return __high;
else
return __value;
>From 15d3b2b79fbd61f97b0312e0913cede36b5b202d Mon Sep 17 00:00:00 2001
From: Jocelyn Castellano <admin at malwarefight.wip.la>
Date: Thu, 14 Sep 2023 10:37:34 -0700
Subject: [PATCH 5/6] Apply suggestions from code review
---
libcxx/include/__algorithm/ranges_clamp.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/libcxx/include/__algorithm/ranges_clamp.h b/libcxx/include/__algorithm/ranges_clamp.h
index 3d7a224b3649a3f..a97538e4c0e3f65 100644
--- a/libcxx/include/__algorithm/ranges_clamp.h
+++ b/libcxx/include/__algorithm/ranges_clamp.h
@@ -37,10 +37,10 @@ struct __fn {
_LIBCPP_ASSERT_UNCATEGORIZED(!bool(std::invoke(__comp, std::invoke(__proj, __high), std::invoke(__proj, __low))),
"Bad bounds passed to std::ranges::clamp");
- auto& ValueProjection = std::invoke(__proj, __value);
- if (std::invoke(__comp, ValueProjection, std::invoke(__proj, __low)))
+ auto&& __projected = std::invoke(__proj, __value);
+ if (std::invoke(__comp, std::forward(__projected), std::invoke(__proj, __low)))
return __low;
- else if (std::invoke(__comp, std::invoke(__proj, __high), ValueProjection))
+ else if (std::invoke(__comp, std::invoke(__proj, __high), std::forward(__projected)))
return __high;
else
return __value;
>From 67a6c91976c76fe1d399a81af8f45ecb549c62ec Mon Sep 17 00:00:00 2001
From: Jocelyn Castellano <admin at malwarefight.wip.la>
Date: Thu, 14 Sep 2023 11:09:55 -0700
Subject: [PATCH 6/6] [libc++] add test to ensure projection is not called more
than 3 times
---
..._not_called_more_than_three_times.pass.cpp | 20 +++++++++++++++++++
1 file changed, 20 insertions(+)
create mode 100644 libcxx/test/std/algorithms/ranges_projection_not_called_more_than_three_times.pass.cpp
diff --git a/libcxx/test/std/algorithms/ranges_projection_not_called_more_than_three_times.pass.cpp b/libcxx/test/std/algorithms/ranges_projection_not_called_more_than_three_times.pass.cpp
new file mode 100644
index 000000000000000..8d0713cd573fb34
--- /dev/null
+++ b/libcxx/test/std/algorithms/ranges_projection_not_called_more_than_three_times.pass.cpp
@@ -0,0 +1,20 @@
+#include <algorithm>
+#include <assert_macros.h>
+
+int Projection(const int value)
+{
+ static int counter = 0;
+
+ if (counter++ == 3)
+ {
+ TEST_FAIL("projection called more than 3 times")
+ }
+
+ return value;
+}
+
+int main()
+{
+ TEST_DOES_NOT_THROW(std::ranges::clamp(3, 2, 4, std::ranges::less{}, &Projection));
+ return 0;
+}
More information about the libcxx-commits
mailing list