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
//
// Copyright 2021 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.
//
// JsonSerializer.cpp: Implementation of a JSON based serializer
// Note that for binary blob data only a checksum is stored so that
// a lossless deserialization is not supported.
#ifdef UNSAFE_BUFFERS_BUILD
# pragma allow_unsafe_buffers
#endif
#include "JsonSerializer.h"
#include "common/debug.h"
#include <anglebase/sha1.h>
#include <rapidjson/document.h>
#include <rapidjson/filewritestream.h>
#include <rapidjson/ostreamwrapper.h>
#include <rapidjson/prettywriter.h>
namespace angle
{
namespace js = rapidjson;
JsonSerializer::JsonSerializer() : mDoc(js::kObjectType), mAllocator(mDoc.GetAllocator()) {}
JsonSerializer::~JsonSerializer() {}
void JsonSerializer::startGroup(const std::string &name)
{
mGroupValueStack.push(SortedValueGroup());
mGroupNameStack.push(name);
}
void JsonSerializer::endGroup()
{
ASSERT(!mGroupValueStack.empty());
ASSERT(!mGroupNameStack.empty());
rapidjson::Value group = makeValueGroup(mGroupValueStack.top());
std::string name = mGroupNameStack.top();
mGroupValueStack.pop();
mGroupNameStack.pop();
addValue(name, std::move(group));
}
void JsonSerializer::addBlob(const std::string &name, const uint8_t *blob, size_t length)
{
addBlobWithMax(name, blob, length, 16);
}
void JsonSerializer::addBlobWithMax(const std::string &name,
const uint8_t *blob,
size_t length,
size_t maxSerializedLength)
{
unsigned char hash[angle::base::kSHA1Length];
angle::base::SHA1HashBytes(blob, length, hash);
std::ostringstream os;
// Since we don't want to de-serialize the data we just store a checksum of the blob
os << "SHA1:";
static constexpr char kASCII[] = "0123456789ABCDEF";
for (size_t i = 0; i < angle::base::kSHA1Length; ++i)
{
os << kASCII[hash[i] & 0xf] << kASCII[hash[i] >> 4];
}
std::ostringstream hashName;
hashName << name << "-hash";
addString(hashName.str(), os.str());
std::vector<uint8_t> data(
(length < maxSerializedLength) ? length : static_cast<size_t>(maxSerializedLength));
std::copy(blob, blob + data.size(), data.begin());
std::ostringstream rawName;
rawName << name << "-raw[0-" << data.size() - 1 << ']';
addVector(rawName.str(), data);
}
void JsonSerializer::addCString(const std::string &name, const char *value)
{
rapidjson::Value tag(name.c_str(), mAllocator);
rapidjson::Value val(value, mAllocator);
addValue(name, std::move(val));
}
void JsonSerializer::addString(const std::string &name, const std::string &value)
{
addCString(name, value.c_str());
}
void JsonSerializer::addVectorOfStrings(const std::string &name,
const std::vector<std::string> &value)
{
rapidjson::Value arrayValue(rapidjson::kArrayType);
arrayValue.SetArray();
for (const std::string &v : value)
{
rapidjson::Value str(v.c_str(), mAllocator);
arrayValue.PushBack(str, mAllocator);
}
addValue(name, std::move(arrayValue));
}
void JsonSerializer::addBool(const std::string &name, bool value)
{
rapidjson::Value boolValue(value);
addValue(name, std::move(boolValue));
}
void JsonSerializer::addHexValue(const std::string &name, int value)
{
// JSON doesn't support hex values, so write it as a string
std::stringstream hexStream;
hexStream << "0x" << std::uppercase << std::setfill('0') << std::setw(4) << std::hex << value;
addCString(name, hexStream.str().c_str());
}
const char *JsonSerializer::data()
{
ensureEndDocument();
return mResult.c_str();
}
std::vector<uint8_t> JsonSerializer::getData()
{
ensureEndDocument();
return std::vector<uint8_t>(mResult.begin(), mResult.end());
}
void JsonSerializer::ensureEndDocument()
{
if (!mResult.empty())
{
return;
}
std::stringstream os;
js::OStreamWrapper osw(os);
js::PrettyWriter<js::OStreamWrapper> prettyOs(osw);
mDoc.Accept(prettyOs);
mResult = os.str();
}
size_t JsonSerializer::length()
{
ensureEndDocument();
return mResult.length();
}
rapidjson::Value JsonSerializer::makeValueGroup(SortedValueGroup &group)
{
rapidjson::Value valueGroup(js::kObjectType);
for (auto &it : group)
{
rapidjson::Value tag(it.first.c_str(), mAllocator);
valueGroup.AddMember(tag, it.second, mAllocator);
}
return valueGroup;
}
void JsonSerializer::addValue(const std::string &name, rapidjson::Value &&value)
{
if (!mGroupValueStack.empty())
{
mGroupValueStack.top().insert(std::make_pair(name, std::move(value)));
}
else
{
rapidjson::Value nameValue(name, mAllocator);
mDoc.AddMember(nameValue, std::move(value), mAllocator);
}
}
} // namespace angle