1From 1b4d42ca2e97061042ec44a0b34ceb176c78c7e1 Mon Sep 17 00:00:00 2001 2From: d-winsor <danwin@microsoft.com> 3Date: Mon, 26 Feb 2024 13:17:12 -0800 4Subject: [PATCH] Fix initialization in test (#1140) 5 6* Suppress unsafe-buffer-usage 7 8Upstream-Status: Backport [https://github.com/microsoft/GSL/commit/1b4d42ca2e97061042ec44a0b34ceb176c78c7e1] 9 10Signed-off-by: Peter Marko <peter.marko@siemens.com> 11--- 12 include/gsl/span | 10 ++++++++++ 13 include/gsl/util | 10 ++++++++++ 14 tests/CMakeLists.txt | 10 ++++++++++ 15 tests/span_tests.cpp | 2 +- 16 4 files changed, 31 insertions(+), 1 deletion(-) 17 18diff --git a/include/gsl/span b/include/gsl/span 19index cc8a7b9..d254e4d 100644 20--- a/include/gsl/span 21+++ b/include/gsl/span 22@@ -58,6 +58,12 @@ 23 #pragma GCC diagnostic ignored "-Wsign-conversion" 24 #endif 25 26+// Turn off clang unsafe buffer warnings as all accessed are guarded by runtime checks 27+#if defined(__clang__) && __has_warning("-Wunsafe-buffer-usage") 28+#pragma clang diagnostic push 29+#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" 30+#endif // defined(__clang__) && __has_warning("-Wunsafe-buffer-usage") 31+ 32 namespace gsl 33 { 34 35@@ -818,4 +824,8 @@ as_writable_bytes(span<ElementType, Extent> s) noexcept 36 #pragma GCC diagnostic pop 37 #endif // __GNUC__ > 6 38 39+#if defined(__clang__) && __has_warning("-Wunsafe-buffer-usage") 40+#pragma clang diagnostic pop 41+#endif 42+ 43 #endif // GSL_SPAN_H 44diff --git a/include/gsl/util b/include/gsl/util 45index a215bad..11735a8 100644 46--- a/include/gsl/util 47+++ b/include/gsl/util 48@@ -39,6 +39,12 @@ 49 50 #endif // _MSC_VER 51 52+// Turn off clang unsafe buffer warnings as all accessed are guarded by runtime checks 53+#if defined(__clang__) && __has_warning("-Wunsafe-buffer-usage") 54+#pragma clang diagnostic push 55+#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" 56+#endif // defined(__clang__) && __has_warning("-Wunsafe-buffer-usage") 57+ 58 #if defined(__cplusplus) && (__cplusplus >= 201703L) 59 #define GSL_NODISCARD [[nodiscard]] 60 #else 61@@ -157,4 +163,8 @@ constexpr auto at(std::span<T, extent> sp, const index i) -> decltype(sp[sp.size 62 63 #endif // _MSC_VER 64 65+#if defined(__clang__) && __has_warning("-Wunsafe-buffer-usage") 66+#pragma clang diagnostic pop 67+#endif 68+ 69 #endif // GSL_UTIL_H 70diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt 71index cab4e56..20de9e1 100644 72--- a/tests/CMakeLists.txt 73+++ b/tests/CMakeLists.txt 74@@ -167,6 +167,11 @@ else() 75 > 76 ) 77 endif(MSVC) 78+check_cxx_compiler_flag("-Wno-unsafe-buffer-usage" WARN_UNSAFE_BUFFER) 79+if (WARN_UNSAFE_BUFFER) 80+ # This test uses very greedy heuristics such as "no pointer arithmetic on raw buffer" 81+ target_compile_options(gsl_tests_config INTERFACE "-Wno-unsafe-buffer-usage") 82+endif() 83 84 # for tests to find the gtest header 85 target_include_directories(gsl_tests_config SYSTEM INTERFACE 86@@ -267,6 +272,11 @@ else() 87 > 88 ) 89 endif(MSVC) 90+check_cxx_compiler_flag("-Wno-unsafe-buffer-usage" WARN_UNSAFE_BUFFER) 91+if (WARN_UNSAFE_BUFFER) 92+ # This test uses very greedy heuristics such as "no pointer arithmetic on raw buffer" 93+ target_compile_options(gsl_tests_config_noexcept INTERFACE "-Wno-unsafe-buffer-usage") 94+endif() 95 96 add_executable(gsl_noexcept_tests no_exception_ensure_tests.cpp) 97 target_link_libraries(gsl_noexcept_tests 98diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp 99index 33ccf56..3c1dfe5 100644 100--- a/tests/span_tests.cpp 101+++ b/tests/span_tests.cpp 102@@ -330,7 +330,7 @@ TEST(span_test, from_array_constructor) 103 EXPECT_TRUE(s.data() == std::addressof(arr2d[0])); 104 } 105 106- int arr3d[2][3][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; 107+ int arr3d[2][3][2] = { { {1, 2}, {3, 4}, {5, 6} }, { {7, 8}, {9, 10}, {11, 12} } }; 108 109 #ifdef CONFIRM_COMPILATION_ERRORS 110 { 111-- 1122.30.2 113 114