[cairo] Potential division by zero in cairo_arc

Luiz Americo Pereira Camara luizmed at oi.com.br
Mon May 5 18:50:03 PDT 2008


My name is Luiz Américo and i've been following cairo development with 
interest for some time. Before anything i'd like to say that cairo is a 
great piece of software.

My main programming language is pascal/object pascal using freepascal 
compiler, so in order to use cairo, i updated the pascal bindings up to 
cairo 1.4, started a object pascal OOP binding and integrated cairo with 
Lazarus*. I also ported some demos like the gtkmm cairoclock, JanaGtk 
Clock and snippets demo.

While porting the snippets demo (http://cairographics.org/samples/) i 
faced a bug in the arc example at line:

|  cairo_arc (cr, xc, yc, radius, angle1, angle1);

The interesting is that the bug (an Invalid Float Point Operation exception) occurs only when using the freepascal compiler and not gcc. 
Initially i think that fpc was generating some incompatible code, but today i found the reason. See a snippet of _cairo_arc_in_direction function:

        segments = _arc_segments_needed (angle_max - angle_min,
					 radius, &ctm,
					 cairo_get_tolerance (cr));
	angle_step = (angle_max - angle_min) / (double) segments;

When values of angle1 and angle2 parameters of cairo_arc are equal, 
segments variable will get a 0 value. This will lead to a division by 
zero. Somehow gcc generates code that ignores this and assign nan to 
angle_step. No error is visible because angle_step is not used (the for 
loop is not executed). The fix should be a trivial check for the 
segments value > 0.

I hope this helps.

* Lazarus is a Delphi like RAD with the advantages of being cross 
platform and open source.



More information about the cairo mailing list