[spanify] Use safe buffers pt.1 (#26486)

This change touches in a few places where safe buffers usage can be
improved, by making use of `base::span`, and `std::array`.
This commit is contained in:
cdesouza-chromium
2024-11-12 03:52:42 +00:00
committed by GitHub
parent 644290acc9
commit 13a2432bc2
5 changed files with 25 additions and 23 deletions
+2 -1
View File
@@ -5,6 +5,7 @@
#include "brave/base/process/process_launcher.h"
#include <array>
#include <optional>
#include <string>
#include <vector>
@@ -22,7 +23,7 @@ std::optional<std::string> ProcessLauncher::ReadAppOutput(
base::CommandLine cmdline,
base::LaunchOptions options,
int timeout_sec) {
int pipe_fd[2];
std::array<int, 2> pipe_fd;
if (pipe(pipe_fd) < 0) {
return std::nullopt;
}
+12 -9
View File
@@ -5,7 +5,11 @@
#include "brave/browser/ui/views/brave_ads/color_util.h"
#include <array>
#include "base/compiler_specific.h"
#include "base/containers/span_reader.h"
#include "base/containers/span_writer.h"
#include "base/strings/string_number_conversions.h"
namespace brave_ads {
@@ -21,18 +25,17 @@ bool RgbStringToSkColor(std::string_view rgb, SkColor* color) {
return false;
}
uint32_t components[kColorComponentsCount];
for (size_t i = 0; i < kColorComponentsCount; ++i) {
const size_t beg = kColorComponentLen * i;
uint32_t component = 0;
if (!base::HexStringToUInt(rgb.substr(beg, kColorComponentLen),
&component)) {
std::array<uint32_t, kColorComponentsCount> hex;
base::SpanReader<const char> reader(rgb);
base::SpanWriter<uint32_t> writer(hex);
while (auto component = reader.Read<kColorComponentLen>()) {
uint32_t value;
if (!base::HexStringToUInt(base::as_string_view(*component), &value)) {
return false;
}
UNSAFE_TODO(components[i] = component);
writer.Write(value);
}
*color = SkColorSetRGB(components[0], components[1], components[2]);
*color = SkColorSetRGB(hex[0], hex[1], hex[2]);
return true;
}
@@ -5,6 +5,7 @@
#include "brave/browser/ui/views/brave_help_bubble/brave_help_bubble_delegate_view.h"
#include <array>
#include <utility>
#include "base/strings/utf_string_conversions.h"
@@ -101,7 +102,7 @@ class BorderWithArrow : public views::BubbleBorder {
BubbleArrowPart part) {
constexpr size_t kNumPoints = 4;
gfx::RectF bounds_f(bounds);
SkPoint points[kNumPoints];
std::array<SkPoint, kNumPoints> points;
switch (GetBubbleArrowSide(arrow)) {
case views::BubbleArrowSide::kRight:
points[0] = {bounds_f.x(), bounds_f.y()};
@@ -137,7 +138,8 @@ class BorderWithArrow : public views::BubbleBorder {
break;
}
return SkPath::Polygon(points, kNumPoints, part == BubbleArrowPart::kFill);
return SkPath::Polygon(points.data(), kNumPoints,
part == BubbleArrowPart::kFill);
}
};
} // namespace
+5 -10
View File
@@ -3,18 +3,13 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and
// convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "brave/browser/ui/webui/brave_webui_source.h"
#include <string_view>
#include <vector>
#include "base/containers/flat_map.h"
#include "base/containers/span.h"
#include "base/strings/utf_string_conversions.h"
#include "brave/components/constants/url_constants.h"
#include "brave/components/tor/buildflags/buildflags.h"
@@ -61,7 +56,7 @@ void CustomizeWebUIHTMLSource(content::WebUI* web_ui,
content::WebUIDataSource* CreateWebUIDataSource(
content::WebUI* web_ui,
const std::string& name,
const webui::ResourcePath* resource_map,
base::span<const webui::ResourcePath> resource_map,
size_t resource_map_size,
int html_resource_id,
bool disable_trusted_types_csp) {
@@ -87,8 +82,8 @@ content::WebUIDataSource* CreateWebUIDataSource(
source->UseStringsJs();
source->SetDefaultResource(html_resource_id);
// Add generated resource paths
for (size_t i = 0; i < resource_map_size; ++i) {
source->AddResourcePath(resource_map[i].path, resource_map[i].id);
for (const auto& resource : resource_map) {
source->AddResourcePath(resource.path, resource.id);
}
CustomizeWebUIHTMLSource(web_ui, name, source);
return source;
@@ -99,7 +94,7 @@ content::WebUIDataSource* CreateWebUIDataSource(
content::WebUIDataSource* CreateAndAddWebUIDataSource(
content::WebUI* web_ui,
const std::string& name,
const webui::ResourcePath* resource_map,
base::span<const webui::ResourcePath> resource_map,
size_t resource_map_size,
int html_resource_id,
bool disable_trusted_types_csp) {
+2 -1
View File
@@ -8,6 +8,7 @@
#include <string>
#include "base/containers/span.h"
#include "build/build_config.h"
namespace content {
@@ -24,7 +25,7 @@ struct ResourcePath;
content::WebUIDataSource* CreateAndAddWebUIDataSource(
content::WebUI* web_ui,
const std::string& name,
const webui::ResourcePath* resource_map,
base::span<const webui::ResourcePath> resource_map,
size_t resouece_map_size,
int html_resource_id,
bool disable_trusted_types_csp = false);