Hash :
25390156
Author :
Date :
2025-08-21T00:13:19
Suppress unsafe buffers on a file-by-file basis in src/ [1 of N] In this CL, we suppress many files but stop short of actually enabling the warning by not removing the line from the unsafe_buffers_paths.txt file. That will happen in a follow-on CL, along with resolving any stragglers missed here. This is mostly a manual change so as to familiarize myself with the kinds of issues faced by the Angle codebase when applying buffer safety warnings. -- Re-generate affected hashes. -- Clang-format applied to all changed files. -- Add a few missing .reserve() calls to vectors as noticed. -- Fix some mismatches between file names and header comments. -- Be more consistent with header comment format (blank lines and trailing //-only lines when a filename comment adjoins license boilerplate). Bug: b/436880895 Change-Id: I3bde5cc2059acbe8345057289214f1a26f1c34aa Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6869022 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
//
// Copyright 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// utilities_unittest.cpp: Unit tests for ANGLE's GL utility functions
#ifdef UNSAFE_BUFFERS_BUILD
# pragma allow_unsafe_buffers
#endif
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "common/utilities.h"
namespace
{
// Test parsing valid single array indices
TEST(ParseResourceName, ArrayIndex)
{
std::vector<unsigned int> indices;
EXPECT_EQ("foo", gl::ParseResourceName("foo[123]", &indices));
ASSERT_EQ(1u, indices.size());
EXPECT_EQ(123u, indices[0]);
EXPECT_EQ("bar", gl::ParseResourceName("bar[0]", &indices));
ASSERT_EQ(1u, indices.size());
EXPECT_EQ(0u, indices[0]);
}
// Parsing a negative array index should result in INVALID_INDEX.
TEST(ParseResourceName, NegativeArrayIndex)
{
std::vector<unsigned int> indices;
EXPECT_EQ("foo", gl::ParseResourceName("foo[-1]", &indices));
ASSERT_EQ(1u, indices.size());
EXPECT_EQ(GL_INVALID_INDEX, indices.back());
}
// Parsing a letter array index should result in INVALID_INDEX.
TEST(ParseResourceName, LetterArrayIndex)
{
std::vector<unsigned int> indices;
EXPECT_EQ("foo", gl::ParseResourceName("foo[a]", &indices));
ASSERT_EQ(1u, indices.size());
EXPECT_EQ(GL_INVALID_INDEX, indices.back());
}
// Parsing no array indices should result in an empty array.
TEST(ParseResourceName, NoArrayIndex)
{
std::vector<unsigned int> indices;
EXPECT_EQ("foo", gl::ParseResourceName("foo", &indices));
EXPECT_TRUE(indices.empty());
EXPECT_EQ("foo[]", gl::ParseResourceName("foo[]", &indices));
EXPECT_TRUE(indices.empty());
}
// The ParseResourceName function should work when a nullptr is passed as the indices output vector.
TEST(ParseResourceName, NULLArrayIndices)
{
EXPECT_EQ("foo", gl::ParseResourceName("foo[10]", nullptr));
}
// Parsing multiple array indices should result in outermost array indices being last in the vector.
TEST(ParseResourceName, MultipleArrayIndices)
{
std::vector<unsigned int> indices;
EXPECT_EQ("foo", gl::ParseResourceName("foo[12][34][56]", &indices));
ASSERT_EQ(3u, indices.size());
// Indices are sorted with outermost array index last.
EXPECT_EQ(56u, indices[0]);
EXPECT_EQ(34u, indices[1]);
EXPECT_EQ(12u, indices[2]);
}
// Trailing whitespace should not be accepted by ParseResourceName.
TEST(ParseResourceName, TrailingWhitespace)
{
std::vector<unsigned int> indices;
EXPECT_EQ("foo ", gl::ParseResourceName("foo ", &indices));
EXPECT_TRUE(indices.empty());
EXPECT_EQ("foo[10] ", gl::ParseResourceName("foo[10] ", &indices));
EXPECT_TRUE(indices.empty());
EXPECT_EQ("foo[10][20] ", gl::ParseResourceName("foo[10][20] ", &indices));
EXPECT_TRUE(indices.empty());
}
// Parse a string without any index.
TEST(ParseArrayIndex, NoArrayIndex)
{
size_t nameLengthWithoutArrayIndex;
EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("foo", &nameLengthWithoutArrayIndex));
EXPECT_EQ(3u, nameLengthWithoutArrayIndex);
}
// Parse an empty string for an array index.
TEST(ParseArrayIndex, EmptyString)
{
size_t nameLengthWithoutArrayIndex;
EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("", &nameLengthWithoutArrayIndex));
EXPECT_EQ(0u, nameLengthWithoutArrayIndex);
}
// A valid array index is parsed correctly from the end of the string.
TEST(ParseArrayIndex, ArrayIndex)
{
size_t nameLengthWithoutArrayIndex;
EXPECT_EQ(123u, gl::ParseArrayIndex("foo[123]", &nameLengthWithoutArrayIndex));
EXPECT_EQ(3u, nameLengthWithoutArrayIndex);
}
// An array index from the middle of the string is not parsed.
TEST(ParseArrayIndex, ArrayIndexInMiddle)
{
size_t nameLengthWithoutArrayIndex;
EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("foo[123].bar", &nameLengthWithoutArrayIndex));
EXPECT_EQ(12u, nameLengthWithoutArrayIndex);
}
// Trailing whitespace in the parsed string is taken into account.
TEST(ParseArrayIndex, TrailingWhitespace)
{
size_t nameLengthWithoutArrayIndex;
EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("foo[123] ", &nameLengthWithoutArrayIndex));
EXPECT_EQ(9u, nameLengthWithoutArrayIndex);
}
// Only the last index is parsed.
TEST(ParseArrayIndex, MultipleArrayIndices)
{
size_t nameLengthWithoutArrayIndex;
EXPECT_EQ(34u, gl::ParseArrayIndex("foo[12][34]", &nameLengthWithoutArrayIndex));
EXPECT_EQ(7u, nameLengthWithoutArrayIndex);
}
// GetProgramResourceLocation spec in GLES 3.1 November 2016 page 87 mentions "decimal" integer.
// So an integer in hexadecimal format should not parse as an array index.
TEST(ParseArrayIndex, HexArrayIndex)
{
size_t nameLengthWithoutArrayIndex;
EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("foo[0xff]", &nameLengthWithoutArrayIndex));
EXPECT_EQ(9u, nameLengthWithoutArrayIndex);
}
// GetProgramResourceLocation spec in GLES 3.1 November 2016 page 87 mentions that the array
// index should not contain a leading plus sign.
TEST(ParseArrayIndex, ArrayIndexLeadingPlus)
{
size_t nameLengthWithoutArrayIndex;
EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("foo[+1]", &nameLengthWithoutArrayIndex));
EXPECT_EQ(7u, nameLengthWithoutArrayIndex);
}
// GetProgramResourceLocation spec in GLES 3.1 November 2016 page 87 says that index should not
// contain whitespace. Test leading whitespace.
TEST(ParseArrayIndex, ArrayIndexLeadingWhiteSpace)
{
size_t nameLengthWithoutArrayIndex;
EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("foo[ 0]", &nameLengthWithoutArrayIndex));
EXPECT_EQ(7u, nameLengthWithoutArrayIndex);
}
// GetProgramResourceLocation spec in GLES 3.1 November 2016 page 87 says that index should not
// contain whitespace. Test trailing whitespace.
TEST(ParseArrayIndex, ArrayIndexTrailingWhiteSpace)
{
size_t nameLengthWithoutArrayIndex;
EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("foo[0 ]", &nameLengthWithoutArrayIndex));
EXPECT_EQ(7u, nameLengthWithoutArrayIndex);
}
// GetProgramResourceLocation spec in GLES 3.1 November 2016 page 87 says that index should only
// contain an integer.
TEST(ParseArrayIndex, ArrayIndexBogus)
{
size_t nameLengthWithoutArrayIndex;
EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("foo[0bogus]", &nameLengthWithoutArrayIndex));
EXPECT_EQ(11u, nameLengthWithoutArrayIndex);
}
// Verify that using an index value out-of-range fails.
TEST(ParseArrayIndex, ArrayIndexOutOfRange)
{
size_t nameLengthWithoutArrayIndex;
EXPECT_EQ(GL_INVALID_INDEX,
gl::ParseArrayIndex("foo[4294967296]", &nameLengthWithoutArrayIndex));
EXPECT_EQ(15u, nameLengthWithoutArrayIndex);
}
// Test that ConstStrLen works.
TEST(Utilities, ConstStrLen)
{
constexpr auto v1 = angle::ConstStrLen(nullptr);
EXPECT_EQ(0u, v1);
constexpr auto v2 = angle::ConstStrLen("");
EXPECT_EQ(0u, v2);
constexpr auto v3 = angle::ConstStrLen("a");
EXPECT_EQ(1u, v3);
constexpr char c[5] = "cc\0c";
constexpr auto v4 = angle::ConstStrLen(c);
EXPECT_EQ(2u, v4);
constexpr char d[] = "dddd";
constexpr auto v5 = angle::ConstStrLen(d);
EXPECT_EQ(4u, v5);
constexpr char *e = nullptr;
constexpr auto v6 = angle::ConstStrLen(e);
EXPECT_EQ(0u, v6);
// Non-constexpr invocations
const char cc[5] = "cc\0c";
auto n1 = angle::ConstStrLen(cc);
EXPECT_EQ(2u, n1);
const char *dd = "ddd";
auto n2 = angle::ConstStrLen(dd);
EXPECT_EQ(3u, n2);
}
// Tests gl::ComputeIndexRange().
TEST(Utilities, IndexRanges)
{
constexpr auto b = gl::DrawElementsType::UnsignedByte;
uint8_t vertices1[] = {1, 2, 3, 4, 0xff, 6, 5, 7, 10, 0xff, 0xff, 0xff, 9, 8, 0xff};
EXPECT_EQ(ComputeIndexRange(b, vertices1, 0, true), gl::IndexRange());
EXPECT_EQ(ComputeIndexRange(b, vertices1, 0, false), gl::IndexRange());
EXPECT_EQ(ComputeIndexRange(b, vertices1, 1, true), gl::IndexRange(1, 1));
EXPECT_EQ(ComputeIndexRange(b, vertices1, 1, false), gl::IndexRange(1, 1));
EXPECT_EQ(ComputeIndexRange(b, vertices1, 2, true), gl::IndexRange(1, 2));
EXPECT_EQ(ComputeIndexRange(b, vertices1, 2, false), gl::IndexRange(1, 2));
EXPECT_EQ(ComputeIndexRange(b, vertices1, 4, true), gl::IndexRange(1, 4));
EXPECT_EQ(ComputeIndexRange(b, vertices1, 4, false), gl::IndexRange(1, 4));
EXPECT_EQ(ComputeIndexRange(b, vertices1, 5, true), gl::IndexRange(1, 4));
EXPECT_EQ(ComputeIndexRange(b, vertices1, 5, false), gl::IndexRange(1, 255));
EXPECT_EQ(ComputeIndexRange(b, vertices1, 15, true), gl::IndexRange(1, 10));
EXPECT_EQ(ComputeIndexRange(b, vertices1, 15, false), gl::IndexRange(1, 255));
EXPECT_EQ(ComputeIndexRange(b, vertices1, 15, false), gl::IndexRange(1, 255));
EXPECT_EQ(ComputeIndexRange(b, vertices1 + 9, 3, false), gl::IndexRange(255, 255));
EXPECT_EQ(ComputeIndexRange(b, vertices1 + 9, 3, true), gl::IndexRange());
EXPECT_EQ(ComputeIndexRange(b, vertices1 + 9, 4, false), gl::IndexRange(9, 255));
EXPECT_EQ(ComputeIndexRange(b, vertices1 + 9, 4, true), gl::IndexRange(9, 9));
EXPECT_EQ(ComputeIndexRange(b, vertices1 + 8, 4, false), gl::IndexRange(10, 255));
EXPECT_EQ(ComputeIndexRange(b, vertices1 + 8, 4, true), gl::IndexRange(10, 10));
uint8_t vertices2[] = {
0xff,
0xff,
2,
};
EXPECT_EQ(ComputeIndexRange(b, vertices2, 1, true), gl::IndexRange());
EXPECT_EQ(ComputeIndexRange(b, vertices2, 1, false), gl::IndexRange(255, 255));
EXPECT_EQ(ComputeIndexRange(b, vertices2, 2, true), gl::IndexRange());
EXPECT_EQ(ComputeIndexRange(b, vertices2, 2, false), gl::IndexRange(255, 255));
EXPECT_EQ(ComputeIndexRange(b, vertices2, 3, true), gl::IndexRange(2, 2));
EXPECT_EQ(ComputeIndexRange(b, vertices2, 3, false), gl::IndexRange(2, 255));
}
} // anonymous namespace