Repository: electron/electron
Pull request: fix: -Wunsafe-buffer-usage warnings in AddComponentResourceEntries()

Backport of #44024

See that PR for details.


Notes: none.

Changed files:
filename: shell/browser/extensions/electron_component_extension_resource_manager.cc
patch: @@ -24,10 +24,9 @@ namespace extensions {
 
 ElectronComponentExtensionResourceManager::
     ElectronComponentExtensionResourceManager() {
-  AddComponentResourceEntries(kComponentExtensionResources,
-                              kComponentExtensionResourcesSize);
+  AddComponentResourceEntries(kComponentExtensionResources);
 #if BUILDFLAG(ENABLE_PDF_VIEWER)
-  AddComponentResourceEntries(kPdfResources, kPdfResourcesSize);
+  AddComponentResourceEntries(kPdfResources);
 
   // Register strings for the PDF viewer, so that $i18n{} replacements work.
   base::Value::Dict pdf_strings;
@@ -80,20 +79,18 @@ ElectronComponentExtensionResourceManager::GetTemplateReplacementsForExtension(
 }
 
 void ElectronComponentExtensionResourceManager::AddComponentResourceEntries(
-    const webui::ResourcePath* entries,
-    size_t size) {
+    const base::span<const webui::ResourcePath> entries) {
   base::FilePath gen_folder_path = base::FilePath().AppendASCII(
       "@out_folder@/gen/chrome/browser/resources/");
   gen_folder_path = gen_folder_path.NormalizePathSeparators();
 
-  for (size_t i = 0; i < size; ++i) {
-    base::FilePath resource_path =
-        base::FilePath().AppendASCII(entries[i].path);
+  for (const auto& entry : entries) {
+    base::FilePath resource_path = base::FilePath().AppendASCII(entry.path);
     resource_path = resource_path.NormalizePathSeparators();
 
     if (!gen_folder_path.IsParent(resource_path)) {
       DCHECK(!base::Contains(path_to_resource_id_, resource_path));
-      path_to_resource_id_[resource_path] = entries[i].id;
+      path_to_resource_id_[resource_path] = entry.id;
     } else {
       // If the resource is a generated file, strip the generated folder's path,
       // so that it can be served from a normal URL (as if it were not
@@ -102,7 +99,7 @@ void ElectronComponentExtensionResourceManager::AddComponentResourceEntries(
           base::FilePath().AppendASCII(resource_path.AsUTF8Unsafe().substr(
               gen_folder_path.value().length()));
       DCHECK(!base::Contains(path_to_resource_id_, effective_path));
-      path_to_resource_id_[effective_path] = entries[i].id;
+      path_to_resource_id_[effective_path] = entry.id;
     }
   }
 }

filename: shell/browser/extensions/electron_component_extension_resource_manager.h
patch: @@ -10,6 +10,7 @@
 #include <map>
 #include <string>
 
+#include "base/containers/span.h"
 #include "base/files/file_path.h"
 #include "extensions/browser/component_extension_resource_manager.h"
 #include "ui/base/webui/resource_path.h"
@@ -38,8 +39,8 @@ class ElectronComponentExtensionResourceManager
       const std::string& extension_id) const override;
 
  private:
-  void AddComponentResourceEntries(const webui::ResourcePath* entries,
-                                   size_t size);
+  void AddComponentResourceEntries(
+      base::span<const webui::ResourcePath> entries);
 
   // A map from a resource path to the resource ID.  Used by
   // IsComponentExtensionResource.