From 13a2432bc24d18feadce4db7d3faa36d8e876b4a Mon Sep 17 00:00:00 2001 From: cdesouza-chromium Date: Tue, 12 Nov 2024 03:52:42 +0000 Subject: [PATCH] [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`. --- base/process/process_launcher_posix.cc | 3 ++- browser/ui/views/brave_ads/color_util.cc | 21 +++++++++++-------- .../brave_help_bubble_delegate_view.cc | 6 ++++-- browser/ui/webui/brave_webui_source.cc | 15 +++++-------- browser/ui/webui/brave_webui_source.h | 3 ++- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/base/process/process_launcher_posix.cc b/base/process/process_launcher_posix.cc index fe84f3cdb10..b4ac1438c3f 100644 --- a/base/process/process_launcher_posix.cc +++ b/base/process/process_launcher_posix.cc @@ -5,6 +5,7 @@ #include "brave/base/process/process_launcher.h" +#include #include #include #include @@ -22,7 +23,7 @@ std::optional ProcessLauncher::ReadAppOutput( base::CommandLine cmdline, base::LaunchOptions options, int timeout_sec) { - int pipe_fd[2]; + std::array pipe_fd; if (pipe(pipe_fd) < 0) { return std::nullopt; } diff --git a/browser/ui/views/brave_ads/color_util.cc b/browser/ui/views/brave_ads/color_util.cc index f88d4a6a4a4..ed6a137129f 100644 --- a/browser/ui/views/brave_ads/color_util.cc +++ b/browser/ui/views/brave_ads/color_util.cc @@ -5,7 +5,11 @@ #include "brave/browser/ui/views/brave_ads/color_util.h" +#include + #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 hex; + base::SpanReader reader(rgb); + base::SpanWriter writer(hex); + while (auto component = reader.Read()) { + 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; } diff --git a/browser/ui/views/brave_help_bubble/brave_help_bubble_delegate_view.cc b/browser/ui/views/brave_help_bubble/brave_help_bubble_delegate_view.cc index 85020b61cce..5e678ff1e85 100644 --- a/browser/ui/views/brave_help_bubble/brave_help_bubble_delegate_view.cc +++ b/browser/ui/views/brave_help_bubble/brave_help_bubble_delegate_view.cc @@ -5,6 +5,7 @@ #include "brave/browser/ui/views/brave_help_bubble/brave_help_bubble_delegate_view.h" +#include #include #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 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 diff --git a/browser/ui/webui/brave_webui_source.cc b/browser/ui/webui/brave_webui_source.cc index 46c1dba7c8e..c753f0bc592 100644 --- a/browser/ui/webui/brave_webui_source.cc +++ b/browser/ui/webui/brave_webui_source.cc @@ -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 #include #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 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 resource_map, size_t resource_map_size, int html_resource_id, bool disable_trusted_types_csp) { diff --git a/browser/ui/webui/brave_webui_source.h b/browser/ui/webui/brave_webui_source.h index 39769214d5e..9804812715f 100644 --- a/browser/ui/webui/brave_webui_source.h +++ b/browser/ui/webui/brave_webui_source.h @@ -8,6 +8,7 @@ #include +#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 resource_map, size_t resouece_map_size, int html_resource_id, bool disable_trusted_types_csp = false);