Skip to content

Commit 5e4382f

Browse files
authored
Catch std::overflow_error exceptions from AGG (#97)
1 parent 39baabb commit 5e4382f

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

agg-svn/agg-2.4/include/agg_rasterizer_cells_aa.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,6 @@ namespace agg
185185
{
186186
if((m_num_cells & cell_block_mask) == 0)
187187
{
188-
if(m_num_blocks >= m_cell_block_limit) {
189-
throw std::overflow_error("Exceeded cell block limit");
190-
}
191188
allocate_block();
192189
}
193190
*m_curr_cell_ptr++ = m_curr_cell;
@@ -474,6 +471,10 @@ namespace agg
474471
{
475472
if(m_num_blocks >= m_max_blocks)
476473
{
474+
if(m_num_blocks >= m_cell_block_limit) {
475+
throw std::overflow_error("Exceeded cell block limit");
476+
}
477+
477478
cell_type** new_cells =
478479
pod_allocator<cell_type*>::allocate(m_max_blocks +
479480
cell_block_pool);

celiagg/_ndarray_canvas.pxd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ cdef extern from "ndarray_canvas.h":
6767
void draw_shape(_vertex_source.VertexSource& shape,
6868
const _transform.trans_affine& transform,
6969
_paint.Paint& linePaint, _paint.Paint& fillPaint,
70-
const _graphics_state.GraphicsState& gs)
70+
const _graphics_state.GraphicsState& gs) except +
7171
void draw_shape_at_points(_vertex_source.VertexSource& shape,
7272
const double* points,
7373
const size_t point_count,
7474
const _transform.trans_affine& transform,
7575
_paint.Paint& linePaint, _paint.Paint& fillPaint,
76-
const _graphics_state.GraphicsState& gs)
76+
const _graphics_state.GraphicsState& gs) except +
7777
void draw_text(const char* text, _font.Font& font,
7878
const _transform.trans_affine& transform,
7979
_paint.Paint& linePaint, _paint.Paint& fillPaint,

celiagg/tests/test_canvas.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,35 @@ def test_stencil_size_mismatch(self):
9393
with self.assertRaises(agg.AggError):
9494
canvas.draw_shape(path, transform, gs)
9595

96+
def test_rasterizer_cell_overflow(self):
97+
canvas = agg.CanvasRGB24(np.zeros((100, 100, 3), dtype=np.uint8))
98+
gs = agg.GraphicsState()
99+
transform = agg.Transform()
100+
path = agg.Path()
101+
102+
def genpoints(num):
103+
arr = np.empty((num, 2))
104+
arr[::2, 0] = np.linspace(0, 99, num=arr[::2, 0].shape[0])
105+
arr[::2, 1] = 0.0
106+
arr[1::2, 0] = np.linspace(1, 100, num=arr[1::2, 0].shape[0])
107+
arr[1::2, 1] = 100.0
108+
return arr
109+
110+
# This many points definitely generates more than 2^22 cells
111+
count = 2**22 // 100 // 2
112+
points = genpoints(count)
113+
path.lines(points)
114+
with self.assertRaises(OverflowError):
115+
canvas.draw_shape(path, transform, gs)
116+
117+
path.reset()
118+
119+
# This many points is OK
120+
count = 2**22 // 103 // 2
121+
points = genpoints(count)
122+
path.lines(points)
123+
canvas.draw_shape(path, transform, gs)
124+
96125
def test_clear(self):
97126
expected = np.zeros((4, 4, 3), dtype=np.uint8)
98127
buffer = np.zeros((4, 4, 3), dtype=np.uint8)

0 commit comments

Comments
 (0)