Skip to content

Commit

Permalink
Suppress unsafe-buffer-usage
Browse files Browse the repository at this point in the history
  • Loading branch information
d-winsor committed Feb 26, 2024
1 parent 48d105a commit 13bb929
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
8 changes: 6 additions & 2 deletions include/gsl/span
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
#endif

// Turn off clang unsafe buffer warnings as all accessed are guarded by runtime checks
#if defined(__clang__)
#if defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
#endif // defined(__clang__)
#endif // defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")

namespace gsl
{
Expand Down Expand Up @@ -852,4 +852,8 @@ as_writable_bytes(span<ElementType, Extent> s) noexcept
#pragma GCC diagnostic pop
#endif // __GNUC__ > 6

#if defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")
#pragma clang diagnostic pop
#endif

#endif // GSL_SPAN_H
10 changes: 10 additions & 0 deletions include/gsl/util
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@

#endif // _MSC_VER

// Turn off clang unsafe buffer warnings as all accessed are guarded by runtime checks
#if defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
#endif // defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")

#if defined(__cplusplus) && (__cplusplus >= 201703L)
#define GSL_NODISCARD [[nodiscard]]
#else
Expand Down Expand Up @@ -154,4 +160,8 @@ constexpr auto at(std::span<T, extent> sp, const index i) -> decltype(sp[sp.size

#endif // _MSC_VER

#if defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")
#pragma clang diagnostic pop
#endif

#endif // GSL_UTIL_H
20 changes: 10 additions & 10 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,6 @@ if(MSVC) # MSVC or simulating MSVC
if (WARN_RESERVED_ID)
target_compile_options(gsl_tests_config INTERFACE "-Wno-reserved-identifier")
endif()
check_cxx_compiler_flag("-Wno-unsafe-buffer-usage" WARN_UNSAFE_BUFFER)
if (WARN_UNSAFE_BUFFER)
# This test uses very greedy heuristics such as "no pointer arithmetic on raw buffer"
target_compile_options(gsl_tests_config INTERFACE "-Wno-unsafe-buffer-usage")
endif()
else()
target_compile_options(gsl_tests_config INTERFACE
-fno-strict-aliasing
Expand Down Expand Up @@ -191,6 +186,11 @@ else()
>
)
endif(MSVC)
check_cxx_compiler_flag("-Wno-unsafe-buffer-usage" WARN_UNSAFE_BUFFER)
if (WARN_UNSAFE_BUFFER)
# This test uses very greedy heuristics such as "no pointer arithmetic on raw buffer"
target_compile_options(gsl_tests_config INTERFACE "-Wno-unsafe-buffer-usage")
endif()

# for tests to find the gtest header
target_include_directories(gsl_tests_config SYSTEM INTERFACE
Expand Down Expand Up @@ -262,11 +262,6 @@ if(MSVC) # MSVC or simulating MSVC
if (WARN_RESERVED_ID)
target_compile_options(gsl_tests_config_noexcept INTERFACE "-Wno-reserved-identifier")
endif()
check_cxx_compiler_flag("-Wno-unsafe-buffer-usage" WARN_UNSAFE_BUFFER)
if (WARN_UNSAFE_BUFFER)
# This test uses very greedy heuristics such as "no pointer arithmetic on raw buffer"
target_compile_options(gsl_tests_config_noexcept INTERFACE "-Wno-unsafe-buffer-usage")
endif()
else()
target_compile_options(gsl_tests_config_noexcept INTERFACE
-fno-exceptions
Expand Down Expand Up @@ -307,6 +302,11 @@ else()
>
)
endif(MSVC)
check_cxx_compiler_flag("-Wno-unsafe-buffer-usage" WARN_UNSAFE_BUFFER)
if (WARN_UNSAFE_BUFFER)
# This test uses very greedy heuristics such as "no pointer arithmetic on raw buffer"
target_compile_options(gsl_tests_config_noexcept INTERFACE "-Wno-unsafe-buffer-usage")
endif()

add_executable(gsl_noexcept_tests no_exception_ensure_tests.cpp)
target_link_libraries(gsl_noexcept_tests
Expand Down

0 comments on commit 13bb929

Please sign in to comment.