Skip to content

Commit f3184ba

Browse files
VoltrexKeyvazbjornson
authored andcommitted
src: refactor and apply fixes
- Defer the initialization of the `op` variable to the `default` switch case to avoid a compiler warning. - Use a `default` switch case with a null statement if some enum values aren't suppsed to be handled, this avoids a compiler warning. - Migrate from librsvg's deprecated `rsvg_handle_get_dimensions()` and `rsvg_handle_render_cairo()` functions to the new `rsvg_handle_get_intrinsic_size_in_pixels()` and `rsvg_handle_render_document()` respectively. - Avoid calling virtual methods in constructors/destructors to avoid bypassing virtual dispatch. - Remove unused private field `backend` in the `Backend` class. - Fix a case of use-after-free. - Fix usage of garbage value by filling the allocated memory entirely with zeros if it's not modified. - Fix a potential memory leak.
1 parent 38e0a32 commit f3184ba

9 files changed

+40
-15
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@ project adheres to [Semantic Versioning](http://semver.org/).
88
(Unreleased)
99
==================
1010
### Changed
11+
* Defer the initialization of the `op` variable to the `default` switch case to avoid a compiler warning. (#2229)
12+
* Use a `default` switch case with a null statement if some enum values aren't suppsed to be handled, this avoids a compiler warning. (#2229)
13+
* Migrate from librsvg's deprecated `rsvg_handle_get_dimensions()` and `rsvg_handle_render_cairo()` functions to the new `rsvg_handle_get_intrinsic_size_in_pixels()` and `rsvg_handle_render_document()` respectively. (#2229)
14+
* Avoid calling virtual methods in constructors/destructors to avoid bypassing virtual dispatch. (#2229)
15+
* Remove unused private field `backend` in the `Backend` class. (#2229)
1116
### Added
1217
### Fixed
18+
* Fix a case of use-after-free. (#2229)
19+
* Fix usage of garbage value by filling the allocated memory entirely with zeros if it's not modified. (#2229)
20+
* Fix a potential memory leak. (#2229)
1321

1422
2.11.2
1523
==================

src/Canvas.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ NAN_METHOD(Canvas::New) {
133133
}
134134

135135
if (!backend->isSurfaceValid()) {
136+
const char *error = backend->getError();
136137
delete backend;
137-
return Nan::ThrowError(backend->getError());
138+
return Nan::ThrowError(error);
138139
}
139140

140141
Canvas* canvas = new Canvas(backend);

src/CanvasRenderingContext2d.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,8 @@ Context2d::blur(cairo_surface_t *surface, int radius) {
607607
// get width, height
608608
int width = cairo_image_surface_get_width( surface );
609609
int height = cairo_image_surface_get_height( surface );
610-
unsigned* precalc =
611-
(unsigned*)malloc(width*height*sizeof(unsigned));
610+
const unsigned int size = width * height * sizeof(unsigned);
611+
unsigned* precalc = (unsigned*)malloc(size);
612612
cairo_surface_flush( surface );
613613
unsigned char* src = cairo_image_surface_get_data( surface );
614614
double mul=1.f/((radius*2)*(radius*2));
@@ -627,6 +627,8 @@ Context2d::blur(cairo_surface_t *surface, int radius) {
627627
unsigned char* pix = src;
628628
unsigned* pre = precalc;
629629

630+
bool modified = false;
631+
630632
pix += channel;
631633
for (y=0;y<height;y++) {
632634
for (x=0;x<width;x++) {
@@ -635,10 +637,15 @@ Context2d::blur(cairo_surface_t *surface, int radius) {
635637
if (y>0) tot+=pre[-width];
636638
if (x>0 && y>0) tot-=pre[-width-1];
637639
*pre++=tot;
640+
if (!modified) modified = true;
638641
pix += 4;
639642
}
640643
}
641644

645+
if (!modified) {
646+
memset(precalc, 0, size);
647+
}
648+
642649
// blur step.
643650
pix = src + (int)radius * width * 4 + (int)radius * 4 + channel;
644651
for (y=radius;y<height-radius;y++) {
@@ -1432,7 +1439,7 @@ NAN_GETTER(Context2d::GetGlobalCompositeOperation) {
14321439
Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
14331440
cairo_t *ctx = context->context();
14341441

1435-
const char *op = "source-over";
1442+
const char *op{};
14361443
switch (cairo_get_operator(ctx)) {
14371444
// composite modes:
14381445
case CAIRO_OPERATOR_CLEAR: op = "clear"; break;
@@ -1469,6 +1476,7 @@ NAN_GETTER(Context2d::GetGlobalCompositeOperation) {
14691476
case CAIRO_OPERATOR_HSL_LUMINOSITY: op = "luminosity"; break;
14701477
// non-standard:
14711478
case CAIRO_OPERATOR_SATURATE: op = "saturate"; break;
1479+
default: op = "source-over";
14721480
}
14731481

14741482
info.GetReturnValue().Set(Nan::New(op).ToLocalChecked());
@@ -2507,6 +2515,7 @@ Context2d::setTextPath(double x, double y) {
25072515
pango_layout_get_pixel_extents(_layout, NULL, &logical_rect);
25082516
x -= logical_rect.width;
25092517
break;
2518+
default: ;
25102519
}
25112520

25122521
y -= getBaselineAdjustment(_layout, state->textBaseline);

src/Image.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,11 +1192,13 @@ Image::loadSVGFromBuffer(uint8_t *buf, unsigned len) {
11921192
return CAIRO_STATUS_READ_ERROR;
11931193
}
11941194

1195-
RsvgDimensionData *dims = new RsvgDimensionData();
1196-
rsvg_handle_get_dimensions(_rsvg, dims);
1195+
double d_width;
1196+
double d_height;
11971197

1198-
width = naturalWidth = dims->width;
1199-
height = naturalHeight = dims->height;
1198+
rsvg_handle_get_intrinsic_size_in_pixels(_rsvg, &d_width, &d_height);
1199+
1200+
width = naturalWidth = d_width;
1201+
height = naturalHeight = d_height;
12001202

12011203
status = renderSVGToSurface();
12021204
if (status != CAIRO_STATUS_SUCCESS) {
@@ -1232,7 +1234,13 @@ Image::renderSVGToSurface() {
12321234
return status;
12331235
}
12341236

1235-
gboolean render_ok = rsvg_handle_render_cairo(_rsvg, cr);
1237+
RsvgRectangle viewport = {
1238+
.x = 0,
1239+
.y = 0,
1240+
.width = static_cast<double>(width),
1241+
.height = static_cast<double>(height),
1242+
};
1243+
gboolean render_ok = rsvg_handle_render_document(_rsvg, cr, &viewport, nullptr);
12361244
if (!render_ok) {
12371245
g_object_unref(_rsvg);
12381246
cairo_destroy(cr);

src/backend/Backend.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Backend::Backend(std::string name, int width, int height)
99

1010
Backend::~Backend()
1111
{
12-
this->destroySurface();
12+
Backend::destroySurface();
1313
}
1414

1515
void Backend::init(const Nan::FunctionCallbackInfo<v8::Value> &info) {
@@ -97,8 +97,7 @@ bool Backend::isSurfaceValid(){
9797

9898
BackendOperationNotAvailable::BackendOperationNotAvailable(Backend* backend,
9999
std::string operation_name)
100-
: backend(backend)
101-
, operation_name(operation_name)
100+
: operation_name(operation_name)
102101
{
103102
msg = "operation " + operation_name +
104103
" not supported by backend " + backend->getName();

src/backend/Backend.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class Backend : public Nan::ObjectWrap
5757
class BackendOperationNotAvailable: public std::exception
5858
{
5959
private:
60-
Backend* backend;
6160
std::string operation_name;
6261
std::string msg;
6362

src/backend/PdfBackend.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ using namespace v8;
88

99
PdfBackend::PdfBackend(int width, int height)
1010
: Backend("pdf", width, height) {
11-
createSurface();
11+
PdfBackend::createSurface();
1212
}
1313

1414
PdfBackend::~PdfBackend() {

src/backend/SvgBackend.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ using namespace v8;
99

1010
SvgBackend::SvgBackend(int width, int height)
1111
: Backend("svg", width, height) {
12-
createSurface();
12+
SvgBackend::createSurface();
1313
}
1414

1515
SvgBackend::~SvgBackend() {

src/register_font.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ get_pango_font_description(unsigned char* filepath) {
304304
}
305305

306306
pango_font_description_set_family_static(desc, family);
307+
free(family);
307308
pango_font_description_set_weight(desc, get_pango_weight(table->usWeightClass));
308309
pango_font_description_set_stretch(desc, get_pango_stretch(table->usWidthClass));
309310
pango_font_description_set_style(desc, get_pango_style(face->style_flags));

0 commit comments

Comments
 (0)