From f15fea8d03c2438cbbfbf4531f81a199cad232a9 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 25 Sep 2024 04:21:15 -0500 Subject: [PATCH] fix: -Wunsafe-buffer-usage warnings in GetNextZoomLevel() (31-x-y) (#43926) * fix: -Wunsafe-buffer-usage warnings in GetNextZoomLevel() (#43803) * empty commit --- shell/browser/ui/inspectable_web_contents.cc | 32 +++++++++++--------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/shell/browser/ui/inspectable_web_contents.cc b/shell/browser/ui/inspectable_web_contents.cc index 8f0ccfb2cf82..adc882ba4064 100644 --- a/shell/browser/ui/inspectable_web_contents.cc +++ b/shell/browser/ui/inspectable_web_contents.cc @@ -5,6 +5,8 @@ #include "shell/browser/ui/inspectable_web_contents.h" +#include +#include #include #include #include @@ -74,10 +76,6 @@ namespace electron { namespace { -const double kPresetZoomFactors[] = {0.25, 0.333, 0.5, 0.666, 0.75, 0.9, - 1.0, 1.1, 1.25, 1.5, 1.75, 2.0, - 2.5, 3.0, 4.0, 5.0}; - const char kChromeUIDevToolsURL[] = "devtools://devtools/bundled/devtools_app.html?" "remoteBase=%s&" @@ -126,16 +124,22 @@ void SetZoomLevelForWebContents(content::WebContents* web_contents, content::HostZoomMap::SetZoomLevel(web_contents, level); } -double GetNextZoomLevel(double level, bool out) { - double factor = blink::PageZoomLevelToZoomFactor(level); - size_t size = std::size(kPresetZoomFactors); - for (size_t i = 0; i < size; ++i) { - if (!blink::PageZoomValuesEqual(kPresetZoomFactors[i], factor)) - continue; - if (out && i > 0) - return blink::PageZoomFactorToZoomLevel(kPresetZoomFactors[i - 1]); - if (!out && i != size - 1) - return blink::PageZoomFactorToZoomLevel(kPresetZoomFactors[i + 1]); +double GetNextZoomLevel(const double level, const bool out) { + static constexpr std::array kPresetFactors{ + 0.25, 0.333, 0.5, 0.666, 0.75, 0.9, 1.0, 1.1, + 1.25, 1.5, 1.75, 2.0, 2.5, 3.0, 4.0, 5.0}; + static constexpr auto kBegin = kPresetFactors.begin(); + static constexpr auto kEnd = kPresetFactors.end(); + + const double factor = blink::PageZoomLevelToZoomFactor(level); + auto matches = [=](auto val) { + return blink::PageZoomValuesEqual(factor, val); + }; + if (auto iter = std::find_if(kBegin, kEnd, matches); iter != kEnd) { + if (out && iter != kBegin) + return blink::PageZoomFactorToZoomLevel(*--iter); + if (!out && ++iter != kEnd) + return blink::PageZoomFactorToZoomLevel(*iter); } return level; }