Skip to content

Commit

Permalink
catch exceptions in casting constructors (#74)
Browse files Browse the repository at this point in the history
* catch exceptions in casting constructors

* Fix the leaks
  • Loading branch information
mmomtchev authored Jan 26, 2024
1 parent 81226f0 commit 9d5af3d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

### [1.0.2]

- Upgrade ImageMagick to the latest 7.1.1-27 (git)
- Fix [#72](https://github.com/mmomtchev/magickwand.js/issues/72), exception in constructors used for implicit casting are not catchable

### [1.0.1] 2024-01-18

- Update ImageMagick to 7.1.1-26
Expand Down
15 changes: 13 additions & 2 deletions src/ImplicitCasting.i
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@
{
// We apply the default std::string typemap - it parses
// its argument in a local temporary $1
std::string *$1;
std::string *$1 = nullptr;
$typemap(in, const std::string &);
// Construct the object from this string
from_string_temp = $*ltype(*$1);
try {
// SWIG will automatically insert an exception handler in actions
// but this does not apply to typemaps - here we call ImageMagick code
// which can throw
from_string_temp = $*ltype(*$1);
} catch (const Magick::Exception &e) {
delete $1;
// SWIG_Raise is the language-independent macro which expands to
// the language-specific function call, it is available only in .i files
SWIG_NAPI_Raise(env, e.what());
SWIG_fail;
}
// The string is not needed anymore
delete $1;
}
Expand Down
6 changes: 6 additions & 0 deletions test/Color.shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ export default function (
.map((v) => Math.floor(v * 255).toString(16).padStart(2, '0')).join(''),
'#ff7f0160');
});

it('handling invalid color', () => {
assert.throws(() => {
new Color('invalid');
}, /unrecognized color `invalid'/);
});
});
});
}
6 changes: 6 additions & 0 deletions test/Image.shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ export default function (
// @ts-ignore
assert.throws(() => new Image(null), /Illegal arguments for construction/);
});

it('handling invalid color', () => {
assert.throws(() => {
new Image('100x100', 'invalid');
}, /unrecognized color `invalid'/);
});
});

describe('pixelColor', () => {
Expand Down

0 comments on commit 9d5af3d

Please sign in to comment.