Skip to content

Commit

Permalink
don't crash if fillRect outside of buffer bounds
Browse files Browse the repository at this point in the history
add unit test for it
  • Loading branch information
joshmarinacci committed Sep 23, 2024
1 parent 06262da commit 3ea5db8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,10 @@ export class Context {
if (new_rgba[3] === 255) {
for (let i = x; i < x + w; i++) {
for (let j = y; j < y + h; j++) {
if (this.pixelInsideClip(i, j)) {
if (
this.pixelInsideClip(i, j) &&
this._bitmap._isValidCoords(i, j)
) {
this._bitmap.setPixelRGBA(i, j, this._fillColor as number);
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ describe("draw rect", () => {
expect(image.getPixelRGBA(100, 100)).to.eq(WHITE);
await save(image, "path/rect/fill_square-fillRect");
});
it("fill rect partially outside of buffer", async () => {
c.fillStyle = "black";
c.fillRect(100, 100, 200, 200);
expect(image.getPixelRGBA(0, 0)).to.eq(WHITE);
// expect(image.getPixelRGBA(11, 11)).to.eq(BLACK);
expect(image.getPixelRGBA(150, 150)).to.eq(BLACK);
// expect(image.getPixelRGBA(100, 100)).to.eq(WHITE);
await save(image, "path/rect/fill_outside-fillRect");
});
it("fill square with lines", async () => {
c.beginPath();
c.moveTo(10, 10);
Expand Down

0 comments on commit 3ea5db8

Please sign in to comment.