Skip to content

Commit

Permalink
Improve safety of Blurhash component (#14278)
Browse files Browse the repository at this point in the history
There was a missed empty hash check. As well as rendering is now wrapped
in try/catch block, so app won't crash if any Blurhash component fails
to render its contents as it's not that critical.
  • Loading branch information
brawaru committed Jul 9, 2020
1 parent 6fda3cb commit 3ef94c0
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions app/javascript/mastodon/components/blurhash.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PropTypes from 'prop-types';

/**
* @typedef BlurhashPropsBase
* @property {string} hash Hash to render
* @property {string?} hash Hash to render
* @property {number} width
* Width of the blurred region in pixels. Defaults to 32
* @property {number} [height]
Expand Down Expand Up @@ -37,13 +37,17 @@ function Blurhash({
const { current: canvas } = canvasRef;
canvas.width = canvas.width; // resets canvas

if (dummy) return;
if (dummy || !hash) return;

const pixels = decode(hash, width, height);
const ctx = canvas.getContext('2d');
const imageData = new ImageData(pixels, width, height);
try {
const pixels = decode(hash, width, height);
const ctx = canvas.getContext('2d');
const imageData = new ImageData(pixels, width, height);

ctx.putImageData(imageData, 0, 0);
ctx.putImageData(imageData, 0, 0);
} catch (err) {
console.error('Blurhash decoding failure', { err, hash });
}
}, [dummy, hash, width, height]);

return (
Expand Down

0 comments on commit 3ef94c0

Please sign in to comment.