fix: bugprone-narrowing-conversions warnings in NativeImage (#44739)

* fix: bugprone-narrowing-conversions warning in NativeImage::memory_usage_

- fix signed / unsigned math by using base/numerics/safe_conversions

- make memory_usage_ an int64_t so it can safely take the size_t
  returned by computeByteSize()

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
trop[bot] 2024-11-20 11:15:22 -06:00 committed by GitHub
parent 6cc9aa57fe
commit d9d6ba180c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 14 deletions

View file

@ -12,6 +12,7 @@
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/memory/ref_counted_memory.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/pattern.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
@ -146,13 +147,13 @@ NativeImage::~NativeImage() {
}
void NativeImage::UpdateExternalAllocatedMemoryUsage() {
int32_t new_memory_usage = 0;
int64_t new_memory_usage = 0;
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
auto* const image_skia = image_.ToImageSkia();
if (!image_skia->isNull()) {
new_memory_usage = image_skia->bitmap()->computeByteSize();
}
if (!image_skia->isNull())
new_memory_usage =
base::as_signed(image_skia->bitmap()->computeByteSize());
}
isolate_->AdjustAmountOfExternalAllocatedMemory(new_memory_usage -
@ -492,9 +493,8 @@ gin::Handle<NativeImage> NativeImage::CreateFromBitmap(
return gin::Handle<NativeImage>();
}
unsigned int width = 0;
unsigned int height = 0;
double scale_factor = 1.;
int width = 0;
int height = 0;
if (!options.Get("width", &width)) {
thrower.ThrowError("width is required");
@ -506,6 +506,9 @@ gin::Handle<NativeImage> NativeImage::CreateFromBitmap(
return gin::Handle<NativeImage>();
}
if (width <= 0 || height <= 0)
return CreateEmpty(thrower.isolate());
auto info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
auto size_bytes = info.computeMinByteSize();
@ -514,16 +517,12 @@ gin::Handle<NativeImage> NativeImage::CreateFromBitmap(
return gin::Handle<NativeImage>();
}
options.Get("scaleFactor", &scale_factor);
if (width == 0 || height == 0) {
return CreateEmpty(thrower.isolate());
}
SkBitmap bitmap;
bitmap.allocN32Pixels(width, height, false);
bitmap.writePixels({info, node::Buffer::Data(buffer), bitmap.rowBytes()});
float scale_factor = 1.0F;
options.Get("scaleFactor", &scale_factor);
gfx::ImageSkia image_skia =
gfx::ImageSkia::CreateFromBitmap(bitmap, scale_factor);

View file

@ -138,7 +138,7 @@ class NativeImage final : public gin::Wrappable<NativeImage> {
gfx::Image image_;
raw_ptr<v8::Isolate> isolate_;
int32_t memory_usage_ = 0;
int64_t memory_usage_ = 0;
};
} // namespace electron::api