1 /* * Copyright (c) 2018 Apple Inc. All rights reserved.
2 *
3 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
4 *
5 * This file contains Original Code and/or Modifications of Original Code
6 * as defined in and that are subject to the Apple Public Source License
7 * Version 2.0 (the 'License'). You may not use this file except in
8 * compliance with the License. The rights granted to you under the License
9 * may not be used to create, or enable the creation or redistribution of,
10 * unlawful or unlicensed copies of an Apple operating system, or to
11 * circumvent, violate, or enable the circumvention or violation of, any
12 * terms of an Apple operating system software license agreement.
13 *
14 * Please obtain a copy of the License at
15 * http://www.opensource.apple.com/apsl/ and read it before using this file.
16 *
17 * The Original Code and all software distributed under the License are
18 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
19 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
20 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
22 * Please see the License for the specific language governing rights and
23 * limitations under the License.
24 *
25 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
26 */
27
28 #ifndef _OS_CXX_SAFE_BUFFERS_H
29 #define _OS_CXX_SAFE_BUFFERS_H
30
31 #if (defined(__has_include) && __has_include(<span>))
32 #include <span>
33
34 namespace os {
35 namespace span {
36 #pragma clang unsafe_buffer_usage begin
37 /* The `__unsafe_forge_span` functions are for suppressing false
38 * positive `-Wunsafe-buffer-usage-in-container` warnings on
39 * uses of the two-parameter `std::span` constructors.
40 *
41 * For a `std::span(ptr, size)` call that raises a false alarm, one
42 * can suppress the warning by changing the call to
43 * `__unsafe_forge_span(ptr, size)`.
44 *
45 * Please consider the C++ Safe Buffers Programming Model and
46 * Adoption Tooling Guide as a reference to identify false positives
47 * and do not use the functions in non-applicable cases.
48 */
49 template<class It>
50 std::span<std::remove_reference_t<std::iter_reference_t<It> > >
__unsafe_forge_span(It data,typename std::span<std::remove_reference_t<std::iter_reference_t<It>>>::size_type size)51 __unsafe_forge_span(It data, typename std::span<std::remove_reference_t<std::iter_reference_t<It> > >::size_type size)
52 {
53 return std::span<std::remove_reference_t<std::iter_reference_t<It> > >
54 {data, size};
55 }
56
57 template<std::contiguous_iterator It, std::sized_sentinel_for<It> End>
58 std::span<std::remove_reference_t<std::iter_reference_t<It> > >
__unsafe_forge_span(It begin,End end)59 __unsafe_forge_span(It begin, End end)
60 {
61 return std::span<std::remove_reference_t<std::iter_reference_t<It> > >{begin, end};
62 }
63 #pragma clang unsafe_buffer_usage end
64 } // namespace span
65 } // namespace os
66 #endif /* (defined(__has_include) && __has_include(<span>)) */
67 #endif /* _OS_CXX_SAFE_BUFFERS_H */
68