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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
# pragma allow_unsafe_buffers
#endif
#include "anglebase/sha1.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "anglebase/sys_byteorder.h"
namespace angle
{
namespace base
{
static inline uint32_t f(uint32_t t, uint32_t B, uint32_t C, uint32_t D)
{
if (t < 20)
{
return (B & C) | ((~B) & D);
}
else if (t < 40)
{
return B ^ C ^ D;
}
else if (t < 60)
{
return (B & C) | (B & D) | (C & D);
}
else
{
return B ^ C ^ D;
}
}
static inline uint32_t S(uint32_t n, uint32_t X)
{
return (X << n) | (X >> (32 - n));
}
static inline uint32_t K(uint32_t t)
{
if (t < 20)
{
return 0x5a827999;
}
else if (t < 40)
{
return 0x6ed9eba1;
}
else if (t < 60)
{
return 0x8f1bbcdc;
}
else
{
return 0xca62c1d6;
}
}
const int SecureHashAlgorithm::kDigestSizeBytes = 20;
void SecureHashAlgorithm::Init()
{
A = 0;
B = 0;
C = 0;
D = 0;
E = 0;
cursor = 0;
l = 0;
H[0] = 0x67452301;
H[1] = 0xefcdab89;
H[2] = 0x98badcfe;
H[3] = 0x10325476;
H[4] = 0xc3d2e1f0;
}
void SecureHashAlgorithm::Final()
{
Pad();
Process();
for (int t = 0; t < 5; ++t)
H[t] = ByteSwap(H[t]);
}
void SecureHashAlgorithm::Update(const void *data, size_t nbytes)
{
const uint8_t *d = reinterpret_cast<const uint8_t *>(data);
while (nbytes--)
{
M[cursor++] = *d++;
if (cursor >= 64)
Process();
l += 8;
}
}
void SecureHashAlgorithm::Pad()
{
M[cursor++] = 0x80;
if (cursor > 64 - 8)
{
// pad out to next block
while (cursor < 64)
M[cursor++] = 0;
Process();
}
while (cursor < 64 - 8)
M[cursor++] = 0;
M[cursor++] = (l >> 56) & 0xff;
M[cursor++] = (l >> 48) & 0xff;
M[cursor++] = (l >> 40) & 0xff;
M[cursor++] = (l >> 32) & 0xff;
M[cursor++] = (l >> 24) & 0xff;
M[cursor++] = (l >> 16) & 0xff;
M[cursor++] = (l >> 8) & 0xff;
M[cursor++] = l & 0xff;
}
void SecureHashAlgorithm::Process()
{
uint32_t t;
// Each a...e corresponds to a section in the FIPS 180-3 algorithm.
// a.
//
// W and M are in a union, so no need to memcpy.
// memcpy(W, M, sizeof(M));
for (t = 0; t < 16; ++t)
W[t] = ByteSwap(W[t]);
// b.
for (t = 16; t < 80; ++t)
W[t] = S(1, W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]);
// c.
A = H[0];
B = H[1];
C = H[2];
D = H[3];
E = H[4];
// d.
for (t = 0; t < 80; ++t)
{
uint32_t TEMP = S(5, A) + f(t, B, C, D) + E + W[t] + K(t);
E = D;
D = C;
C = S(30, B);
B = A;
A = TEMP;
}
// e.
H[0] += A;
H[1] += B;
H[2] += C;
H[3] += D;
H[4] += E;
cursor = 0;
}
std::array<uint8_t, kSHA1Length> SecureHashAlgorithm::DigestAsArray() const
{
std::array<uint8_t, kSHA1Length> digest;
memcpy(digest.data(), Digest(), SecureHashAlgorithm::kDigestSizeBytes);
return digest;
}
std::string SHA1HashString(const std::string &str)
{
char hash[SecureHashAlgorithm::kDigestSizeBytes];
SHA1HashBytes(reinterpret_cast<const unsigned char *>(str.c_str()), str.length(),
reinterpret_cast<unsigned char *>(hash));
return std::string(hash, SecureHashAlgorithm::kDigestSizeBytes);
}
void SHA1HashBytes(const unsigned char *data, size_t len, unsigned char *hash)
{
SecureHashAlgorithm sha;
sha.Update(data, len);
sha.Final();
memcpy(hash, sha.Digest(), SecureHashAlgorithm::kDigestSizeBytes);
}
} // namespace base
} // namespace angle