From d92d4b1e67d2ce3d6977582f8e184f22f1341333 Mon Sep 17 00:00:00 2001 From: buliabyak Date: Wed, 9 Jul 2008 19:33:25 +0000 Subject: [PATCH] prevent assert crashes on bad data in cal1 or cal2 --- src/dyna-draw-context.cpp | 58 ++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/src/dyna-draw-context.cpp b/src/dyna-draw-context.cpp index 1d6538aa7..eb54ce5fe 100644 --- a/src/dyna-draw-context.cpp +++ b/src/dyna-draw-context.cpp @@ -92,7 +92,7 @@ static gint sp_dyna_draw_context_root_handler(SPEventContext *ec, GdkEvent *even static void clear_current(SPDynaDrawContext *dc); static void set_to_accumulated(SPDynaDrawContext *dc, bool unionize); static void add_cap(SPCurve *curve, NR::Point const &from, NR::Point const &to, double rounding); -static void accumulate_calligraphic(SPDynaDrawContext *dc); +static bool accumulate_calligraphic(SPDynaDrawContext *dc); static void fit_and_split(SPDynaDrawContext *ddc, gboolean release); @@ -807,8 +807,10 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context, /* Create object */ fit_and_split(dc, TRUE); - accumulate_calligraphic(dc); - set_to_accumulated(dc, event->button.state & GDK_SHIFT_MASK); // performs document_done + if (accumulate_calligraphic(dc)) + set_to_accumulated(dc, event->button.state & GDK_SHIFT_MASK); // performs document_done + else + g_warning ("Failed to create path: invalid data in dc->cal1 or dc->cal2"); /* reset accumulated curve */ dc->accumulated->reset(); @@ -1023,26 +1025,49 @@ add_cap(SPCurve *curve, } } -static void +static bool accumulate_calligraphic(SPDynaDrawContext *dc) { - if ( !dc->cal1->is_empty() && !dc->cal2->is_empty() ) { - dc->accumulated->reset(); /* Is this required ?? */ - SPCurve *rev_cal2 = dc->cal2->create_reverse(); + if ( + dc->cal1->is_empty() || + dc->cal2->is_empty() || + (dc->cal1->get_segment_count() <= 0) || + dc->cal1->first_path()->closed() + ) { + dc->cal1->reset(); + dc->cal2->reset(); + return false; // failure + } - g_assert(dc->cal1->get_segment_count() > 0); - g_assert(rev_cal2->get_segment_count() > 0); - g_assert( ! dc->cal1->first_path()->closed() ); - g_assert( ! rev_cal2->first_path()->closed() ); + SPCurve *rev_cal2 = dc->cal2->create_reverse(); + if ( + (rev_cal2->get_segment_count() <= 0) || + rev_cal2->first_path()->closed() + ) { + rev_cal2->unref(); + dc->cal1->reset(); + dc->cal2->reset(); + return false; // failure + } Geom::CubicBezier const * dc_cal1_firstseg = dynamic_cast( dc->cal1->first_segment() ); Geom::CubicBezier const * rev_cal2_firstseg = dynamic_cast( rev_cal2->first_segment() ); Geom::CubicBezier const * dc_cal1_lastseg = dynamic_cast( dc->cal1->last_segment() ); Geom::CubicBezier const * rev_cal2_lastseg = dynamic_cast( rev_cal2->last_segment() ); - g_assert( dc_cal1_firstseg ); - g_assert( rev_cal2_firstseg ); - g_assert( dc_cal1_lastseg ); - g_assert( rev_cal2_lastseg ); + + if ( + !dc_cal1_firstseg || + !rev_cal2_firstseg || + !dc_cal1_lastseg || + !rev_cal2_lastseg + ) { + rev_cal2->unref(); + dc->cal1->reset(); + dc->cal2->reset(); + return false; // failure + } + + dc->accumulated->reset(); /* Is this required ?? */ dc->accumulated->append(dc->cal1, false); @@ -1058,7 +1083,8 @@ accumulate_calligraphic(SPDynaDrawContext *dc) dc->cal1->reset(); dc->cal2->reset(); - } + + return true; // success } static double square(double const x) -- 2.30.2