1 // Copyright 2024 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "base/compiler_specific.h" 6 #include "testing/gtest/include/gtest/gtest.h" 7 8 namespace { 9 10 template <int A = 0, int B = 0> uses_pointer_as_array(int * i)11UNSAFE_BUFFER_USAGE int uses_pointer_as_array(int* i) { 12 return UNSAFE_BUFFERS(i[1]); 13 } 14 TEST(UnsafeBuffers,Macro)15TEST(UnsafeBuffers, Macro) { 16 int arr[] = {1, 2}; 17 18 // Should compile even with -Wunsafe-buffer-usage. 19 int x = UNSAFE_BUFFERS(uses_pointer_as_array(arr)); 20 EXPECT_EQ(x, 2); 21 22 // Should compile even with -Wunsafe-buffer-usage. 23 UNSAFE_BUFFERS({ 24 uses_pointer_as_array(arr); 25 uses_pointer_as_array(arr); 26 }); 27 28 // Commas don't break things. This comma is not wrapped in `()` which verifies 29 // the macro handles the comma correctly. `()` would hide the comma from the 30 // macro. 31 int y = UNSAFE_BUFFERS(uses_pointer_as_array<1, 1>(arr)); 32 EXPECT_EQ(y, 2); 33 } 34 35 } // namespace 36